pytest简介(一)
Pytest规则介绍
- 测试文件以test_开头,或_test结尾
- 测试类以Test开头,且不能有__init__方法
- 测试函数以test_开头
Pytest简单demo
class TestCase001:
def sum(self, x):
return x + 3
def test_001(self):
assert self.sum(1) == 3
def test_002(self):
assert self.sum(2) == 5
- 运行方法
- 直接pycharm右键运行
- main方法运行:
pytest.main(['-v','-s','./test_one.py']) #
-v:丰富信息模式,输出更详细的用例执行信息. -s:显示程序中的print/logging输出
pytest.main(['./TeseCase/test_one.py']) #运行指定模块
pytest.main(['./TeseCase/test_one.py::TestOne::testOne_1'])#运行类中的指定用例
pytest.main(['-k',"demo",'./TeseCase/test_one.py'])#匹配test_one.py模块下包含demo的用例 - cmd窗口运行
pytest -s -v ./test_one.py
pytest其他常用命令
- pytest-collect-only 收集可执行得用例
- pytest-last-failed 只重新运行上次运行失败得用例(若没有失败则全跑)
- pytest xxx.py --reruns 2(次数) 失败重跑
- pytest xxx.py -n 2(次数) 多进程运行case
- pytest xxx.py -s --count=2(次数) 循环用例次数pip install pytest-repeat
- pytest xxx.py -s --count=2 -x 循环用例,失败就停止
- pytest xxx.py -m='mark标记名'