pytest安装
pip3 install -U pytest
pytest的main运行方式
import pytest
def test_a():
print(">>>>>>>>>test_a")
assert True
def test_b():
print(">>>>>>>>>test_a")
assert False
if __name__ == '__main__':
#pytest main运行方式
pytest.main(["-s","test001.py"])
插件名称:pytest-html —生成报告
安装方式:
1.安装包方式 python setup.py install
2.命令行 pip3 install pytest-html
使用方法:
命令行格式:pytest --html=用户路径/report.html
运行方式:
1.修改Test_App/pytest.ini文件,添加报告参数,即:addopts = -s --html=./report.html
# -s:输出程序运行信息
# --html=./report.html 在当前目录下生成report.html文件
⚠️ 若要生成xml文件,可将–html=./report.html 改成 --html=./report.xml
2.命令行进入Test_App目录
3.执行命令: pytest
执行结果:
1.在当前目录会生成assets文件夹和report.html文件
插件名称:pytest-ordering —控制函数的执行顺序
安装方式:
1.安装包方式 python setup.py install
2.命令行 pip3 install pytest-ordering
使用方法:
1.标记于被测试函数,@pytest.mark.run(order=x)
2.根据order传入的参数来解决运行顺序
3.order值全为正数或全为负数时,运行顺序:值越小,优先级越高
4.正数和负数同时存在:正数优先级高
插件名称:pytest-rerunfailures ----失败重试
安装方式:
1.安装包方式 python setup.py install
2.命令行 pip3 install pytest-rerunfailures
使用方法:
命令行格式:pytest --reruns n # n:为重试的次数
pytest的配置文件—pytest.ini
[pytest]
# 命令行参数 ---失败重试1次,报告放在report文件夹,报告名为report
addopts = -s --reruns 1 --html=./report/report.html
# 测试路径 ---测试脚本的路径文件夹
testpaths = ./scripts_test
# 搜索文件名 ---找test_开始的文件名
python_file = test_*.py
# 搜索测试类名 ---Test_开始的类名
python_classes = Test_*
#搜索测试方法名 ---test_开始的方法名
python_functions = test_*
fixture
方法:fixture(scope=“function”, params=None, autouse=False, ids=None, name=None)
常用参数:
scope:被标记方法的作用域
function" (default):作用于每个测试方法,每个test都运行一次
“class”:作用于整个类,每个class的所有test只运行一次
“module”:作用于整个模块,每个module的所有test只运行一次
"session:作用于整个session(慎用),每个session只运行一次
params:(list类型)提供参数数据,供调用标记方法的函数使用
autouse:是否自动运行,默认为False不运行,设置为True自动运行
通过参数引用
import pytest
class Test_ABC:
@pytest.fixture()
def before(self):
print("------->before")
def test_a(self,before): # ⚠️ test_a方法传入了被fixture标识的函数,已变量的形式
print("------->test_a")
assert 1
if __name__ == '__main__':
pytest.main(["-s","test_abc.py"])
注:发现before自动优先于测试类运行
通过函数引用
import pytest
@pytest.fixture() # fixture标记的函数可以应用于测试类外部
def before():
print("------->before")
@pytest.mark.usefixtures("before")
class Test_ABC:
def setup(self):
print("------->setup")
def test_a(self):
print("------->test_a")
assert 1
if __name__ == '__main__':
pytest.main(["-s","test_abc.py"])
注:发现before自动优先于测试类运行
通过默认设置运行
import pytest
@pytest.fixture(autouse=True) # 设置为默认运行
def before():
print("------->before")
class Test_ABC:
def setup(self):
print("------->setup")
def test_a(self):
print("------->test_a")
assert 1
if __name__ == '__main__':
pytest.main(["-s","test_abc.py"])
注:发现before自动优先于测试类运行
设置作用域为function
import pytest
@pytest.fixture(scope='function',autouse=True) # 设置为默认运行
def before():
print("------->before")
class Test_ABC:
def setup(self):
print("------->setup")
def test_a(self):
print("------->test_a")
assert 1
def test_b(self):
print("------->test_b")
assert 1
if __name__ == '__main__':
pytest.main(["-s","test_abc.py"])
注:发现before运行了两次
设置作用域为class
import pytest
@pytest.fixture(scope='class',autouse=True) # 设置为默认运行
def before():
print("------->before")
class Test_ABC:
def setup(self):
print("------->setup")
def test_a(self):
print("------->test_a")
assert 1
def test_b(self):
print("------->test_b")
assert 1
if __name__ == '__main__':
pytest.main(["-s","test_abc.py"])
返回值
import pytest
@pytest.fixture()
def need_data():
return 2 # 返回数字2
class Test_ABC:
def test_a(self, need_data):
print("------->test_a")
assert need_data != 3 # 拿到返回值做一次断言
if __name__ == '__main__':
pytest.main(["-s","test_abc.py"])
下面这个会发现结果运行了三遍
import pytest
@pytest.fixture(params=[1, 2, 3])
def need_data(request): # 传入参数request 系统封装参数
return request.param # 取列表中单个值,默认的取值方式
class Test_ABC:
def test_a(self,need_data):
print("------->test_a")
assert need_data != 3 # 断言need_data不等于3
if __name__ == '__main__':
pytest.main(["-s","test_abc.py"])
跳过测试函数
方法:
skipif(condition, reason=None)
参数:
condition:跳过的条件,必传参数
reason:标注原因,必传参数
使用方法:
@pytest.mark.skipif(condition, reason=“xxx”)
标记为预期失败函数
方法:
xfail(condition=None, reason=None, raises=None, run=True, strict=False)
常用参数:
condition:预期失败的条件,必传参数
reason:失败的原因,必传参数
使用方法:
@pytest.mark.xfail(condition, reason=“xx”)
函数数据参数化
方法:
parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)
常用参数:
argnames:参数名
argvalues:参数对应值,类型必须为list
当参数为一个时格式:[value]
当参数个数大于一个时,格式为:[(param_value1,param_value2…),(param_value1,param_value2…)]
使用方法:
@pytest.mark.parametrize(argnames,argvalues)
⚠️ 参数值为N个,测试方法就会运行N次
单个参数示例:
import pytest
class Test_ABC:
def setup_class(self):
print("------->setup_class")
def teardown_class(self):
print("------->teardown_class")
@pytest.mark.parametrize("a",[3,6]) # a参数被赋予两个值,函数会运行两遍
def test_a(self,a): # 参数必须和parametrize里面的参数一致
print("test data:a=%d"%a)
assert a%3 == 0
执行结果:
test_abc.py
------->setup_class
test data:a=3 # 运行第一次取值a=3
.
test data:a=6 # 运行第二次取值a=6
.
------->teardown_class
多个参数示例:
import pytest
class Test_ABC:
def setup_class(self):
print("------->setup_class")
def teardown_class(self):
print("------->teardown_class")
@pytest.mark.parametrize("a,b",[(1,2),(0,3)]) # 参数a,b均被赋予两个值,函数会运行两遍
def test_a(self,a,b): # 参数必须和parametrize里面的参数一致
print("test data:a=%d,b=%d"%(a,b))
assert a+b == 3
执行结果:
test_abc.py
------->setup_class
test data:a=1,b=2 # 运行第一次取值 a=1,b=2
.
test data:a=0,b=3 # 运行第二次取值 a=0,b=3
.
------->teardown_class
函数返回值类型示例:
import pytest
def return_test_data():
return [(1,2),(0,3)]
class Test_ABC:
def setup_class(self):
print("------->setup_class")
def teardown_class(self):
print("------->teardown_class")
@pytest.mark.parametrize("a,b",return_test_data()) # 使用函数返回值的形式传入参数值
def test_a(self,a,b):
print("test data:a=%d,b=%d"%(a,b))
assert a+b == 3
执行结果:
test_abc.py
------->setup_class
test data:a=1,b=2 # 运行第一次取值 a=1,b=2
.
test data:a=0,b=3 # 运行第二次取值 a=0,b=3
.
------->teardown_class