混合参数化测试和标记

时间:2018-05-21 14:08:16

标签: python python-3.x pytest

好吧,我正在努力解决一些让我头疼的事情。

虽然我的实际代码不同,但这基本上解决了这个问题。假设此示例代码:

import pytest

@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_meh(type):
    assert type == 'integration'

以下是运行该测试的输出。

$pytest -m integration test_meeh.py
========================================================= test session starts =========================================================
platform darwin -- Python 3.6.4, pytest-3.1.2, py-1.5.2, pluggy-0.4.0
rootdir: /Users/yzT/Desktop, inifile:
collected 2 items

test_meeh.py F

============================================================== FAILURES ===============================================================
_________________________________________________________ test_meh[stability] _________________________________________________________

type = 'stability'

    @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_meh(type):
>       assert type == 'integration'
E       AssertionError: assert 'stability' == 'integration'
E         - stability
E         + integration

test_meeh.py:10: AssertionError
========================================================= 1 tests deselected ==========================================================
=============================================== 1 failed, 1 deselected in 0.07 seconds ================================================


$ pytest -m stability test_meeh.py
========================================================= test session starts =========================================================
platform darwin -- Python 3.6.4, pytest-3.1.2, py-1.5.2, pluggy-0.4.0
rootdir: /Users/yzT/Desktop, inifile:
collected 2 items

test_meeh.py .

========================================================= 1 tests deselected ==========================================================
=============================================== 1 passed, 1 deselected in 0.02 seconds ================================================

这里发生了什么?为什么当我使用-m integration时使用stability,当我使用-m stability时,它会使用integration

1 个答案:

答案 0 :(得分:2)

您提供的代码示例看起来有两个问题。

首先,您不应该将单独的参数化测试标记为测试功能。只要您提供的-m选项与函数上的标记装饰器匹配,就会选择测试。

这是一个最小的比较。使用功能上的标记和单独的参数化测试:

# parametrized_tests.py
import pytest
@pytest.mark.parametrize('smiley', [
    pytest.param(':)', marks=[pytest.mark.happy]),
    pytest.param(':(', marks=[pytest.mark.unhappy]),
])
@pytest.mark.happy
@pytest.mark.unhappy
def test_meh(smiley):
    assert smiley == ':)'

您将收集并选择两个测试:

$ pytest -m happy --collect-only parametrized_tests.py
========================================================================================== test session starts ===========================================================================================
platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: [redacted], inifile:
collected 2 items
<Module 'params-individual-tests.py'>
  <Function 'test_smiley[:)]'>
  <Function 'test_smiley[:(]'>

====================================================================================== no tests ran in 0.01 seconds ======================================================================================

但是如果只标记了参数化测试:

# parametrized_tests.py
import pytest

@pytest.mark.parametrize('smiley', [
    pytest.param(':)', marks=[pytest.mark.happy]),
    pytest.param(':(', marks=[pytest.mark.unhappy]),
])
def test_smiley(smiley):
    assert smiley == ':)'

您将收到两个测试,但只有一个被选中,如您所愿:

$ pytest -m happy --collect-only parametrized_tests.py
========================================================================================== test session starts ===========================================================================================
platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: [redacted], inifile:
collected 2 items / 1 deselected
<Module 'params-individual-tests.py'>
  <Function 'test_smiley[:)]'>

====================================================================================== 1 deselected in 0.01 seconds ======================================================================================

其次,pytest.param中似乎存在一个令人费解的错误(或未记录的“功能”),其中使用标记的确切名称作为参数值使得测试未被选中。

来自您(稍加修改)的代码:

# mark_name.py
import pytest

@pytest.mark.parametrize('type_', [
    pytest.param('integration', marks=[pytest.mark.integration]),
    pytest.param('stability', marks=[pytest.mark.stability]),
])
def test_meh(type_):
    assert type_ == 'integration'

如果我尝试仅运行integration测试,则不会选择任何:

$ pytest -m integration mark_name.py
========================================================================================== test session starts ===========================================================================================
platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: [redacted], inifile:
collected 2 items / 2 deselected

但是简单地修改值(在这种情况下我只是将其设为大写)使一切都按预期工作:

# mark_name.py
import pytest

@pytest.mark.parametrize('type_', [
    pytest.param('INTEGRATION', marks=[pytest.mark.integration]),
    pytest.param('STABILITY', marks=[pytest.mark.stability]),
])
def test_meh(type_):
    assert type_ == 'INTEGRATION'

现在我可以正确选择测试:

$ pytest -m integration mark_name.py
========================================================================================== test session starts ===========================================================================================
platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: [redacted], inifile:
collected 2 items / 1 deselected

mark_name.py .                                                                                                                                                                                     [100%]

================================================================================= 1 passed, 1 deselected in 0.01 seconds =================================================================================
$ pytest -m stability mark_name.py
========================================================================================== test session starts ===========================================================================================
platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: [redacted], inifile:
collected 2 items / 1 deselected

mark_name.py F                                                                                                                                                                                     [100%]

================================================================================================ FAILURES ================================================================================================
__________________________________________________________________________________________ test_meh[STABILITY] ___________________________________________________________________________________________

type_ = 'STABILITY'

    @pytest.mark.parametrize('type_', [
        pytest.param('INTEGRATION', marks=[pytest.mark.integration]),
        pytest.param('STABILITY', marks=[pytest.mark.stability]),
    ])
    def test_meh(type_):
>       assert type_ == 'INTEGRATION'
E       AssertionError: assert 'STABILITY' == 'INTEGRATION'
E         - STABILITY
E         + INTEGRATION

mark_name.py:9: AssertionError
================================================================================= 1 failed, 1 deselected in 0.08 seconds =================================================================================

我怀疑这与用作测试ID的字符串值有关,但如果您不想解决这个问题,建议打开GitHub issue

此外,完全不相关,但最好不要将参数定义为type,因为您正在隐藏内置函数typePEP-8建议使用尾随下划线来防止名称冲突。