【发布时间】:2020-08-01 13:03:07
【问题描述】:
著名的基于属性的测试框架假设能够生成大量测试用例。
但是有没有办法限制假设生成的测试用例的数量,以缩短测试周期?
例如将特定的关键字参数提供给 @given 装饰器?
【问题讨论】:
标签: python automated-tests property-based-testing python-hypothesis
著名的基于属性的测试框架假设能够生成大量测试用例。
但是有没有办法限制假设生成的测试用例的数量,以缩短测试周期?
例如将特定的关键字参数提供给 @given 装饰器?
【问题讨论】:
标签: python automated-tests property-based-testing python-hypothesis
这取决于您是要限制一项测试还是全部测试,但方法类似并基于settings。
要更改某些测试的默认行为,我们可以使用settings object 来装饰它们
from hypothesis import given, settings, strategies
@given(strategies.integers())
@settings(max_examples=10)
def test_this_a_little(x):
...
@given(strategies.integers())
@settings(max_examples=1000)
def test_this_many_times(x):
...
对于test_this_a_little,将生成10 示例(最多),对于test_this_many_times,将生成1000。
要在测试运行的引导期间更改所有测试的默认行为(例如,对于pytest,它可以是conftest.py module),我们可以定义一个自定义的hypothesis 设置配置文件,然后在测试调用期间使用它,例如
from hypothesis import settings
settings.register_profile('my-profile-name',
max_examples=10)
然后(假设您使用的是pytest)
> pytest --hypothesis-profile=my-profile-name
hypothesis 非常棒,它允许我们配置很多东西,可用的选项列在in the docs。
【讨论】: