首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >防止仅仅因为参数不同而“重复”测试代码。

防止仅仅因为参数不同而“重复”测试代码。
EN

Stack Overflow用户
提问于 2018-05-21 12:16:50
回答 1查看 360关注 0票数 1

在我的测试套件中,我有不同的集成测试和稳定性测试。

例如,

代码语言:javascript
复制
@pytest.mark.integration
def test_integration_total_devices(settings, total_devices):
    assert total_devices == settings['integration']['nodes']['total']

@pytest.mark.stability
def test_stability_total_devices(settings, total_devices):
    assert total_devices == settings['stability']['nodes']['total']

正如您所注意到的,它是完全相同的代码,只是从配置中读取一个不同的参数。

如何防止这种重复代码的情况?设置的值是不同的,所以我不能只是:

代码语言:javascript
复制
@pytest.mark.integration
@pytest.mark.stability
def test_integration_total_devices(settings, total_devices):
    assert total_devices == settings['nodes']['total']

我忘了提一下(为了提醒我,谢谢@dzejdzej ),看来热测试参数化不起作用。当我想运行两个“标记”时,它可以工作,但是标记的目的是能够独立地运行其中一个标记的测试,例如pytest -m integration。然而,就我测试而言,每当我设置参数化时,它都会同时运行。

代码语言:javascript
复制
@pytest.mark.parametrize('type', (
    pytest.param('stability', marks=pytest.mark.stability),
    pytest.param('integration', marks=pytest.mark.integration),
))
@pytest.mark.integration
@pytest.mark.stability
def test_total_devices(settings, total_devices, type):
    assert total_devices == settings[type]['nodes']['total']
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-21 12:44:11

请看一看pytest参数化https://docs.pytest.org/en/latest/parametrize.html

应该可以这样做:

代码语言:javascript
复制
@pytest.mark.parametrize('area,total_devices', (
    pytest.param('stability', 10, marks=pytest.mark.stability),
    pytest.param('integration', 15, marks=pytest.mark.integration),
))
def test_integration_total_devices(area, total_devices):
    assert total_devices == settings.get(area)['nodes']['total']
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50448724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档