Pytest的运行方式有多种,可以支持单个测试用例执行、分组执行、文件执行、目录执行等,同时还执行多线程执行。
单个用例执行:
pytest 模块名.py::[测试类名]::[测试方法名]
示例:
pytest test_pytestDemo.py::TestDemoAAAA::test_DemoA1 -s
执行测试类里面的所有用例:
pytest 模块名.py::[测试类名]
示例:
pytest test_pytestDemo.py::TestDemoAAAA -s
执行单个文件中的所有用例:
pytest 文件名
示例:
pytest test_pytestDemo.py -s
执行目录下的所有用例:
pytest 文件目录
示例:
pytest testcase/
分组执行:
可以通过pytest.mark 修饰符自定义标识名称进行分组。
pytest [模块名.py] -m 标识名
示例:
pytest test_pytestDemo.py -m L1 -s
Mark自定义名称需要在目录下创建conftest.py 配置文件,pytest会自动去找里面的配置内容。
代码如下:
注意pytest_configure方法名不能变。
def pytest_configure(config):
config.addinivalue_line("markers","L1")
config.addinivalue_line("markers", "L2")
config.addinivalue_line("markers", "L3")
config.addinivalue_line("markers", "L4")
多线程执行:
这个就需要用到第三方插件:pytest-xdist
pip install -U pytest-xdist
使用方法:
pytest 模块名 -n num
num:表示要开启的线程数。
示例:
pytest test_pytestDemo.py -n 3
多线程下好像不支持 -s 的print输出
测试代码:
#pytest test_pytestDemo.py
import pytest
@pytest.mark.L1
class TestDemoAAAA:
def test_DemoA1(self):
print("this is a pytest Demo A11111111111")
assert True
def test_DemoA2(self):
print("this is a pytest Demo A222222222222")
assert True
def test_DemoA3(self):
print("this is a pytest Demo A33333333333")
assert True
@pytest.mark.L2
class TestDemoBBBB:
def test_DemoB1(self):
print("this is a pytest Demo B111111111")
assert True
def test_DemoB2(self):
print("this is a pytest Demo B222222")
assert True
def test_DemoB3(self):
print("this is a pytest Demo B33333")
assert True
@pytest.mark.L3
def test_DemoB3():
print("this is a pytest Demo B33333")
assert True
if __name__ == '__main__':
pytest.main([("test_pytestDemo.py"),("-s")])
-------------------------------------最后---------------------------------
更多软件测试相关内容请关注“软件测试道与术”公众号或扫描下方二维码