相关疑难解决方法(0)

如何使用外部夹具跳过pytest?

背景

我运行py.test夹具conftest文件.你可以看到下面的代码(一切正常):

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')
Run Code Online (Sandbox Code Playgroud)

conftest.py

import pytest

@pytest.fixture
def platform_external():
    return "android"
Run Code Online (Sandbox Code Playgroud)

问题

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

不幸的是,我无法在声明中得到(我的外部定义夹具).当我运行下面的代码时,我收到以下异常:.我不知道这是否是py.test错误,因为本地定义的灯具正在工作.platform_externalskipifNameError: name 'platform_external' is not defined

example_test.py的附加组件

@pytest.mark.skipif("platform_external == 'android'")
def test_android(platform_external):
    """This test will fail as …
Run Code Online (Sandbox Code Playgroud)

python decorator pytest python-decorators

18
推荐指数
3
解决办法
8067
查看次数

除非py.test中存在命令行参数,否则默认跳过测试

我有一个长期测试,持续2天,我不想包括在通常的测试运行中.我也不想输入命令行参数,这会在每次通常的测试运行中取消选择它和其他测试.当我真正需要时,我宁愿选择默认取消选择的测试.我尝试将测试重命名test_longrunlongrun并使用该命令

py.test mytests.py::longrun
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

python pytest

10
推荐指数
3
解决办法
2413
查看次数

标签 统计

pytest ×2

python ×2

decorator ×1

python-decorators ×1