Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用py.test命令行和属性子集指定自定义标记?_Python_Pytest - Fatal编程技术网

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

Python 如何使用py.test命令行和属性子集指定自定义标记?,python,pytest,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.te

我想定义一个自定义标记(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会抛出一个错误。 有什么建议吗

问候


沙拉德

我能够合理地接近这一点:

conftest.py 测试棒 输出
#不指定任何标记 py.test-vs test_bar.py
#指定foo=bar py.test-vs-m“{'my_marker':{'foo':'bar'}”test_bar.py
测试会话开始(平台:linux、Python 3.5.1、pytest 2.8.7、pytest sugar 0.5.1)
cachedir:。缓存
rootdir:/home/sharad/tmp,文件:
插件:cov-2.2.1、timeout-1.0.0、sugar-0.5.1、json-0.4.0、html-1.8.0、spec-1.0.1
取消选择:[]
选定:[,]
测试杆。pytest测试001 0%测试001
测试条。pytest_001✓                                                                                                                                                                                                    50% █████     
测试棒。测试棒003 50%█████     测试单元003
测试杆。pytest测试杆003✓                                                                                                                                                                                                   100% ██████████
==========================================================================================================================================================1个测试被“-m”{'myu-marker\':{'foo\':'bar\}取消选择===================================================================================
结果(0.04s):
2通过
1取消选择

#指定foo=bar,cat=dog py.test-vs-m“{'my_marker':{'foo':'bar','cat':'dog'}}”test_bar.py
测试会话开始(平台:linux、Python 3.5.1、pytest 2.8.7、pytest sugar 0.5.1)
cachedir:。缓存
rootdir:/home/sharad/tmp,文件:
插件:cov-2.2.1、timeout-1.0.0、sugar-0.5.1、json-0.4.0、html-1.8.0、spec-1.0.1
取消选择:[,]
选定:[]
测试杆。测试杆003 0%测试杆003
测试杆。pytest测试杆003✓                                                                                                                                                                                                   100% ██████████
==============================================================================================================================================2个测试由“-m”{my\u marker\:{'foo\:'bar\,'cat\:'dog\}取消选择==========================================================================
结果(0.06s):
1通过
2取消选择

我能够合理地接近这一点:

conftest.py 测试棒 输出
#不指定任何标记 py.test-vs test_bar.py
#指定foo=bar py.test-vs-m“{'my_marker':{'foo':'bar'}”test_bar.py
测试会话开始(平台:linux、Python 3.5.1、pytest 2.8.7、pytest sugar 0.5.1)
cachedir:。缓存
rootdir:/home/sharad/tmp,文件:
插件:cov-2.2.1、timeout-1.0.0、sugar-0.5.1、json-0.4.0、html-1.8.0、spec-1.0.1
取消选择:[]
选定:[,]
测试杆。pytest测试001 0%测试001
测试条。pytest_001✓                                                                                                                                                                                                    50% █████     
测试棒。测试棒003 50%█████     测试单元003
测试杆。pytest测试杆003✓                                                                                                                                                                                                   100% ██████████
==========================================================================================================================================================1个测试被“-m”{'myu-marker\':{'foo\':'bar\}取消选择===================================================================================
结果(0.04s):
2通过
1取消选择

#指定foo=bar,cat=dog py.test-vs-m“{'my_marker':{'foo':'bar','cat':'dog'}}”test_bar.py
测试会话开始(平台:linux、Python 3.5.1、pytest 2.8.7、pytest sugar 0.5.1)
cachedir:。缓存
rootdir:/home/sharad/tmp,文件:
插件:cov-2.2.1、timeout-1.0.0、sugar-0.5.1、json-0.4.0、html-1.8.0、spec-1.0.1
取消选择:[,]
选定:[]
测试杆。测试杆003 0%测试杆003
测试杆。pytest测试杆003✓
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))
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')
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
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
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