pytest作为自动化测试中常用的库,学会一些常用快捷名令更能结合用例去使用
import pytest
class Test_l():
def test_one(self):
print('hello')
assert 1==1
def test_two(self):
assert 1==3
def test_three(self):
assert 5==5
if __name__ == '__main__':
'''
—-ff 先运行失败的用例,在运行所有用例
-n auto 多线程运行
-reruns 失败重试次数
--lf仅运行上次失败的测试用例
-x 遇到失败就停止运行
-maxfail=x x等于失败的用例数,超过这个数量就停止运行
-k xx 模糊匹配def中包含xx的方法
--collect-only 收集可以运行的测试用例
-m 执行标记用例
-vs 一般放在一起使用,控制台打印测试用例运行情况更详细的输出
'''
pytest.main(['-vs','test_run.py','--lf']) #--lf仅运行失败的用例,在运行所有的用例
pytest.main(['-vs','test_run.py','--ff']) #--ff先运行失败的用例,在执行其他用例
pytest.main(['-vs', 'test_run.py', '-k','one']) # -k xx 模糊匹配def中包含xx的方法
pytest.main(['-vs', 'test_run.py', '--collect-only']) # --collect-only 收集可以运行的·测试用例
pytest.ini文件 通俗将就是将用例执行的代码,写在pytest.ini文件中使代码看着更加简洁
pytest.ini文件一般放在项目的根目录下,pytest.mian()运行时会自动查找pytest.ini文件的数据,然后去运行文件中配置中的数据
1、look look 文件
#文件必须以[pytest]开头
[pytest]
#测试用例存放的路径
testpaths=·/testcase 测试文件路径
#测试用例对应的py文件名,'test_demo_*代表的意思:所有包含test_demo_的py文件'
python_files = test_demo_* 文件
#测试用例中对应的测试类的名称
python_classes = Test* 类
#测试用例中对应测试类下的测试方法
python_functions = test_one 方法
#addopts 的意思 = pytest.main(['-vs','-reruns=2','xxxx.py'])里的'-vs','-reruns=2'
addopts=-vs -rerun=2
2、如何调用pytest.ini文件
import pytest
class Test_l():
def test_one(self):
print('hello')
assert 1==1
def test_two(self):
assert 1==3
def test_three(self):
assert 5==5
if __name__ == '__main__':
pytest.main()