Loading

Python 分布式执行测试用例

pytest-parallel 库

安装

pipenv install pytest-parallel

使用

--workers auto 多进程

缺陷

暂时不知道如何使fixture只执行一次

pytest-xdist 库

安装

pipenv install pytest-xdist

使用

-n auto
--dist=loadscope 按class分配进程
--dist=loadfile 按文件名分配进程

问题

一、如何使session级别fixture只执行一次,其他用例在测试时调用文件
添加 FileLock 库,安装: pipenv install filelock
@pytest.fixture(scope="session")
def testfixture1():
with FileLock("session.lock"):
login(account,password)

二、pytest-html 的pytest_runtest_makereport 和 xdist存在冲突,使用report.description会报assert crashitm错误!
解决方案:
1.删除report.description,替换成显示在extra里
2.此时执行代码,还是会有部分用例在用例执行失败时报错 assert crashitm,查看html官网,添加 (tryfirst=True, hookwrapper=True)

@pytest.mark.hookwrapper(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item):
pytest_html = item.config.pluginmanager.getplugin("html")
outcome = yield
report = outcome.get_result()
description = description_html(item.function.__doc__)
extra = getattr(report, "extra", [])
desc_html = "<p>Description:%s;</p>" % (description)
extra.append(pytest_html.extras.html(desc_html))
if report.when == "call" or report.when == "setup":
xfail = hasattr(report, "wasxfail")
if (report.skipped and xfail) or (report.failed and not xfail):
html = (
"<div><p>Request URL:%s;</p>"
"<p>Response:%s</p>"
"<p>Request header:%s</p></div>"
% (resp.request.url, resp.json(), resp.request.headers)
)
extra.append(pytest_html.extras.html(html))
report.extra = extra

posted @ 2020-07-09 10:11  Tester-Dolores  阅读(203)  评论(0编辑  收藏  举报