我有一个严格固定的测试功能,它会因某些灯具输入而失败(应该如此)。我该如何表明这一点?这就是我现在所做的,也许还有更好的方法。我对py.test
很陌生,所以我很感激任何提示。
下一部分是所有输入装置。仅供参考,example_datapackage_path
在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_col
或phenotype_col
时,它会失败。我查看了pytest: Skip and xfail: dealing with tests that can not succeed,但它只讨论skip
和xfail
进行参数化测试,但没有讨论固定装置。
答案 0 :(得分:16)
在您的datapackage
或expression_key
灯具中,您可以按here所述使用pytest.xfail
和pytest.skip
。例如:
@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
,如下例所示:
@pytest.fixture(params=['a', pytest.mark.xfail('b'), 'c'])
def fx1(request):
return request.param
def test_spam(fx1):
assert fx1
如果您希望跳过这些测试,这似乎有效:
@pytest.fixture(
params=['a', pytest.mark.skipif(True, reason='reason')('b'), 'c'])
def fx1(request):
return request.param
def test_spam(fx1):
assert fx1