Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In pytest, how to skip or xfail certain fixtures?

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.

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)

And here's the test itself.

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

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.

like image 717
Olga Botvinnik Avatar asked Sep 30 '14 19:09

Olga Botvinnik


1 Answers

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"')
    ...

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
like image 135
jiffyclub Avatar answered Sep 20 '22 08:09

jiffyclub