如何更改由假设生成的测试用例的最大数量?

著名的基于属性的测试框架假设能够生成大量的测试案例。

但是有什么方法可以限制假设产生的测试用例的数量,以缩短测试时间?

例如将特定的关键字参数提供给@given装饰器?

rczqy 回答:如何更改由假设生成的测试用例的最大数量?

这取决于您是要限制一个测试还是全部测试,但是方法类似,并且基于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中。

本文链接:https://www.f2er.com/2410136.html

大家都在问