繁体   English   中英

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

[英]How to use @pytest.mark with base classes?

我正在使用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

这种设置的问题是第一类的装饰器泄漏到第二类。 当我创建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)

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

$ 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 ===================================

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

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

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

(技术细节:目前这种情况发生在_pytest.python.transfer_markers() ,但不依赖_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"

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

win32only = pytest.mark.skipif("sys.platform != 'win32'")

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

@win32only
def test_something(...):

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

class TestCrossPlatform:
     def test_base_tests(...):
         ...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM