著名的基于属性的测试框架假设能够生成大量的测试案例。
但是有什么方法可以限制假设产生的测试用例的数量,以缩短测试时间?
例如将特定的关键字参数提供给@given
装饰器?
著名的基于属性的测试框架假设能够生成大量的测试案例。
但是有什么方法可以限制假设产生的测试用例的数量,以缩短测试时间?
例如将特定的关键字参数提供给@given
装饰器?
这取决于您是要限制一个测试还是全部测试,但是方法类似,并且基于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中。