-
通过 node id 运行测试子集
运行pytest搜索路径下所有用例: pytest
运行单个目录: pytest 目录名称
运行单个测试文件/模块:pytest 文件名
运行单个类:pytest 文件名::类名
运行类中的测试方法:pytest 文件名::类名::函数名
运行单个用例(测试函数):pytest 文件名::函数名 -
通过关键字表达式过滤执行
用测试名划分测试集合: pytest -k 测试名 (and or not ),过滤并运行“测试模块名、测试类名或测试函数名”中包含关键字的测试用例
or:包含关键字中的一个即可pytest --collect-only -k 'est_demo1 or test_demo2 or Demo2'
运行结果:
and:包含所有关键字pytest --collect-only -k 'est_demo2 and test01'
运行结果:
not:不包含关键字pytest --collect-only -k 'not test0'
运行结果:
-
通过标记表达式执行
pytest -v -m run_these_please #运行@pytest.mark.run_these_please 标记的用例
-
通过包执行测试
pytest --pyargs pkg.testdemo
这条命令会自动导入包 pkg.testdemo,并使用该包所在的目录,执行下面的用例。 -
多进程运行cases 可使用pytest-xdist插件实现
当cases量很多时,运行时间也会变的很长,如果想缩短脚本运行的时长,就可以用多进程来运行。
a、安装pytest-xdist:pip install -U pytest-xdist
b、运行命令,指定并发数量:
pytest test_se.py -n NUM # 其中NUM填写并发的进程数。
-
错误重试 rerun cases
在做接口测试时,有事会遇到503或短时的网络波动,导致case运行失败,而这并非是我们期望的结果,此时可以就可以通过重试运行cases的方式来解决。
a、安装pytest-rerunfailures:pip install -U pytest-rerunfailures
b、运行命令,指定重试的次数:
pytest --reruns NUM #NUM填写重试的次数。