我试图调整文档的example
integers(min_value=0, max_value=10).flatmap(lambda n:
... lists(lists(integers(), min_size=n, max_size=n)))
生成3个整数的元组,其中2/3由另一个整数限定,
@given(integers(min_value=0).flatmap(
lambda n: tuples(n, integers(min_value=0, max_value=n),
integers(min_value=0, max_value=n)))
)
def testIdentityConfiguration(self, config):
cipher = ShiftCipher(base=config[0], key=config[1])
assert cipher.decipher(cipher.cipher(config[2])) == config[2]
但是在运行时,我得到了这个不幸的堆栈跟踪:
/usr/bin/python3.4 /opt/pycharm-eap/helpers/pycharm/utrunner.py ./py4175/ true
Testing started at 1:05 AM ...
Error
Traceback (most recent call last):
File ./.hypothesis/eval_source/hypothesis_temporary_module_065b4bdcf5d456cb3454a65c416569a728b57d13.py", line 5, in testIdentityConfiguration
return f(self, config)
File "/usr/lib/python3.4/site-packages/hypothesis/core.py", line 574, in wrapped_test
print_example=True
File "/usr/lib/python3.4/site-packages/hypothesis/executors/executors.py", line 25, in default_executor
return function()
File "/usr/lib/python3.4/site-packages/hypothesis/core.py", line 354, in run
args, kwargs = search_strategy.reify(template)
File "/usr/lib/python3.4/site-packages/hypothesis/searchstrategy/reprwrapper.py", line 49, in reify
return self.wrapped_strategy.reify(value)
File "/usr/lib/python3.4/site-packages/hypothesis/searchstrategy/collections.py", line 65, in reify
e.reify(v) for e, v in zip(self.element_strategies, value)
File "/usr/lib/python3.4/site-packages/hypothesis/searchstrategy/collections.py", line 65, in <listcomp>
e.reify(v) for e, v in zip(self.element_strategies, value)
File "/usr/lib/python3.4/site-packages/hypothesis/searchstrategy/reprwrapper.py", line 49, in reify
return self.wrapped_strategy.reify(value)
File "/usr/lib/python3.4/site-packages/hypothesis/searchstrategy/strategies.py", line 570, in reify
return self.pack(self.mapped_strategy.reify(value))
File "/usr/lib/python3.4/site-packages/hypothesis/searchstrategy/collections.py", line 65, in reify
e.reify(v) for e, v in zip(self.element_strategies, value)
File "/usr/lib/python3.4/site-packages/hypothesis/searchstrategy/collections.py", line 65, in <listcomp>
e.reify(v) for e, v in zip(self.element_strategies, value)
File "/usr/lib/python3.4/site-packages/hypothesis/searchstrategy/strategies.py", line 570, in reify
return self.pack(self.mapped_strategy.reify(value))
File "/usr/lib/python3.4/site-packages/hypothesis/searchstrategy/flatmapped.py", line 48, in pack
return morpher.become(strategy(self.expand(source)))
File "./test_shiftCipher.py", line 22, in <lambda>
integers(min_value=0, max_value=n)))
File "./.hypothesis/eval_source/hypothesis_temporary_module_319ade46afdab08a61611170bc661bd6b6fd0507.py", line 5, in tuples
return f(*args)
File "/usr/lib/python3.4/site-packages/hypothesis/strategies.py", line 60, in accept
result = strategy_definition(*args, **kwargs)
File "/usr/lib/python3.4/site-packages/hypothesis/strategies.py", line 296, in tuples
check_strategy(arg)
File "/usr/lib/python3.4/site-packages/hypothesis/strategies.py", line 704, in check_strategy
check_type(SearchStrategy, arg)
File "/usr/lib/python3.4/site-packages/hypothesis/strategies.py", line 700, in check_type
u'Expected %s but got %r' % (typ_string, arg,))
hypothesis.errors.InvalidArgument: Expected SearchStrategy but got 0
我试图摆弄它并附加.example()
,并发现删除内部策略并替换为n
,例如,但是一旦我开始使用n
作为<{1}}内策略的命名参数,我收到错误。
tuples()
这个例子确实有效,我看不出我在做什么不同。
>>> integers(min_value=0).flatmap(lambda n: tuples(integers())).example()
(64608439914476552176530421138352852526387959835075692,)
>>> integers(min_value=0).flatmap(lambda n: tuples(integers(max_value=n), n, n)).example()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/hypothesis/searchstrategy/strategies.py", line 180, in example
return self.reify(template)
File "/usr/lib/python3.4/site-packages/hypothesis/searchstrategy/strategies.py", line 570, in reify
return self.pack(self.mapped_strategy.reify(value))
File "/usr/lib/python3.4/site-packages/hypothesis/searchstrategy/flatmapped.py", line 48, in pack
return morpher.become(strategy(self.expand(source)))
File "<stdin>", line 1, in <lambda>
File "/home/automaticgiant/.hypothesis/eval_source/hypothesis_temporary_module_319ade46afdab08a61611170bc661bd6b6fd0507.py", line 5, in tuples
return f(*args)
File "/usr/lib/python3.4/site-packages/hypothesis/strategies.py", line 60, in accept
result = strategy_definition(*args, **kwargs)
File "/usr/lib/python3.4/site-packages/hypothesis/strategies.py", line 296, in tuples
check_strategy(arg)
File "/usr/lib/python3.4/site-packages/hypothesis/strategies.py", line 704, in check_strategy
check_type(SearchStrategy, arg)
File "/usr/lib/python3.4/site-packages/hypothesis/strategies.py", line 700, in check_type
u'Expected %s but got %r' % (typ_string, arg,))
hypothesis.errors.InvalidArgument: Expected SearchStrategy but got 2141
答案 0 :(得分:3)
我认为你误解了the documentation for the tuples function。元组的参数是策略,它产生一个与参数数量相同的元组,其值从相应的参数中提取。
调用元组的第一个参数是&#39; n&#39;,这是从外部策略中提取的整数,而不是一个策略。如果您想要一个始终返回该值的策略,则需要使用just(n)将其包装为一个。