关键字:-n
pytest运行参数 -n用于并发运行多个测试用例,该并发运行是进程级别的并发测试,使用前,需安装pytest-xdist插件。其中 -n=auto表示使用服务器所有的CPU来运行测试用例,举例如下:
import time
import pytest
class TestPytestFeatures(object):
def test_case_login(self):
time.sleep(1)
num = 1
assert num == 1
def test_case_logout(self):
time.sleep(1)
tmp = ["a", "b"]
assert isinstance(tmp, list)
def test_case_shopping(self):
time.sleep(1)
tmp = ["a", "b"]
assert isinstance(tmp, list)
def test_case_walk(self):
time.sleep(1)
tmp = ["a", "b"]
assert isinstance(tmp, list)
if __name__ == "__main__":
pytest.main(["-n=2", "test_case_pytest_features.py"])
"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:\icp_capp\help
plugins: allure-pytest-2.9.43, forked-1.3.0, xdist-2.4.0
gw0 I / gw1 I
gw0 [4] / gw1 [4]
.... [100%]
============================== warnings summary ===============================
test_case_pytest_features.py:37
E:\icp_capp\help\test_case_pytest_features.py:37: DeprecationWarning: invalid escape sequence \i
-- Docs: https://docs.pytest.org/en/stable/warnings.html
======================== 4 passed, 1 warning in 2.51s =========================
Process finished with exit code 0
"""