【问题标题】:Hypothesis strategy to generate multiple kwargs生成多个 kwargs 的假设策略
【发布时间】:2020-06-20 08:53:06
【问题描述】:

用 3 个单独的 numpy 数组来编写我的测试是很自然的,但是每个 numpy 数组的第一个维度必须是相等的长度。作为一个黑客,我可以简单地要求一个更大的 numpy 数组

@given(
    arrays=arrays(
        dtype=float,
        shape=tuples(
            integers(3, 3),
            array_shapes(max_dims=1).map(lambda t: t[0]),
            array_shapes(max_dims=1).map(lambda t: t[0]),
        ),
        elements=floats(width=16, allow_nan=False, allow_infinity=False),
    ),
)
def test(arrays: np.ndarray):
  a, b, c = arrays[0], arrays[1], arrays[2]
  ...

但这掩盖了我真正想要生成的内容,并且无法对每个数组的元素制定单独的策略。有没有办法在保持对第一维大小的约束的同时生成这些数组?我想我会想要类似的东西

@given(
  (a, b, c) = batched_arrays(
    n_arrays=3,
    shared_sizes=array_sizes(max_dims=1),
    unshared_sizes=arrays_sizes(),
    dtypes=[float, int, float],
    elements=[floats(), integers(0), floats(0, 1)])
)
def test(a: np.ndarray, b:np.ndarray, c:np.ndarray):
  assert a.shape[0] == b.shape[0] and a.shape[0] == c.shape[0]
  ...

【问题讨论】:

    标签: python-hypothesis


    【解决方案1】:

    很抱歉回答我自己的问题。事实证明你可以通过 shared 获得这个

    @given(
        a=arrays(float, shared(array_shapes(max_dims=1), key="dim1")),
        b=arrays(float, shared(array_shapes(max_dims=1), key="dim1")),
    )
    def test_shared(a, b):
        assert a.shape[0] == b.shape[0]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-28
      • 2017-04-24
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多