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