栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytest--编写以及管理测试用例

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytest--编写以及管理测试用例

测试文件目录

测试案例要和源码分开,单独放在一个tests文件夹里
tests文件夹位于项目根目录

一般测试用例的目录这样
conftest包含hook函数和fixture
pytest.ini,保存pytest在该项目下的配置

assert

断言有两种:第一种,在assert 后面添加任何表达式
assert
第二种:异常类型的断言,with pytest.raise()

def test_div_one():
    with pytest.raises(ZeroDivisionError):
        result=my_div(1,0)

更进一步,如果想断言异常抛出的信息,可以使用value.args[0]来获取到异常信息

def test_div_two():
    with pytest.raises(ZeroDivisionError) as exceptInfo:
        my_div(1,0)
    excepted_msg=exceptInfo.value.args[0]
    assert excepted_msg=='division by zero'
管理测试用例 (1)通过使用marker(标记)对测试用例进行分组

比如选择一些测试用例作为冒烟测试,可以使用装饰器pytest.mark.smoke来标记这些测试用例
运行的时候,通过-m选项来指定运行那些标记
pytest -m “smoke”
表示运行标记为smoke的测试用例
-m后面也可以使用表达式,可以在标记之前添加 and or not等
比如 pytest -m “smoke and get” 运行标记为smoke和get的测试用例

(2)跳过测试用例

pytest内置了一些标记,skip,skipif xfail
skip和skipif允许跳过不希望运行的测试用例
只是简单的跳过某个测试用例,使用pytest.mark.skip(reason=" xxxx")
如果要根据某个条件来判断是否跳过用例,使用pytest.mark.skipif(condition,reason), condition是条件,当条件是true时,跳过该测试用例,reason是原因

参数化测试用例

如果使用多组数据来测试同一个测试用例,可以使用@pytest.mark.parametrize(argnames,argvalues)装饰器达到批量传送参数的目的
第一个参数是str,List[str],Tuple[str]
第二个参数是一个值列表

@pytest.mark.parametrize("a",[1,5,7])
def test_parmetrized(a):
    print(a+1)
@pytest.mark.parametrize(["a","b"],[(1,2),(3,4)])
def test_parametrized_one(a,b):
    print(a+b)

运行结果:

C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py"
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe
cachedir: .pytest_cache
rootdir: E:JenkinsLearnMy-pytesttest, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 2 items

test_my_add.py::test_parametrized_one[1-2] 3
PASSED
test_my_add.py::test_parametrized_one[3-4] 7
PASSED
@pytest.mark.parametrize(("a","b","c"),[("one","two","three")])
def test_paramtrized_two(a,b,c):
    print(a+b+c)

运行结果:

C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py"
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe
cachedir: .pytest_cache
rootdir: E:JenkinsLearnMy-pytesttest, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 1 item

test_my_add.py::test_paramtrized_two[one-two-three] onetwothree
PASSED
@pytest.mark.parametrize("a,b,c",[(1,2,3),(4,5,6)])
def test_parametrized_three(a,b,c):
    print(a+b+c)

运行结果:

C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py"
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe
cachedir: .pytest_cache
rootdir: E:JenkinsLearnMy-pytesttest, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 2 items

test_my_add.py::test_parametrized_three[1-2-3] 6
PASSED
test_my_add.py::test_parametrized_three[4-5-6] 15
PASSED
运行测试子集

运行单个目录:pytest tests/func
运行单个测试文件/模块 pytest tests/func/test_add.py
运行单个测试函数 pytest tests/func/test_add.py::test_one
运行单个测试类 pytest tests/func/test_add.py::TestAdd
运行测试类中的单个测试方法 pytest tests/func/test_add.py::TestAdd::test_one

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/739267.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号