如何使用假设执行Python函数';综合战略?

如何使用假设执行Python函数';综合战略?,python,pytest,python-hypothesis,property-based-testing,Python,Pytest,Python Hypothesis,Property Based Testing,我正在尝试执行一个用“@strategy.compositedecorator”修饰的函数 我知道我可以使用@给定的装饰器测试函数,例如- from hypothesis import given from hypothesis import strategies as st @given(st.integers(min_value=1)) def test_bar(x): assert x > 0 使用-pytest 但是对于具有类似于装饰器的@strategy.compos

我正在尝试执行一个用“
@strategy.composite
decorator”修饰的函数

我知道我可以使用
@给定的
装饰器测试函数,例如-

from hypothesis import given
from hypothesis import strategies as st

@given(st.integers(min_value=1))
def test_bar(x):
    assert x > 0
使用-
pytest

但是对于具有类似于装饰器的
@strategy.composite
函数-

from hypothesis import strategies as st
from hypothesis import given
import pytest

@st.composite
def test_foo(draw):
    arg1 = draw(st.integers(min_value=1))
    arg2 = draw(st.lists(st.integers(), min_size=arg1, max_size=arg1))
    print(arg1, " ", arg2)
    assert(len(arg2) == arg1)
我无法以类似的方式执行测试。
使用pytest时,我无法执行测试(使用
python
执行python文件没有任何作用)-

如果我做了
@given()
-请注意额外的大括号,相反,我会得到另一个错误-

========================================= FAILURES ==========================================
_________________________________________ test_foo __________________________________________

arguments = (), kwargs = {}

    def wrapped_test(*arguments, **kwargs):
>       raise InvalidArgument(message)
E       hypothesis.errors.InvalidArgument: given must be called with at least one argument

/usr/lib/python3.8/site-packages/hypothesis/core.py:234: InvalidArgument
================================== short test summary info ==================================
FAILED testhypo.py::test_foo - hypothesis.errors.InvalidArgument: given must be called wit...
我尝试将代码包装到另一个函数中-

from hypothesis import strategies as st
from hypothesis import given
import pytest

def demo():
    @st.composite
    def test_foo(draw):
        arg1 = draw(st.integers(min_value=1))
        arg2 = draw(st.lists(st.integers(), min_size=arg1, max_size=arg1))
        print(arg1, " ", arg2)
        assert(len(arg2) == arg1)
但这也不起作用-

[reik@reik-msi tests]$ python testhypo.py
[reik@reik-msi tests]$ pytest testhypo.py
==================================== test session starts ====================================
platform linux -- Python 3.8.3, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: /home/reik/tests
plugins: hypothesis-5.16.0, lazy-fixture-0.6.3
collected 0 items                                                                           

=================================== no tests ran in 0.00s ===================================
(我还尝试将
demo
函数调用放在文件末尾,但这丝毫没有改变测试行为)

声明说调用该函数可以工作,但显然不行。(公平地说,文档没有指定如何使用
@composite
运行测试)


如何测试用
@strategy.composite
修饰的函数?我不必使用pytest——我更希望不必实际使用它,但它似乎是测试函数(用
@given
修饰)的最简单方法,所以我决定走这条路。

是定义自定义策略的辅助函数,而不是测试

通过使用
@given
st.data()
,可以完成您正在尝试的操作:

@给定(st.data())
def测试_foo(数据):
x=data.draw(st.integers())
...
提供了如何使用这些技术的良好概述

from hypothesis import strategies as st
from hypothesis import given
import pytest

def demo():
    @st.composite
    def test_foo(draw):
        arg1 = draw(st.integers(min_value=1))
        arg2 = draw(st.lists(st.integers(), min_size=arg1, max_size=arg1))
        print(arg1, " ", arg2)
        assert(len(arg2) == arg1)
[reik@reik-msi tests]$ python testhypo.py
[reik@reik-msi tests]$ pytest testhypo.py
==================================== test session starts ====================================
platform linux -- Python 3.8.3, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: /home/reik/tests
plugins: hypothesis-5.16.0, lazy-fixture-0.6.3
collected 0 items                                                                           

=================================== no tests ran in 0.00s ===================================