簡體   English   中英

pytest嵌套參數化與樹狀數據

[英]pytest nested parametrization with tree-like data

使用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,類方案,固定裝置和參數化測試函數的不同組合,但未能實現。

任何指針將不勝感激。

Pytest夾具應該是獨立的。 因此,要解決您的任務,您必須使用所有功能組合的簡單列表(文檔-章-段落)構建一個燈具。

您可以使用簡單的夾具返回該列表的一個元素來執行此操作,也可以在測試生成階段生成此列表,如下面的代碼所示。

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

如果像我一樣,您想要通過document參數化某些測試,通過documentchapter某些測試,而通過documentchapterparagraph其他測試,那么我的方法(受Andrey Sorokin啟發)如下。

conftest.py

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())

然后在test.py

def test_d(document):
    print(document)

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

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

運行

>>> pytest test.py -s
test.py Alpha
.Alpha A
.Alpha B
.Alpha A A2
.Alpha A A1
.Alpha B B1
.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM