gpt4 book ai didi

python - 如何创建使用 `builds()` 的可配置自定义假设策略?

转载 作者:行者123 更新时间:2023-12-03 23:40:52 25 4
gpt4 key购买 nike

我使用 builds() 创建了自定义假设策略和 @composite (设计灵感来自 this example from the docs )。策略的设计类似于下面的伪代码:

# 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 in integers(min_value=APP_SPECIFIC_INT_MIN, ...) 定义测试函数时。这可以通过像 done here 这样的参数对 @composite 策略进行。 .但是如何使使用 builds() 的策略可配置?

最佳答案

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

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

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

关于python - 如何创建使用 `builds()` 的可配置自定义假设策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49630196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com