pip3 install pytest-xdist
在远程运行测试之前,pytest有效地将您的程序源代码“rsyncs”到远程位置。报告所有测试结果并显示给您的本地终端。您可以指定不同的Python版本和解释器。
多cpu并行执行用例,直接加个-n参数即可,后面num参数就是并行数量,比如num设置为3
pytest -n 3
运行以下代码,项目结构如下
web_xdist是项目工程名称
│ conftest.py
│ __init__.py
│
├─baidu
│ │ conftest.py
│ │ test_1_baidu.py
│ │ test_2.py
│ │ __init__.py
│
├─blog
│ │ conftest.py
│ │ test_2_blog.py
│ │ __init__.py
具体代码
# web_conf_py/conftest.py
import pytest
@pytest.fixture(scope="session")
def start():
print("\n打开首页")
return "jkc"
# web_xdist/baidu/conftest.py
import pytest
@pytest.fixture(scope="session")
def open_baidu():
print("打开百度页面_session")
# web_xdist/baidu/test_1_baidu.py
import pytest
import time
def test_01(start, open_baidu):
print("测试用例test_01")
time.sleep(1)
assert start == "jkc"
def test_02(start, open_baidu):
print("测试用例test_02")
time.sleep(1)
assert start == "jkc"
if __name__ == "__main__":
pytest.main(["-s", "test_1_baidu.py"])
# web_xdist/baidu/test_2.py
import pytest
import time
def test_06(start, open_baidu):
print("测试用例test_01")
time.sleep(1)
assert start == "jkc"
def test_07(start, open_baidu):
print("测试用例test_02")
time.sleep(1)
assert start == "jkc"
if __name__ == "__main__":
pytest.main(["-s", "test_2.py"])
# web_xdist/blog/conftest.py
import pytest
@pytest.fixture(scope="function")
def open_blog():
print("打开blog页面_function")
# web_xdist/blog/test_2_blog.py
import pytest
import time
def test_03(start, open_blog):
print("测试用例test_03")
time.sleep(1)
assert start == "jkc"
def test_04(start, open_blog):
print("测试用例test_04")
time.sleep(1)
assert start == "jkc"
def test_05(start, open_blog):
'''跨模块调用baidu模块下的conftest'''
print("测试用例test_05,跨模块调用baidu")
time.sleep(1)
assert start == "jkc"
if __name__ == "__main__":
pytest.main(["-s", "test_2_blog.py"])
collecting ...
web_xdist/baidu/test_1_baidu.py ✓✓ 29% ██▉
web_xdist/baidu/test_2.py ✓✓ 57% █████▊
web_xdist/blog/test_2_blog.py ✓✓✓ 100% ██████████
Results (7.09s):
7 passed
pytest -n auto
测试结果
gw0 [7] / gw1 [7] / gw2 [7] / gw3 [7] / gw4 [7] / gw5 [7] / gw6 [7] / gw7 [7] / gw8 [7] / gw9 [7] / gw10 [7] / gw11 [7]
web_xdist/baidu/test_2.py ✓✓ 100% ██████████
web_xdist/baidu/test_1_baidu.py ✓✓ 71% ███████▎
web_xdist/blog/test_2_blog.py ✓✓✓ 86% ████████▋
Results (2.77s):
7 passed
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/166345.html原文链接:https://javaforall.cn