13

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

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

4

2 回答 2

13

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

In particular, markers applied to classes (or modules) are transferred to the test functions themselves, and since a non-overridden derived class method is the same object as the base class method, this means that the marker gets applied to the base class method.

(Technical detail: currently this happens in _pytest.python.transfer_markers(), but don't rely on that.)

Instead of class inheritance, consider using fixtures to encapsulate the platform-specific test setup.


A simpler solution might be to compare against the class name, since py.test adds the immediate containing class to item keywords:

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"
于 2013-08-05T12:34:00.543 回答
7

In addition to ecatmur's good answer: You might want to define an pytest.mark.skipif expression like this::

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

and then just decorate the win32-only tests with it::

@win32only
def test_something(...):

Another question is if you could maybe just turn the "BaseTests" into a normal test class::

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

i.e. avoid any inheritance? If you then need fixtures in your tests, you can define them in your test module and accept them in the test functions (cross-platform or platform-specific ones), see pytest fixture docs. Be sure to use pytest-2.3.5, though, because there have been a lot of improvements especially with respect to fixtures in the pytest-2.3 series (and more are to come with 2.4).

于 2013-08-07T08:29:02.897 回答