在 pytest 中,如何跳过或 xfail 某些装置? [英] In pytest, how to skip or xfail certain fixtures?

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

问题描述

我有一个高度固定的测试功能,它在某些夹具输入下失败(应该如此).我怎样才能表明这一点?这就是我现在正在做的,也许有更好的方法.我对 py.test 很陌生,所以我很感激任何提示.

I have a heavily-fixtured test function which fails (as it should) with certain fixture inputs. How can I indicate this? This is what I'm doing now, and maybe there's a better way. I'm pretty new to py.test so I'd appreciate any tips.

下一部分是所有输入装置.仅供参考,example_datapackage_pathconf.test

The next part is all the input fixtures. FYI, example_datapackage_path is defined in conf.test

@pytest.fixture(params=[None, 'pooled_col', 'phenotype_col'])
def metadata_key(self, request):
    return request.param

@pytest.fixture(params=[None, 'feature_rename_col'])
def expression_key(self, request):
    return request.param

@pytest.fixture(params=[None, 'feature_rename_col'])
def splicing_key(self, request):
    return request.param

@pytest.fixture
def datapackage(self, example_datapackage_path, metadata_key,
                expression_key, splicing_key):
    with open(example_datapackage_path) as f:
        datapackage = json.load(f)
    datatype_to_key = {'metadata': metadata_key,
                       'expression': expression_key,
                       'splicing': splicing_key}
    for datatype, key in datatype_to_key.iteritems():
        if key is not None:
            resource = name_to_resource(datapackage, datatype)
            if key in resource:
                resource.pop(key)
    return datapackage

@pytest.fixture
def datapackage_dir(self, example_datapackage_path):
    return os.path.dirname(example_datapackage_path)

这是测试本身.

def test_from_datapackage(self, datapackage, datapackage_dir):
    import flotilla
    from flotilla.external import get_resource_from_name

    study = flotilla.Study.from_datapackage(datapackage, datapackage_dir,
                                            load_species_data=False)

    metadata_resource = get_resource_from_name(datapackage, 'metadata')
    expression_resource = get_resource_from_name(datapackage,
                                                 'expression')
    splicing_resource = get_resource_from_name(datapackage, 'splicing')

    phenotype_col = 'phenotype' if 'phenotype_col' \
        not in metadata_resource else metadata_resource['phenotype_col']
    pooled_col = None if 'pooled_col' not in metadata_resource else \
        metadata_resource['pooled_col']
    expression_feature_rename_col = 'gene_name' if \
        'feature_rename_col' not in expression_resource \
        else expression_resource['feature_rename_col']
    splicing_feature_rename_col = 'gene_name' if \
        'feature_rename_col' not in splicing_resource \
        else splicing_resource['feature_rename_col']

    assert study.metadata.phenotype_col == phenotype_col
    assert study.metadata.pooled_col == pooled_col
    assert study.expression.feature_rename_col \
           == expression_feature_rename_col
    assert study.splicing.feature_rename_col == splicing_feature_rename_col

我想做的是在metadata_key中,说当参数是pooled_colphenotype_col时,它会失败.我查看了pytest:Skip and xfail:处理无法成功的测试,但它只谈到了skipxfail 用于参数化测试,但不是夹具.

What I would like to do is in metadata_key, say that when the parameter is pooled_col or phenotype_col, that it will fail. I looked in pytest: Skip and xfail: dealing with tests that can not succeed, but it only talked about skip and xfail for parametrized test, but not fixtures.

推荐答案

在您的 datapackageexpression_key 固定装置中,您可以使用 pytest.xfailpytest.skip这里.例如:

In your datapackage or expression_key fixtures you can use pytest.xfail and pytest.skip as described here. For example:

@pytest.fixture
def datapackage(self, example_datapackage_path, metadata_key,
                expression_key, splicing_key):
    if metadata_key == 'pooled_col':
        pytest.skip('metadata key is "pooled_col"')
    ...

您也可以在夹具参数中使用 pytest.mark.xfail,如下例所示:

You can also use pytest.mark.xfail in fixture parameters as in this example:

@pytest.fixture(params=['a', pytest.mark.xfail('b'), 'c'])
def fx1(request):
    return request.param


def test_spam(fx1):
    assert fx1

如果您想跳过这些测试,这似乎可行:

If you prefer to skip these tests this seems to work:

@pytest.fixture(
    params=['a', pytest.mark.skipif(True, reason='reason')('b'), 'c'])
def fx1(request):
    return request.param


def test_spam(fx1):
    assert fx1

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

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