Python 假设:确保输入列表具有相同的长度

Python hypothesis: Ensure that input lists have same length

我正在使用假设来测试将两个等长列表作为输入的函数。

import hypothesis.strategies as st
from hypothesis import assume, given


@given(st.lists(ints, min_size=1),
       st.lists(ints, min_size=1),
       )
def test_my_func(x, y):
    assume(len(x) == len(y))

    # Assertions

这给了我错误信息:

FailedHealthCheck: It looks like your strategy is filtering out a lot of data. Health check found 50 filtered examples but only 4 good ones.

假设 len(x) == len(y) 过滤掉了太多的输入。所以我想生成一个随机正数并将其用作 xy 的长度。有什么办法可以做到吗?

您可以使用 flatmap 生成依赖于其他生成数据的数据。

import hypothesis.strategies as st
from hypothesis import assume, given
from hypothesis.strategies import integers as ints

same_len_lists = ints(min_value=1, max_value=100).flatmap(lambda n: st.lists(st.lists(ints(), min_size=n, max_size=n), min_size=2, max_size=2))

@given(same_len_lists)
def test_my_func(lists):
    x, y = lists
    assume(len(x) == len(y))

有点笨拙,而且我对必须在测试体内解压列表感到不太高兴。

我使用 @composite 装饰器找到了答案。

import hypothesis.strategies as st
from hypothesis import given

@st.composite
def same_len_lists(draw):

    n = draw(st.integers(min_value=1, max_value=50))
    fixed_length_list = st.lists(st.integers(), min_size=n, max_size=n)

    return (draw(fixed_length_list), draw(fixed_length_list))


@given(same_len_lists())
def test_my_func(lists):

    x, y = lists

    # Assertions

其他解决方案提供了很好的可重用策略。这是一个简短的低技术解决方案,可能更适合一次性使用,因为您需要在测试函数中进行一行处理。我们使用 zip 转置成对列表(2 元素元组);从概念上讲,我们将 n x 2 矩阵转换为 2 x n 矩阵。

import hypothesis.strategies as st
from hypothesis import given

pair_lists = st.lists(st.tuples(st.integers(), st.integers()), min_size=1)

@given(pair_lists)
def test_my_func(L):
    x, y = map(list, zip(*L))

警告:min_size=1 很重要,因为如果列表为空,zip 什么也不会给出。