【问题标题】:python Hypothesis test with optional parameter带有可选参数的python假设测试
【发布时间】:2018-01-12 16:57:19
【问题描述】:

在我的项目中,我使用Hypothesis 来测试一个功能。 被测函数接受一个名为stop 的强制参数和两个分别名为startstep 的可选参数。 如果参数step为零,则被测代码应触发异常。

这是我的测试函数

@given(start=st.integers(min_value=-1000, max_value=1000),
       stop=st.integers(min_value=-1000, max_value=1000),
       step=st.integers(min_value=-1000, max_value=1000))
@settings(settings(verbosity=Verbosity.verbose))
def test_XXX_for_integer(start, stop, step):
    if step == 0:
        with raises(ValueError) as excinfo:
            _ = myrange3(start, stop, step)
        assert 'arg 3 must not be zero' in str(excinfo.value)
    else:
        output = <some code>
        expected = <some output> 
        assert output == expected

我的问题:我还想模拟startstep 是可选的,因此这些参数中的一个或两个都设置为None。如果不为每个代码变体重新创建专用测试函数,我该如何做到这一点?

【问题讨论】:

    标签: python python-3.x python-hypothesis


    【解决方案1】:

    您可以像加入集合一样加入策略。示例:

    strategy_bools_or_ints = st.booleans() | st.integers()
    

    在测试中,你可以加入st.integersst.none

    st_input = st.integers(min_value=-1000, max_value=1000)
    st_input_optional = st_input | st.none()
    
    @given(start=st_input_optional, stop=st_input, step=st_input_optional)
    def test_XXX_for_integer(start, stop, step):
        assert myrange3(stop, start=start, step=step)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 2019-07-13
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多