如何使用@ pytest.mark与基类?

amo*_*ej1 11 python pytest

我正在使用py.test 2.2.4,我的测试用例组织如下:

import pytest 

class BaseTests():
    def test_base_test(self):
        pass

@pytest.mark.linuxonly
class TestLinuxOnlyLocal(BaseTests):
    pass

@pytest.mark.windowsonly
class TestWindowsOnly(BaseTests):
    pass

class TestEverywhere(BaseTests):
    pass
Run Code Online (Sandbox Code Playgroud)

这种设置的问题是第一类的装饰器泄漏到第二类.当我创建conftest.py时如下:

import pytest
import sys 

def pytest_runtest_setup(item):
    print "\n %s keywords: %s" % (item.getmodpath(), item.keywords)
    skip_message = None
    if 'windowsonly' in item.keywords and not sys.platform.startswith('win'):
        skip_message =  "Skipped: Windows only test"

    if 'linuxonly' in item.keywords and not sys.platform.startswith('linux'):
        skip_message = "Skipped: Linux only test"

    if skip_message is not None:
        print skip_message
        pytest.skip(skip_message)
Run Code Online (Sandbox Code Playgroud)

当我执行此设置时,输出显示标记似乎堆叠起来:

$ py.test  --capture=no
========================================== test session starts ===========================================
platform linux2 -- Python 2.7.3 -- pytest-2.2.4
collected 3 items 

test_cases.py 
 TestLinuxOnlyLocal.test_base_test keywords: {'linuxonly': <MarkInfo 'linuxonly' args=() kwargs={}>, 'test_base_test': True}
.
 TestWindowsOnly.test_base_test keywords: {'linuxonly': <MarkInfo 'linuxonly' args=() kwargs={}>, 'test_base_test': True, 'windowsonly': <MarkInfo 'windowsonly' args=() kwargs={}>}
Skipped: Windows only test
s
 TestEverywhere.test_base_test keywords: {'linuxonly': <MarkInfo 'linuxonly' args=() kwargs={}>, 'test_base_test': True, 'windowsonly': <MarkInfo 'windowsonly' args=() kwargs={}>}
Skipped: Windows only test
s

================================== 1 passed, 2 skipped in 0.01 seconds ===================================
Run Code Online (Sandbox Code Playgroud)

所以我想了解这些标记是如何在子类之间泄漏的,以及如何解决这个问题(测试用例将存在于基类中,但子类将建立必要的平台抽象) .

eca*_*mur 11

与其他Python测试框架(例如unittest)相比,pytest采用了​​更多面向函数的测试方法,因此类主要被视为组织测试的一种方式.

特别是,应用于类(或模块)的标记将转移到测试函数本身,并且由于未重写的派生类方法与基类方法是同一个对象,这意味着标记应用于基类方法.

(技术细节:目前发生这种情况_pytest.python.transfer_markers(),但不依赖于此.)

考虑使用fixture来封装特定于平台的测试设置,而不是类继承.


一个更简单的解决方案可能是与类名进行比较,因为py.test将直接包含类添加到项关键字:

if 'TestWindowsOnly' in item.keywords and not sys.platform.startswith('win'):
    skip_message =  "Skipped: Windows only test"

if 'TestLinuxOnly' in item.keywords and not sys.platform.startswith('linux'):
    skip_message = "Skipped: Linux only test"
Run Code Online (Sandbox Code Playgroud)


hpk*_*k42 5

除了ecatmur的好答案:你可能想要定义一个pytest.mark.skipif这样的表达式::

win32only = pytest.mark.skipif("sys.platform != 'win32'")
Run Code Online (Sandbox Code Playgroud)

然后用它装饰win32-only测试::

@win32only
def test_something(...):
Run Code Online (Sandbox Code Playgroud)

另一个问题是,如果你可以将"BaseTests"变成普通的测试类::

class TestCrossPlatform:
     def test_base_tests(...):
         ...
Run Code Online (Sandbox Code Playgroud)

即避免任何继承?如果您在测试中需要灯具,则可以在测试模块中定义它们并在测试功能(跨平台或平台特定的)中接受它们,请参阅pytest fixture文档.pytest-2.3.5但是一定要使用,因为有很多改进,特别是在pytest-2.3系列中的灯具方面(还有更多2.4).