假设策略:对于每个"bucket",从桶中抽取一个值
Hypothesis strategy: for each "bucket", draw one value from the bucket
我有以下方法在我的一个测试中生成随机数据:
import random
data_categories = {
'a': [1, 2, 3],
'b': [4, 5],
'c': [6, 7, 8]
}
def make_record():
return [random.choice(vals) for vals in data_categories.values()]
如何将其转换为假设策略?
这是我尝试使用 hypothesis.strategies.composite
,但很难知道我是否正确使用它:
import hypothesis.strategies as hs
@hs.composite
def make_record(draw):
return [draw(hs.sampled_from(vals)) for vals in data_categories.values()]
您的尝试基本上是正确的,除了字典迭代顺序可能不可靠 - 因此示例可能无法正确重现或缩小。我会把它写成内联,如:
my_strategy = hs.tuples(
*[hs.sampled_from(data_categories[k]) for k in sorted(data_categories)]
).map(list)
我有以下方法在我的一个测试中生成随机数据:
import random
data_categories = {
'a': [1, 2, 3],
'b': [4, 5],
'c': [6, 7, 8]
}
def make_record():
return [random.choice(vals) for vals in data_categories.values()]
如何将其转换为假设策略?
这是我尝试使用 hypothesis.strategies.composite
,但很难知道我是否正确使用它:
import hypothesis.strategies as hs
@hs.composite
def make_record(draw):
return [draw(hs.sampled_from(vals)) for vals in data_categories.values()]
您的尝试基本上是正确的,除了字典迭代顺序可能不可靠 - 因此示例可能无法正确重现或缩小。我会把它写成内联,如:
my_strategy = hs.tuples(
*[hs.sampled_from(data_categories[k]) for k in sorted(data_categories)]
).map(list)