若是你还想从头学起Pytest,能够看看这个系列的文章哦!html
https://www.cnblogs.com/poloyy/category/1690628.htmlpython
pip3 install pytest-xdist -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
这是运行代码的包结构web
14xdist是项目文件夹名称 │ conftest.py │ test_1.py │ __init__.py │ ├─test_51job │ │ conftest.py │ │ test_case1.py │ │ __init__.py │ ├─test_toutiao │ │ test_case2.py │ ├─test_weibo │ │ conftest.py │ │ test_case3.py │ │ __init__.py │
# 外层conftest.py @pytest.fixture(scope="session") def login(): print("====登陆功能,返回帐号,token===") name = "testyy" token = "npoi213bn4" yield name, token print("====退出登陆!!!====")
import pytest @pytest.mark.parametrize("n", list(range(5))) def test_get_info(login, n): sleep(1) name, token = login print("***基础用例:获取用户我的信息***", n) print(f"用户名:{name}, token:{token}")
import pytest @pytest.fixture(scope="module") def open_51(login): name, token = login print(f"###用户 {name} 打开51job网站###")
from time import sleep import pytest @pytest.mark.parametrize("n", list(range(5))) def test_case2_01(open_51, n): sleep(1) print("51job,列出全部职位用例", n) @pytest.mark.parametrize("n", list(range(5))) def test_case2_02(open_51, n): sleep(1) print("51job,找出全部python岗位", n)
from time import sleep import pytest @pytest.mark.parametrize("n", list(range(5))) def test_no_fixture(login, n): sleep(1) print("==没有__init__测试用例,我进入头条了==", login)
import pytest @pytest.fixture(scope="function") def open_weibo(login): name, token = login print(f"&&& 用户 {name} 返回微博首页 &&&")
from time import sleep import pytest @pytest.mark.parametrize("n", list(range(5))) class TestWeibo: def test_case1_01(self, open_weibo, n): sleep(1) print("查看微博热搜", n) def test_case1_02(self, open_weibo, n): sleep(1) print("查看微博范冰冰", n)
pytest -s
能够看到,执行一条用例大概1s(由于每一个用例都加了 sleep(1) ),一共30条用例,总共运行30s;那么若是有1000条用例,执行时间就真的是1000ssession
pytest -s -n auto
pytest -s -n 2
pytest -s -n auto --html=report.html --self-contained-html
pytest-xdist默认是无序执行的,能够经过 --dist 参数来控制顺序并发
--dist=loadscope 分布式
--dist=loadfile ide
按照同一个文件名来分组,而后将每一个测试组发给能够执行的worker,确保同一个组的测试用例在同一个进程中执行函数
pytest-xdist是让每一个worker进程执行属于本身的测试用例集下的全部测试用例oop
这意味着在不一样进程中,不一样的测试用例可能会调用同一个scope范围级别较高(例如session)的fixture,该fixture则会被执行屡次,这不符合scope=session的预期测试
虽然pytest-xdist没有内置的支持来确保会话范围的夹具仅执行一次,可是能够经过使用锁定文件进行进程间通讯来实现。
import pytest from filelock import FileLock @pytest.fixture(scope="session") def login(): print("====登陆功能,返回帐号,token===") with FileLock("session.lock"): name = "testyy" token = "npoi213bn4" # web ui自动化 # 声明一个driver,再返回 # 接口自动化 # 发起一个登陆请求,将token返回均可以这样写 yield name, token print("====退出登陆!!!====")