如何使用外部装置跳过pytest? [英] How to skip a pytest using an external fixture?

查看:82
本文介绍了如何使用外部装置跳过pytest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 py.test ,其中< 夹具 -per-directory-plugins>竞争文件。您可以在下面看到代码(一切正常):

I am running a py.test with a fixture in a conftest file. You can see the code below(this all works fine):

example_test.py

import pytest

@pytest.fixture
def platform():
    return "ios"

@pytest.mark.skipif("platform == 'ios'")
def test_ios(platform):
    if platform != 'ios':
        raise Exception('not ios')

def test_android_external(platform_external):
    if platform_external != 'android':
        raise Exception('not android')

conftest.py

import pytest

@pytest.fixture
def platform_external():
    return "android"



问题



现在,我希望能够跳过一些不适用于当前测试运行的测试。在我的示例中,我正在针对 iOS Android 运行测试(这仅出于演示目的,可以是任何其他表达式)。

Problem

Now I want to be able to skip some tests that do not apply to my current test-run. In my example I am running tests either for iOS or Android (This is just for demonstration purposes only and could be any other expression).

不幸的是,我无法获得<我的外部定义的灯具 platform_external code> skipif 语句。当我运行下面的代码时,收到以下异常: NameError:未定义名称 platform_external 。我不知道这是否是 py.test 错误,因为本地定义的灯具正在工作。

Unfortunately I cannot get ahold of (my externally defined fixture) platform_external in the skipif statement. When I run the code below I receive the following exception: NameError: name 'platform_external' is not defined. I don't know if this is a py.test bug as locally defined fixtures are working.

添加-on example_test.py

@pytest.mark.skipif("platform_external == 'android'")
def test_android(platform_external):
    """This test will fail as 'platform_external' is not available in the decorator.
    It is only available for the function parameter."""
    if platform_external != 'android':
        raise Exception('not android')

所以我想我将创建自己的装饰器,只是要看到它不会接收灯具作为参数:

So I thought I will just create my own decorator, just to see that it won't receive the fixtures as parameters:

from functools import wraps

def platform_custom_decorator(func):
    @wraps(func)
    def func_wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return func_wrapper

@platform_custom_decorator
def test_android_2(platform_external):
    """This test will also fail as 'platform_external' will not be given to the 
    decorator."""
    if platform_external != 'android':
        raise Exception('not android')



问题



如何在 conftest 文件中定义夹具,并使用它(有条件地)跳过测试

Question

How can I define a fixture in a conftest file and use it to (conditionally) skip a test?

推荐答案

似乎py.test在评估 skipif 。以您的示例为例, test_ios 实际上是成功的,因为它正在比较在以下位置找到的功能 平台模块的命名空间为 ios 字符串,其结果为 False ,因此测试得以执行并成功。如果pytest如您期望的那样插入评估夹具,则应该跳过该测试。

It seems py.test doesn't use the test fixtures when evaluating the expression for skipif. By your example, test_ios is actually successful because it is comparing the function platform found in the module's namespace to the "ios" string, which evaluates to False hence the test is executed and succeeds. If pytest was inserting the fixture for evaluation as you expect, that test should have been skipped.

解决您的问题(虽然不是您的问题)的一种方法是实施用来检查测试标记的夹具,并相应地跳过它们:

A solution to your problem (not to your question though) would be to implement a fixture that inspects marks into the tests, and skips them accordingly:

# conftest.py
import pytest

@pytest.fixture
def platform():
    return "ios"

@pytest.fixture(autouse=True)
def skip_by_platform(request, platform):
    if request.node.get_closest_marker('skip_platform'):
        if request.node.get_closest_marker('skip_platform').args[0] == platform:
            pytest.skip('skipped on this platform: {}'.format(platform))   

密钥关键是 autouse 参数,它将使该固定装置自动包含在所有测试中。然后您的测试可以像这样标记出要跳过的平台:

A key point is the autouse parameter, which would make that fixture to be automatically included by all tests. Then your tests can mark which platforms to skip like this:

@pytest.mark.skip_platform('ios')
def test_ios(platform, request):
    assert 0, 'should be skipped' 

希望有帮助!

这篇关于如何使用外部装置跳过pytest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆