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

如何在py.test运行中多次重复每个测试?

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

如何在py.test运行中多次重复每个测试?

为了多次运行每个测试,我们将在生成测试时以编程方式对每个测试进行参数化。

首先,让我们添加解析器选项(在您的conftest.py中添加以下内容):

def pytest_addoption(parser):    parser.addoption('--repeat', action='store',        help='Number of times to repeat each test')

现在,我们添加一个“ pytest_generate_tests”挂钩。这就是魔术发生的地方。

def pytest_generate_tests(metafunc):    if metafunc.config.option.repeat is not None:        count = int(metafunc.config.option.repeat)        # We're going to duplicate these tests by parametrizing them,        # which requires that each test has a fixture to accept the parameter.        # We can add a new fixture like so:        metafunc.fixturenames.append('tmp_ct')        # Now we parametrize. This is what happens when we do e.g.,        # @pytest.mark.parametrize('tmp_ct', range(count))        # def test_foo(): pass        metafunc.parametrize('tmp_ct', range(count))

在没有重复标志的情况下运行:

(env) $ py.test test.py -vv============================= test session starts ==============================platform darwin -- Python 2.7.5 -- py-1.4.20 -- pytest-2.5.2 -- env/bin/pythoncollected 2 itemstest.py:4: test_1 PASSEDtest.py:8: test_2 PASSED=========================== 2 passed in 0.01 seconds ===========================

使用重复标志运行:

(env) $ py.test test.py -vv --repeat 3============================= test session starts ==============================platform darwin -- Python 2.7.5 -- py-1.4.20 -- pytest-2.5.2 -- env/bin/pythoncollected 6 itemstest.py:4: test_1[0] PASSEDtest.py:4: test_1[1] PASSEDtest.py:4: test_1[2] PASSEDtest.py:8: test_2[0] PASSEDtest.py:8: test_2[1] PASSEDtest.py:8: test_2[2] PASSED=========================== 6 passed in 0.01 seconds ===========================

进一步阅读:

  • https://pytest.org/latest/plugins.html#well-specified-hooks
  • https://pytest.org/latest/example/parametrize.html


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

版权所有 (c)2021-2022 wk8.com.cn

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