【问题标题】:How do I use composite strategies in hypothesis (hypothesis.errors.InvalidArgument: Expected SearchStrategy but got function)如何在假设中使用复合策略(hypothesis.errors.InvalidArgument: Expected SearchStrategy but got function)
【发布时间】:2018-06-15 21:36:15
【问题描述】:

此示例是docs 中的示例的变体:

import hypothesis.strategies as st

from hypothesis import given

@st.composite
def s(draw):
    x = draw(st.text(), min_size=1)
    y = draw(st.text(alphabet=x))
    return (x, y)



@given(s1=s, s2=s)
def test_subtraction(s1, s2):

    print(s1, s2)

    assert 0

失败了:

E hypothesis.errors.InvalidArgument: Expected SearchStrategy but got <function accept.<locals>.s at 0x7fd7e5c05620> (type=function)

/mnt/work/unfuncat/software/anaconda/lib/python3.6/site-packages/hypothesis/internal/validation.py:40: InvalidArgument

我做错了什么?

【问题讨论】:

  • 这里的draw 是什么?

标签: python-hypothesis


【解决方案1】:

您需要调用复合函数。这在文档中没有解释,但在 2016 blog post 中有一个示例。

@given(s1=s(), s2=s()) # <===== change
def test_subtraction(s1, s2):

    print(s1, s2)

    assert 0

【讨论】:

  • 还有an example in the docs (list_and_index),但我同意这应该在正文中明确说明。
  • 展示了如何在 REPL 中生成一个示例。我不清楚这是否意味着您必须在给定的语句中使用函数调用。在没有函数调用的情况下使用常规策略。我认为写复合材料是不同的会很有帮助:)
猜你喜欢
  • 2021-03-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
  • 2020-09-20
相关资源
最近更新 更多