目录
8.1 @pytest.mark.parametrize(argnames,argvalues)
8.2 @pytest.fixtrue(params=[])
8.3 pytest_generate_tests(metafunc)
1.识别
识别测试文件
test_*.py
*_test.py
识别测试用例
test_* #函数
test_* 类(不能有__init__()方法)的 test_* 方法 #类方法
2.运行和参数
运行
pytest #识别并运行当前目录下的全部用例
pytest filename.py #识别并运行filename中的全部用例
pytest filename.py::testclass #运行filename的testclass类中的全部方法用例(函数、单独类方法也用这种参数格式来指定)
参数
参数 -v #打印pytest执行的详细日志信息
参数 -s #带控制台输出结果
参数 -x #运行过程中遇到报错,停止运行
参数 --maxfail=num #运行过程中遇到报错的数量为num时,停止运行
参数 -k "test_case" #运行中跳过用例名包含“test_case”的用例
参数 -m markname #用装饰器 @pytest.mark.markname对用例分组,运行时只执行markname标记的用例
参数 --result-log=path #将测试结果保存到本地path
参数 --collect-only #不执行用例,只识别、收集出将会执行的用例
3.失败重新执行 rerunfailures
install pytest.rerunfailures
参数 --reruns num #运行中遇到用例执行失败,重新执行这个用例num次
参数 --reruns num --reruns-delay ns #重新执行用例num次,执行间隔时间ns秒
4.断言失败继续向下执行 assume
install pytest.assume
用例中存在多条断言,当一个断言失败时,后面的断言就不会执行了,如果想断言失败后继续向下执行,用 pytest.assume() 替换 assert 进行断言,示例:
errormessage = r.json()[errormessage]
pytest.assume(response.status_code == 200) #该条断言失败,继续向下执行,不会停止
pytest.assume(errormessage == '')
5.setup,teardown
setup_module/teardown_module #模块文件级
setup_function/teardown_function #函数级
setup_class/teardown_class #类级
setup_method/teardown_method #类方法级
setup/teardown #对每个方法执行一次
6.fixture 功能
fixture是用 @pytest.fixture 装饰器定义的函数。在编写测试函数时,可以将fixture名作为参数传入,这样会将fixtrue的return/yield返回值作为传入的参数。
fixture的主要目的是提供一种可靠、可重复性的手段去运行最基本的测试内容。
6.1 参数 scope
用于设置fixture共享的作用域。
默认为“function”,还可以是 class、module、package、session。
例如,fixture=module,那这个fixture的作用域就是整个模块文件,也就是说,在整个模块文件中都可以使用这个fixtrue。
6.2 参数 params
一个列表,用 request.params 来调用。
params 列表被遍历,元素会作为测试用例的入参,执行测试用例。运行直到列表遍历完,也就是说,列表有多少元素,测试用例就执行几次。
示例:
@pytest.fixture(scope="function",params=["2015","1883"])
def testdata(request):
return request.param
def test_func(testdata):
print(testdata)
assert type(testdata) is str
运行结果:
collecting ... collected 2 items
test_sample.py::test_func[2015] PASSED [ 50%]2015
test_sample.py::test_func[1883] PASSED [100%]1883
============================== 2 passed in 0.10s ==============================
可见,两个元素"2015","1883",test_func执行了两次,入参分别为"2015","1883"。
6.3 参数 autouse
默认为False,如果是True,就会为该fixtrue的作用域中的所有用例,自动应用该fixtrue功能(即参数传入该fixtrue名)。
7.conftest.py
conftest.py 文件是pytest专门用于共享的地方,有当前目录下的自动模块发现机制,全局配置和前置工作可以放到这里。比如一个测试用例的使用了fixtrue功能,执行时会先在文件里查找这个fixtrue,文件中没有,再去conftest.py文件中查找。
conftest.py 的作用域是所在目录及子目录。
8.参数化
8.1 @pytest.mark.parametrize(argnames,argvalues)
@pytest.mark.parametrize("argName1,argName2...",argValueList)
示例:
response = [(True,''),(False,'the response time out'),(True,'')]
@pytest.mark.parametrize("key,message",response)
def test_func(key,message):
print(message)
assert key is True
运行结果:
collected 3 items
test_sample.py::test_func[True-0] PASSED [ 33%]
test_sample.py::test_func[False-the response time out] FAILED [ 66%]
test_sample.py::test_func[True-1] PASSED [100%]
==================================1 failed, 2 passed in 0.25s=================================
8.2 @pytest.fixtrue(params=[])
6.2
8.3 pytest_generate_tests(metafunc)
hook函数,用于自定义动态参数化方案、扩展
9.assert
pytest 对Python原生的assert语句进行了优化和改进,当断言失败时,将错误的具体信息和位置显示出来。
除了对正常运行的结果断言外,pytest也能够对Exception和Warnning进行断言,断定在某种条件下,一定会出现某种异常或者警告。
示例:
def test_zero_division():
with pytest.raises(ZeroDivisionError) as excinfo:
1 / 0
assert "division by zero" in str(excinfo.value)
这里是断言字符串"division by zero"一定在str(excinfo.value)中。