2

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_valueintegers(min_value=APP_SPECIFIC_INT_MIN, ...)定义测试功能时定义。这可以通过像done here@composite这样的agruments 来为策略完成。但是我怎样才能使使用可配置的策略呢?builds()

4

2 回答 2

5

您可以定义一个返回策略的函数,就像其他任何策略一样:

def some_custom_strategy(a, b):
    return builds(foo, bar(a, b))

当您有额外的参数时,这就是复合材料所发生的一切 - 复合材料正在定义一个返回策略的函数,并且这些额外的参数通过函数传递给底层的修饰函数。

于 2018-04-03T13:39:01.080 回答
3

应用于上述伪代码以使 SUT 配置的整数值可SutConfigurationStrategy配置的解决方案如下所示:

# 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()
def SutConfigurationStrategy(min_int_config, max_int_config):
    return builds(SutConfiguration,
                  integer=integers(min_value=min_int_config, max_value=max_int_config),
                  float=floats(min_value=APP_SPECIFIC_FLOAT_MIN, max_value=APP_SPECIFIC_FLOAT_MAX),
)

@composite
def overall_test_configuration(draw, sut_min_int_config, sut_max_int_config, sut_st=SutConfigurationStrategy, env_st=SutEnvironmentStrategy):
    """Custom strategy which uses draw."""
    sut_config = draw(sut_st(sut_min_int_config, sut_max_int_config))
    env_config = draw(env_st)
    return (sut_config, rc_stereomatching_config, env_config)

然后可以将策略整数值配置为最小10和最大100(再次: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(min_int_config=10, max_int_config=100))
    def test_something():
        """Test which uses overall_test_configuration"""
        ...

旁注class SutConfiguration:在这里使用来封装数据并不是最佳选择。Namedtuple 将是一个更好的选择......

于 2018-04-04T07:36:39.293 回答