使用固定装置跳过 pytest 中的测试

NFS*_*edy 4 testing pytest python-3.x python-3.8

所以我有一个巨大的对象来保存在夹具内启动的信息。我需要使用这些信息来运行我的测试,这里开始棘手的部分。如果我在测试用例中使用的对象内没有属性,我必须跳过它。

生成对象的装置在测试运行之前启动一次(通常)。在测试之前我需要一个易于使用的装饰器/固定装置/任何东西来检查对象是否具有对象内部所需的内容。

例子:

@pytest.fixture(scope="package")
def info(request):
    print("Setting up...")
    obj = Creator()
    obj.setup()
    obj.prepare() if hasattr(obj, "prepare") else ""
    def teardown():
        obj.teardown() if hasattr(obj, "teardown") else ""
    request.addfinalizer(teardown)
    return obj.call()

...

@has_attr("some_attr")
def test_sometest(info):
    assert info.some_attr == 42
Run Code Online (Sandbox Code Playgroud)

MrB*_*men 6

我可以想到多种可能性来实现这一目标,但没有一种看起来像您的示例那么干净。

最简单的方法就是在测试中进行跳过:

def test_something(info):
    if not hasattr(info, "some_attr"):
        pytest.skip("Missing attribute 'some_attr'")
    assert info.some_attr == 42
Run Code Online (Sandbox Code Playgroud)

可能不是您想要的,但如果您没有进行很多测试,这可能是有意义的。如果您只想检查几个不同的属性,您可以为这些属性制作特定的装置:

@pytest.fixture
def info_with_some_attr(info):
    if not hasattr(info, "some_attr"):
        pytest.skip("Missing attribute 'some_attr'")
    yield info

def test_something(info_with_some_attr):
    assert info_with_some_attr.some_attr == 42
Run Code Online (Sandbox Code Playgroud)

如果您有更多属性,您可以使用属性名称来参数化夹具:

@pytest.fixture
def info_with_attr(request, info):
    if hasattr(request, "param"):
        for attr in request.param:
            if not hasattr(info, attr):
                pytest.skip(f"Missing attribute '{attr}'")
    yield info


@pytest.mark.parametrize("info_with_attr", [("some_attr", "another_attr")], indirect=True)
def test_something(info_with_attr):
    assert info_with_attr.some_attr == 42
Run Code Online (Sandbox Code Playgroud)

这正是你想要的,尽管看起来有点尴尬。

编辑:更新了最后一个示例以使用元组而不是单个字符串,如评论中所述。