如何使用params上的自定义标记在pytest中选择测试子集

mve*_*lay 5 python pytest python-2.7

我想知道如何使用pytest自定义标记选择我的测试子集

一个简单的测试按预期工作:

带有一个标记参数的代码

import pytest

@pytest.mark.parametrize('a', [pytest.mark.my_custom_marker(0), 1])
@pytest.mark.parametrize('b', [0, 1])
def test_increment(a, b):
    pass
Run Code Online (Sandbox Code Playgroud)

如果我只想运行标有'my_custom_marker'的测试

产量

$ pytest test_machin.py -m my_custom_marker --collect-only
platform linux2 -- Python 2.7.12, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /home/mvelay/workspace/sandbox, inifile: 
plugins: hypothesis-3.6.0, html-1.12.0, xdist-1.15.0, timeout-1.0.0
collected 4 items 
<Module 'test_machin.py'>
  <Function 'test_increment[0-0]'>
  <Function 'test_increment[0-1]'>
Run Code Online (Sandbox Code Playgroud)

但是一旦我尝试测试多个标记的参数,我就会面临一个问题

代码带有两个标记的参数

import pytest

@pytest.mark.parametrize('a', [pytest.mark.my_custom_marker(0), 1])
@pytest.mark.parametrize('b', [pytest.mark.my_custom_marker(0), 1])
def test_increment(a, b):
    pass
Run Code Online (Sandbox Code Playgroud)

产量

$ pytest -m my_custom_marker test_machin.py --collect-only
platform linux2 -- Python 2.7.12, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /home/mvelay/workspace/sandbox, inifile: 
plugins: hypothesis-3.6.0, html-1.12.0, xdist-1.15.0, timeout-1.0.0
collected 4 items 
<Module 'test_machin.py'>
  <Function 'test_increment[0-0]'>
  <Function 'test_increment[0-1]'>
  <Function 'test_increment[1-0]'>
Run Code Online (Sandbox Code Playgroud)

我被期待只有[0-0]组合跑了.

有没有办法以优雅的方式做到这一点?

YCF*_*ame 5

您可以使用两种不同的标记,如下所示:

import pytest

@pytest.mark.parametrize('a', [pytest.mark.marker_a(0), 1])
@pytest.mark.parametrize('b', [pytest.mark.marker_b(0), 1])
def test_increment(a, b):
    pass
Run Code Online (Sandbox Code Playgroud)

并指定一个标记表达式:

$ pytest test_machin.py -m "marker_a and marker_b" --collect-only
============= test session starts ===============================
platform darwin -- Python 2.7.10, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /Users/yangchao/Code, inifile:
collected 4 items
<Module 'test_machin.py'>
  <Function 'test_increment[0-0]'>
============= 3 tests deselected =================================
============= 3 deselected in 0.00 seconds =======================
Run Code Online (Sandbox Code Playgroud)