python-hypothesis - 假设能否缩小假设未发现的失败测试用例?

标签 python-hypothesis property-based-testing

尼古拉斯·查马斯 wrote在假设用户邮件列表上:

A user reported a bug in a function and provided a reproduction. I decided to write a test for this function to see if Hypothesis could find the same bug.

The test looks like this:

@given(data=lists(tuples(floats())))
def test_percentile(data):
    ...

I’ve run this test with up to 100,000 examples, but Hypothesis is not finding this bug. A very specific set of circumstances that I do not understand need to line up precisely for this bug to reveal itself.

I am now trying to figure out something different: Given a known failing example, can Hypothesis help me shrink it to its simplest form?

The reproduction provided by the user is a list that’s 373 elements long. I managed to manually shrink it down to 45 elements.

Is there any way to get Hypothesis to shrink the known failing example even further?

这个问题的答案是什么?假设可以缩小它没有找到的失败测试用例吗?

(我已将问题复制到此处,以便可以随时更新答案。)

最佳答案

假设只能缩小它生成的示例,但您可以通过以下方式提高发现已知故障的几率......

  1. 运行更长时间(例如过夜) - 计算机时间比您的时间便宜
  2. using target() to 'aim at'已知的失败可以使搜索更加高效

这是假设设计方式的一个基本结果:[生成和收缩都在相同的底层表示上工作,并且因为策略可以包含任意 Python 代码,所以通常不可能“向后运行它们”并转动你的测试用例进入红外。

(在受限设置中可能run generators backwards,原则上这可以用于简单的假设策略 - 但我真的不喜欢只在简单情况下工作的界面。我们使用类似的不过,我们的内部存在欺骗。)

尽管如此,“缩小用户报告的失败案例”多年来一直在我的个人愿望 list 上 - @example(<your value here>).shrink()将是一个可爱的界面(如果我们无法到达它,则失败;如果我们缩小它,dump a patch)。最简单的实现是自动派生一些 target()如上所述的指标;在我们可以的情况下,用分析解决方案来补充它更加复杂和有效。 Support for symbolic execution可能也有用,尽管我们现在正在考虑结合愿望 list 的想法:-)

关于python-hypothesis - 假设能否缩小假设未发现的失败测试用例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77817658/

相关文章:

python - 如何使用假设库创建日期时间索引 pandas DataFrame?

python - 当特别要求不要时,假设策略会生成 inf

python - 如何构建策略来创建具有相同值对的元组数组?

python - 假设检验 : how to sample_from values from another strategy?

testing - 如何在基于属性的测试中得出属性

python - 如何对假设策略实现相对约束?

javascript - 使用 JSVerify 强制进行特定输入

python - 如何重构这个递归策略表达式 Strategy 以参数化其长度?

haskell - 在 Hedgehog 中通过 'Gen' 或通过 'forAll' 生成随机输入的区别

python - 使用 Hypothesis Python 生成条件数据