相关疑难解决方法(0)

使用假设生成具有自定义值限制的列表列表

故事:

目前,我有一个测试功能,需要一个包含以下规则的整数列表:

  1. 子列表的数量(让我们称之为N)可以是1到50
  2. 子列表中的值的数量对于所有子列表(矩形形式)是相同的,并且应该> = 0且<= 5
  3. 子列表中的值不能大于或等于子列表的总数.换句话说,子列表中的每个值都是> = 0和<的整数N

样本有效输入:

[[0]]
[[2, 1], [2, 0], [3, 1], [1, 0]]
[[1], [0]]
Run Code Online (Sandbox Code Playgroud)

示例无效输入:

[[2]]  # 2 is more than N=1 (total number of sublists)
[[0, 1], [2, 0]]  # 2 is equal to N=2 (total number of sublists)
Run Code Online (Sandbox Code Playgroud)

我试图用接近它基于属性的测试,并产生不同的有效输入hypothesis,并试图绕到我的头lists()integers(),但不能使其工作:

  • 条件#1是容易接近lists()min_sizemax_size参数
  • 条件#2包含在 Chaining strategies together
  • 条件#3是我正在努力的 - 因为,如果我们使用rectangle_lists上面的例子,我们没有引用里面的"父"列表的长度integers() …

python testing unit-testing property-based-testing python-hypothesis

11
推荐指数
3
解决办法
1178
查看次数