如何使用带有属性子集的py.test命令行指定自定义标记?

时间:2016-06-02 05:06:31

标签: python pytest

我想定义一个自定义标记(my_marker包含一堆属性。在触发测试时,如何选择/指定一组具有特定属性的测试?

例如,请考虑以下事项:

import pytest

@pytest.mark.my_marker(foo='bar')
def test_001():
    pass 

def test_002():
    pass

@pytest.mark.my_marker(foo='bar', cat='dog')
def test_003():
    pass

现在,py.test -m my_marker将选择test_001和test_003。我想指定类似的内容:

py.test -s -m "my_marker(cat='dog')"

这应该只选择test_003。但py.test会引发错误。 有什么建议吗?

此致

沙拉德帕

1 个答案:

答案 0 :(得分:3)

我能够合理地接近这个:

conftest.py

import ast
import pytest

def pytest_collection_modifyitems(session, config, items):
    mymarkerFilter = config.option.markexpr
    if not mymarkerFilter:
        return
    mymarkerFilter = ast.literal_eval(mymarkerFilter).get('my_marker')
    if not mymarkerFilter:
        return

    selected = []
    deselected = []
    for item in items:
        testMymarker = item.get_marker('my_marker')
        if not testMymarker:
            deselected.append(item)
            continue
        found = False
        for filterKey in mymarkerFilter:
            if not testMymarker.kwargs.get(filterKey):
                deselected.append(item)
                found = True
                break
            if mymarkerFilter[filterKey] != testMymarker.kwargs.get(filterKey):
                deselected.append(item)
                found = True
                break
        if not found:
            selected.append(item)
    if deselected:
        config.hook.pytest_deselected(items=deselected)
        items[:] = selected
    print('Deselected: {}'.format(deselected))
    print('Selected: {}'.format(items))

test_bar.py

import pytest

@pytest.mark.my_marker(foo='bar')
def test_001():
    print('test_001')

def test_002():
    print('test_002')

@pytest.mark.my_marker(foo='bar', cat='dog')
def test_003():
    print('test_003')

输出

#未指定任何标记

py.test -vs test_bar.py

Test session starts (platform: linux, Python 3.5.1, pytest 2.8.7, pytest-sugar 0.5.1)
cachedir: .cache
rootdir: /home/sharad/tmp, inifile: 
plugins: cov-2.2.1, timeout-1.0.0, sugar-0.5.1, json-0.4.0, html-1.8.0, spec-1.0.1

 test_bar.pytest_001                                                                                                                                                                                                       0%           test_001
 test_bar.pytest_001 ✓                                                                                                                                                                                                    33% ███▍      
 test_bar.pytest_002                                                                                                                                                                                                      33% ███▍      test_002
 test_bar.pytest_002 ✓                                                                                                                                                                                                    67% ██████▋   
 test_bar.pytest_003                                                                                                                                                                                                      67% ██████▋   test_003
 test_bar.pytest_003 ✓                                                                                                                                                                                                   100% ██████████

Results (0.06s):
       3 passed

#指定foo = bar

py.test -vs -m" {' my_marker':{' foo':' bar'}}" test_bar.py

Test session starts (platform: linux, Python 3.5.1, pytest 2.8.7, pytest-sugar 0.5.1)
cachedir: .cache
rootdir: /home/sharad/tmp, inifile: 
plugins: cov-2.2.1, timeout-1.0.0, sugar-0.5.1, json-0.4.0, html-1.8.0, spec-1.0.1
Deselected: [<Function 'test_002'>]
Selected: [<Function 'test_001'>, <Function 'test_003'>]

 test_bar.pytest_001                                                                                                                                                                                                       0%           test_001
 test_bar.pytest_001 ✓                                                                                                                                                                                                    50% █████     
 test_bar.pytest_003                                                                                                                                                                                                      50% █████     test_003
 test_bar.pytest_003 ✓                                                                                                                                                                                                   100% ██████████
=================================================================================== 1 tests deselected by '-m "{\'my_marker\': {\'foo\': \'bar\'}}"' ===================================================================================

Results (0.04s):
       2 passed
       1 deselected

#指定foo = bar,cat = dog

py.test -vs -m&#34; {&#39; my_marker&#39;:{&#39; foo&#39;:&#39; bar&#39;,&#39; cat&#39 ;:&#39; dog&#39;}}&#34; test_bar.py

Test session starts (platform: linux, Python 3.5.1, pytest 2.8.7, pytest-sugar 0.5.1)
cachedir: .cache
rootdir: /home/sharad/tmp, inifile: 
plugins: cov-2.2.1, timeout-1.0.0, sugar-0.5.1, json-0.4.0, html-1.8.0, spec-1.0.1
Deselected: [<Function 'test_001'>, <Function 'test_002'>]
Selected: [<Function 'test_003'>]

 test_bar.pytest_003                                                                                                                                                                                                       0%           test_003
 test_bar.pytest_003 ✓                                                                                                                                                                                                   100% ██████████
========================================================================== 2 tests deselected by '-m "{\'my_marker\': {\'foo\': \'bar\', \'cat\': \'dog\'}}"' ==========================================================================

Results (0.06s):
       1 passed
       2 deselected