Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.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 如何在基类中使用@pytest.mark?_Python_Pytest - Fatal编程技术网

Python 如何在基类中使用@pytest.mark?

Python 如何在基类中使用@pytest.mark?,python,pytest,Python,Pytest,我使用的是py.test 2.2.4,我的测试用例组织如下: 导入pytest 类BaseTests(): def测试_基本_测试(自): 通过 @pytest.mark.linuxonly 类TestLinuxOnlyLocal(基本测试): 通过 @pytest.mark.windowsonly 类TestWindowsOnly(BaseTests): 通过 类别测试地点(基本测试): 通过 这个设置的问题是第一个类的decorator正在泄漏到第二个类中。当我创建conftest.py时

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

导入pytest
类BaseTests():
def测试_基本_测试(自):
通过
@pytest.mark.linuxonly
类TestLinuxOnlyLocal(基本测试):
通过
@pytest.mark.windowsonly
类TestWindowsOnly(BaseTests):
通过
类别测试地点(基本测试):
通过
这个设置的问题是第一个类的decorator正在泄漏到第二个类中。当我创建conftest.py时,如下所示:

导入pytest
导入系统
def pytest_运行测试_设置(项目):
打印“\n%s关键字:%s”%(item.getmodpath(),item.keywords)
跳过消息=无
如果item.keywords中的“windowsonly”而不是sys.platform.startswith('win'):
跳过消息=“跳过:仅Windows测试”
如果item.keywords中的'linuxonly'而不是sys.platform.startswith('linux'):
skip_message=“跳过:仅Linux测试”
如果跳过_消息不是无:
打印跳过消息
pytest.skip(跳过消息)
当我执行此设置时,输出显示标记似乎堆叠起来:

$py.test--capture=no
============================================================测试会话开始===========================================
平台linux2——Python 2.7.3——pytest-2.2.4
收集了3个项目
test_cases.py
TestLinuxOnlyLocal.test_base_test关键字:{'linuxonly':,'test_base_test':True}
.
TestWindowsOnly.test_base_测试关键字:{'linuxonly':,'test_base_test':True,'windowsonly':}
跳过:仅Windows测试
s
Testerywhere.test_base_测试关键字:{'linuxonly':,'test_base_测试]:True,'windowsonly':}
跳过:仅Windows测试
s
================================================================1通过,2在0.01秒内跳过===================================

因此,我想了解这些标记如何可能在子类之间泄漏,以及如何修复/解决这一问题(测试用例将存在于基类中,但子类将建立必要的平台抽象)

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

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

(技术细节:目前这种情况发生在
\u pytest.python.transfer\u markers()
中,但不要依赖于此。)

代替类继承,考虑使用封装特定平台的测试设置。


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

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(...):
         ...

i、 避免任何继承?如果测试中需要固定装置,可以在测试模块中定义它们,并在测试功能(跨平台或特定于平台的功能)中接受它们,请参阅。但是,一定要使用
pytest-2.3.5
,因为有很多改进,特别是在
pytest-2.3
系列中的夹具方面(还有更多改进将随
2.4
一起提供)。

是否有解决此问题的方法?我有一个巨大的现有测试套件,我想将它迁移到使用py.test的测试套件中,但在我们的案例中,它不起作用-我们有一个基本测试,它继承了超过100个其他测试,为不同的面板提供一些通用测试。我将提出一个关于py.test-ish方法的新问题。