【发布时间】:2017-01-30 02:03:45
【问题描述】:
故事:
我目前正在使用hypothesis 和custom generation strategy 对函数进行单元测试,试图找到一个特定的输入来“破坏”我当前的解决方案。这是我的测试的样子:
from solution import answer
# skipping mystrategy definition - not relevant
@given(mystrategy)
def test(l):
assert answer(l) in {0, 1, 2}
基本上,当answer() 函数不返回 0 或 1 或 2 时,我正在寻找可能的输入。
这是我的当前工作流程的样子:
- 运行测试
-
hypothesis查找产生AssertionError的输入:$ pytest test.py =========================================== test session starts ============================================ ... ------------------------------------------------ Hypothesis ------------------------------------------------ Falsifying example: test(l=[[0], [1]]) 使用此特定输入调试函数,尝试了解此输入/输出是否合法且函数是否正常工作
问题:
如何跳过这个伪造的生成示例(在这种情况下为[[0], [1]])并要求hypothesis 为我生成一个不同的示例?
这个问题也可以解释为:如果发现一个伪造的例子,我可以要求hypothesis不要终止,而是生成更多的伪造例子吗?
【问题讨论】:
-
我不熟悉假设,但我知道它因为断言而停止。如果您只是想暂时解决这个问题,您可以打印一条失败消息,但实际上并不断言。因此,框架会认为它通过并继续运行。
-
@KennyOstrom 是的,但是
hypothesis在生成大量样本输入方面做得很好也很快。我可以解决它有一组输入来忽略并将这个“不在”检查添加到测试本身,但这不会扩展..另外我确信hypothesis有一个内置的方法来解决这个问题,此时我对图书馆还不够熟悉。谢谢!
标签: python unit-testing testing property-based-testing python-hypothesis