builds()
我使用and创建了自定义假设策略@composite
(设计灵感来自文档中的这个示例)。这些策略的设计类似于下面的伪代码:
# strategies.py
from hypothesis.strategies import builds, composite, draw, floats, integers
class SutConfiguration:
""" Data which represents the configuration of the system under test. """
def __init__(self, integer, float):
self.integer = integer
self.float = float
# custom strategy which uses builds()
SutConfigurationStrategy = builds(
SutConfiguration,
integer=integers(min_value=APP_SPECIFIC_INT_MIN, max_value=APP_SPECIFIC_INT_MAX),
float=floats(min_value=APP_SPECIFIC_FLOAT_MIN, max_value=APP_SPECIFIC_FLOAT_MAX),
)
@composite
def overall_test_configuration(draw, sut_st=SutConfigurationStrategy, env_st=SutEnvironmentStrategy):
"""Custom strategy which uses draw."""
sut_config = draw(sut_st)
env_config = draw(env_st)
return (sut_config, rc_stereomatching_config, env_config)
该策略照常使用,例如unittest
用作测试运行器:
# test.py
import unittest
from <package>.strategies import overall_test_configuration
class TestSut(unittest.TestCase):
"""Class containing several tests for the system under test."""
@given(overall_test_configuration())
def test_something():
"""Test which uses overall_test_configuration"""
...
现在我想让策略可配置到实际应用程序中,例如min_value
在integers(min_value=APP_SPECIFIC_INT_MIN, ...)
定义测试功能时定义。这可以通过像done here@composite
这样的agruments 来为策略完成。但是我怎样才能使使用可配置的策略呢?builds()