簡體   English   中英

參數化夾具和功能,無需組合

[英]Parametrizing fixtures and functions without combinations

事情是這樣的:

我有一個接收參數'shape'和'val'的Array2D class。 構造函數如下:

class Array2D:
    def __init__(self, shape: tuple, val):
        self.shape = shape
        self.size = self.shape[0] * self.shape[1]
        self.data = [val] * self.size

出於學習的原因,我想在這個 function 中執行測試。 為此,我聲明了一個變量 ARRAY_CONFIG,它列出了不同二維 arrays 的參數。 這是一個例子:

ARRAY_CONFIG = [
    [(2, 3), 3],
    [(2, 3), 1.7]
]

我還定義了其他列表,用於存儲未來測試的預期值。 看一看:

SIZE = [6, 6]
DATA = [
    [3, 3, 3, 3, 3, 3],
    [1.7, 1.7, 1.7, 1.7, 1.7, 1.7]
]

現在,我想通過參數化裝飾器構建一個測試 function,在這些列表之間進行比較。 這是一個沒有來自 PyTest 庫的裝飾器的示例:

def test_size_and_data():
    for i in range(len(ARRAY_CONFIG)):
        array_config = ARRAY_CONFIG[i]
        size = SIZE[i]
        data = DATA[i]

        array = Array2D(*array_config)
        assert array.size == size
        assert array.data == data

我設法開發了一些東西,但對我來說似乎有些重復。 這里是:

def combine(list1, list2):
    return [(list1[i], list2[i]) for i in range(len(list1))]


@pytest.fixture
def array(request):
    return Array2D(*request.param)


@pytest.mark.parametrize('array, expected', combine(ARRAY_CONFIG, SIZE), indirect=['array'])
def test_size(array, expected):
    assert array.size == expected


@pytest.mark.parametrize('array, expected', combine(ARRAY_CONFIG, DATA), indirect=['array'])
def test_data(array, expected):
    assert array.data == expected

在有“預期值”列表的情況下執行這種測試的最佳方法是什么? 例如,我嘗試執行兩個參數化,但最終我執行了值之間的組合,而不是一對一的比較。 如此處所做:

# Other tests --> generate 4 tests instead of 2
@pytest.fixture(params=ARRAY_CONFIG)
def array(request):
    return Array2D(*request.param)


@pytest.fixture
def array_size(array):
    return array.size


@pytest.mark.parametrize('expected', SIZE)
def test_size(array_size, expected):
    assert array_size == expected

提前致謝。

如果您使用combine function 作為參數,它會簡單得多parametrize 您可以yield每組值

def combine():
    for arr, size, data in zip(ARRAY_CONFIG, SIZE, DATA):
        yield Array2D(*arr), size, data


@pytest.mark.parametrize('expected', combine())
def test_size_and_data(expected):
    array, size, data = expected
    print(array.size, size)
    print(array.data, data)

或更明確的參數

@pytest.mark.parametrize('array, size, data', combine())
def test_size_and_data(array, size, data):
    print(array.size, size)
    print(array.data, data)

Output

example_test.py::test_size_and_data[expected0] PASSED                    [ 50%]
6 6
[3, 3, 3, 3, 3, 3] [3, 3, 3, 3, 3, 3]

example_test.py::test_size_and_data[expected1] PASSED                    [100%]
6 6
[1.7, 1.7, 1.7, 1.7, 1.7, 1.7] [1.7, 1.7, 1.7, 1.7, 1.7, 1.7]

--------------------------------------------------------------

example_test.py::test_size_and_data[array0-6-data0] PASSED               [ 50%]
6 6
[3, 3, 3, 3, 3, 3] [3, 3, 3, 3, 3, 3]

example_test.py::test_size_and_data[array1-6-data1] PASSED               [100%]
6 6
[1.7, 1.7, 1.7, 1.7, 1.7, 1.7] [1.7, 1.7, 1.7, 1.7, 1.7, 1.7]

暫無
暫無

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

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