我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用hypothesis.strategies.builds()。
def buildable(message_obj): """Return a "buildable" callable for st.builds which will handle optionals.""" def builder(**kwargs): return message_obj(**{ k: v for k, v in kwargs.items() if v is not None # filter out unpopulated optional param }) builder.__name__ = message_obj.DESCRIPTOR.full_name return builder
def message_to_strategy(message_obj, env, overrides=None): """Generate strategy from message.""" # TODO: nested enums are not supported # TODO: nested messages are not supported return st.builds( buildable(message_obj), **{ field_name: field_to_strategy(field, env, overrides=overrides) for field_name, field in message_obj.DESCRIPTOR.fields_by_name.items() } )
def color_strategy(): """Hypothesis strategy for generating instances of `RGBColor`.""" return st.builds( RGBColor, st.integers(min_value=0, max_value=255), st.integers(min_value=0, max_value=255), st.integers(min_value=0, max_value=255), )
def urls(): """ Strategy for generating ``twisted.python.url.URL``\s. """ return s.builds( URL, scheme=s.just(u'https'), host=dns_names(), path=s.lists(s.text( max_size=64, alphabet=s.characters(blacklist_characters=u'/?#', blacklist_categories=('Cs',)) ), min_size=1, max_size=10))