[pytest]
addopts = -vs
testpaths = test_cases
python_files = test_*.py
python_classes = Test*
python_functions = test_*
markers =
smoke: Run smoke
login: Run login
需要注意的点:
1.第一行[pytest]不可缺少
2.addopts是传控制参数
3.testpaths是控制运行路径,比如当下是识别test_cases文件夹中所有用例
4.python_files、python_classes、python_funcitons是对识别文件、类、方法格式
5.markers是对于用例进行分类,比如可以在用例方法上添加装饰器,@pytest.mark.smoke()
import pytest class TestRun: @pytest.mark.smoke() def test_name1(self): print("11111") raise Exception(print("错误")) def test_name2(self): print("2222")
代码块中,方法test_name1 做标记,配置文件中在addopts中加参数-m smoke,执行,这种情况是只执行标记smoke的用例
[pytest]
addopts = -vs -m smoke
testpaths = test_cases
python_files = test_*.py
python_classes = Test*
python_functions = test_*
markers =
smoke: Run smoke
login: Run login
执行结果如下:
_____________________________ TestRun.test_name1 ______________________________
self = <test_cases.test_run.TestRun object at 0x0000012DE3006E20>
@pytest.mark.smoke()
def test_name1(self):
print("11111")
> raise Exception(print("错误"))
E Exception: None
test_cases\test_run.py:8: Exception
=========================== short test summary info ===========================
FAILED test_cases/test_run.py::TestRun::test_name1 - Exception: None
======================= 1 failed, 1 deselected in 0.06s =======================
Process finished with exit code 0
第二个用例并没有被执行。
6.pytest.ini不要写中文,会有转码的错误,如果出现这样的问题,可以使用notepad打开文件,将编码格式修改一下,修改为ANSI编码格式。当然建议最好不要使用中文