【问题标题】:How to change max number of test cases generated by hypothesis?如何更改假设生成的最大测试用例数?
【发布时间】:2020-08-01 13:03:07
【问题描述】:

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

但是有没有办法限制假设生成的测试用例的数量,以缩短测试周期?

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

【问题讨论】:

    标签: python automated-tests property-based-testing python-hypothesis


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2011-11-16
      • 2022-01-20
      • 2019-06-26
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多