gpt4 book ai didi

python - 在 pytest 中,如何跳过或 xfail 某些固定装置?

转载 作者:太空狗 更新时间:2023-10-29 20:42:14 25 4
gpt4 key购买 nike

我有一个重固定测试函数,它在某些固定输入时失败(它应该)。我怎样才能指出这一点?这就是我现在正在做的,也许还有更好的方法。我是 py.test 的新手,所以我很感激任何提示。

下一部分是所有输入灯具。仅供引用,example_datapackage_pathconf.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: dealing with tests that can not succeed , 但它只讨论了参数化测试的 skipxfail ,而不是 fixtures。

最佳答案

在您的 datapackageexpression_key fixture 中,您可以使用 pytest.xfailpytest.skip,如 here 所述.例如:

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

您还可以在 fixture 参数中使用 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

关于python - 在 pytest 中,如何跳过或 xfail 某些固定装置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26128620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com