pytest——笔记整理
1.测试用例搜索规则:
1.可以指定测试文件,如果没有指定测试文件,会从当前目录(包括子目录)下查找
2.测试文件命名规则以_test开头或_test结尾的.py文件
3.在测试文件中查找Test开头的类,以及类中test_开头的方法,还有测试文件中test_开头的方法
2.pytest.ini文件
1.可以修改测试文件、测试函数、测试类、测试类方法的命名规则。
实例:
[pytest] # 更改测试文件命名规则 python_files = HG* # 更改测试类命名规则 python_classes = HG* # 更嗨测试函数命名规则 python_functions = HG*
# HG_func.py
import pytest
def add(a,b):
return a+b
class HGFunc:
def HG_add_by_class(self):
assert add(2,3) == 5
def HG_add_by_func():
assert add(4,6) == 10
1.
case较少可以不使用测试类
import pytest
def test_func1():
print('func1')
def test_func2():
print('func2')
case较多,并且可以分类,建议使用测试类(测试类不可以有构造函数)
import pytest
class TestFunc:
def test_func1(self):
print('func1')
def test_func2(self):
print('func2')
if __name__ == "__main__":
pytest.main(['-s'])
单独执行某个case
pytest -v test_Smoke.py::TestSmoke::test_01_func
输出测试报告
pip install pytest_html
pytest.main(['-s', '--html=report/Report.html'])

浙公网安备 33010602011771号