2016-11-26 8 views
1

pytestを使って、私は階層的シナリオのようなツリーをテストしようとしています。 は、一例として、文書構造を使用することができます。文書は複数の章を含んpytestは木のようなデータでネストされたパラメータ化

Document --- Chapter --- Paragraph 
     1 n  1 n 

。章には複数の段落が含まれています。

新しいドキュメントのテストを開始するときに、セットアップコードを実行する必要があります。新しいチャプターが開始されると、他のセットアップコードを実行する必要があります。パラグラフと同じです。

for doc in documents: 
    setup_doc(doc) 
    for chapter in doc.chapters: 
     setup_chapter(chapter) 
     for paragraph in chapter.paragraphs: 
      setup_paragraph(paragraph) 
      test_it(doc, chapter, paragraph) 
      teardown_paragraph(paragraph) 
     teardown_chapter(chapter) 
    teardown_doc(doc) 

我々は次のようなデータがある場合::

Document Alpha 
    chapter A 
     Paragraph A1 
     Paragraph A2 
    chapter B 
     Paragraph B1 

を私が収集したテストケースがあることを期待する:

test_it[Alpha, A, A1] 
test_it[Alpha, A, A2] 
test_it[Alpha, B, B1] 

私がきた擬似コードとして書かれ

pytest_generate_tests、クラスシナリオ、フィクスチャ、およびパラメータ化されたテスト関数のさまざまな組み合わせを試しましたが、bこれを達成することができる。

いずれのポインタも大歓迎です。

答えて

0

Pytest fixtesは独立しているはずです。 あなたの仕事を解決するには、すべてのフィーチャの組み合わせ(ドキュメント - 章 - 段落)の単純なリストで1つのフィクスチャを構築する必要があります。

このようなリストの1つの要素を返す単純なフィクスチャで行うことも、以下のコードで示すようにテスト生成段階でこのリストを生成することもできます。次のようにdocumentchapterparagraph私の方法により、documentによって

documents = { 
     'Alpha': { 
      'A': {'A1': {},'A2': {}}, 
      'B': {'B1': {}} 
     } 
    } 


def pytest_generate_tests(metafunc): 
    """Documents tree from documents""" 
    if 'document' in metafunc.fixturenames: 
     documents_plain = [] 
     for document in documents.keys(): 
      for chapter in documents[document].keys(): 
       for paragraph in documents[document][chapter].keys(): 
        documents_plain.append({'document': document, 'chapter': chapter, 'paragraph': paragraph}) 
     metafunc.parametrize(
      'document', 
      documents_plain, 
      scope='session') 


def test_it(document): 
    print('{}, {}, {}'.format(document['document'], document['chapter'], document['paragraph'])) 


py.test -s 

Alpha, B, B1 
Alpha, A, A1 
Alpha, A, A2 
0

私のように、あなたはdocumentによってパラメータいくつかのテストをしたい、場合は、いくつかの、chapter、まだ他の人が(アンドレイSorokinに触発さ)です。 conftest.pytest.py

>>> pytest test.py -s 
test.py Alpha 
.Alpha A 
.Alpha B 
.Alpha A A2 
.Alpha A A1 
.Alpha B B1 
. 
を実行
def test_d(document): 
    print(document) 

def test_dc(document, chapter): 
    print(document, chapter) 

def test_dcp(document, chapter, paragraph): 
    print(document, chapter, paragraph) 

で次に

import pytest 

documents = { 
    'Alpha': { 
     'A': {'A1': {},'A2': {}}, 
     'B': {'B1': {}} 
    } 
} 

def pytest_generate_tests(metafunc): 
    if 'document' in metafunc.fixturenames: 
     if 'chapter' in metafunc.fixturenames: 
      if 'paragraph' in metafunc.fixturenames: 
       metafunc.parametrize(
        ['document', 'chapter', 'paragraph'], 
        [(d, c, p) for d, cs in documents.items() 
           for c, ps in cs.items() 
           for p in ps.keys() 
        ]) 
      else: 
       metafunc.parametrize(
        ['document', 'chapter'], 
        [(d, c) for d, cs in documents.items() 
          for c in cs.keys() 
        ]) 
     else: 
      metafunc.parametrize(
        'document', documents.keys()) 

関連する問題