我有一个固定夹具的测试功能,由于某些夹具输入而失败(应该如此)。我该如何指出呢?这就是我现在正在做的,也许还有更好的方法。我是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
,而不是夹具。 最佳答案
在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