2014-09-30 2 views
8

मेरे पास एक भारी-फिक्स्चर टेस्ट फ़ंक्शन है जो कुछ स्थिरता इनपुट के साथ विफल रहता है (जैसा होना चाहिए)। मैं इसे कैसे इंगित कर सकता हूं? यही वह है जो मैं अब कर रहा हूं, और शायद एक बेहतर तरीका है। मैं py.test पर बहुत नया हूं इसलिए मैं किसी भी सुझाव की सराहना करता हूं।पाइस्टेस्ट में, कुछ फिक्स्चर को कैसे छोड़ना या एक्सफेल करना है?

अगला भाग सभी इनपुट फिक्स्चर है। FYI करें, 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_col या phenotype_col है, कि यह असफल हो जायेगी। मैंने pytest: Skip and xfail: dealing with tests that can not succeed में देखा, लेकिन यह केवल parametrized परीक्षण के लिए skip और xfail के बारे में बात की, लेकिन फिक्स्चर नहीं।

उत्तर

15

अपने datapackage या expression_key जुड़नार में आप pytest.xfail और pytest.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"') 
    ... 

तुम भी 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 
+0

ओह, अच्छा! मुझे एहसास नहीं हुआ कि आप 'pytest.fixture' के लिए 'pytest.mark.parameterize' के समान तर्कों का उपयोग कर सकते हैं। धन्यवाद! –

संबंधित मुद्दे