栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在不同的进程中运行py.test测试

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在不同的进程中运行py.test测试

如评论中所建议,使用

pytest-xdist
将是解决方案。该插件设计用于并行或分布式执行测试(甚至可以执行多平台),但是非常适合满足您在单独的过程中运行每个测试的请求-
您可以使用
--forked
参数来实现。

免责声明

--forked
参数在Windows上不起作用,因为Windows不支持fork-exec模型,并且不提供任何替代
fork()

快速演示

让我们定义一个夹具,它将在运行每个测试之前尝试打开急切的执行:

from tensorflow.contrib.eager.python import tfeimport pytest@pytest.fixture(scope='function', autouse=True)def eager(request):    tfe.enable_eager_execution()

很显然,该夹具将无法通过所有测试,但第一个测试将失败,因为急切的执行只能进行一次。通过一些虚拟测试:

def test_spam():    assert Truedef test_eggs():    assert Truedef test_bacon():    assert True

普通运行

pytest
失败,按预期进行:

$ pytest -v============================== test session starts ================================platform darwin -- Python 3.6.3, pytest-3.3.1, py-1.5.2, pluggy-0.6.0 -- /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6cachedir: .cacherootdir: /Users/hoefling/projects/private/stackoverflow/so-48234032, inifile:plugins: forked-0.2, mock-1.6.3, hypothesis-3.44.4collected 3 itemstest_spam.py::test_spam PASSED    [ 33%]test_spam.py::test_eggs ERROR     [ 66%]test_spam.py::test_bacon ERROR    [100%]...E       ValueError: Do not call tfe.enable_eager_execution more than once in thesame process. Note eager-mode methods such as tfe.run() also call tfe.enable_eager_execution....

现在安装

pytest-xdist

$ pip install pytest-xdist

并重新运行测试:

$ pytest -v --forked============================== test session starts ================================platform darwin -- Python 3.6.3, pytest-3.3.1, py-1.5.2, pluggy-0.6.0 -- /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6cachedir: .cacherootdir: /Users/hoefling/projects/private/stackoverflow/so-48234032, inifile:plugins: forked-0.2, xdist-1.22.0, mock-1.6.3, hypothesis-3.44.4collected 3 itemstest_spam.py::test_spam PASSED    [ 33%]test_spam.py::test_eggs PASSED    [ 66%]test_spam.py::test_bacon PASSED   [100%]============================= 3 passed in 6.09 seconds ============================

测试仍然按顺序运行,但是每个测试都在一个自己的子流程中,因此它们都不会失败。

现在您可以开始尝试并行执行,例如

$ pytest -v --forked --numprocesses=auto

等等。有关更多信息和更多用法示例,请参阅插件文档。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/429249.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号