不同 Django 模型的相同假设检验
Same hypothesis test for different django models
我想使用假设来测试我们编写的用于从 Django 模型创建 avro 模式的工具。使用 django extra 为单个模型编写测试非常简单:
from avro.io import AvroTypeException
from hypothesis import given
from hypothesis.extra.django.models import models as hypothetical
from my_code import models
@given(hypothetical(models.Foo))
def test_amodel_schema(self, amodel):
"""Test a model through avro_utils.AvroSchema"""
# Get the already-created schema for the current model:
schema = (s for m, s in SCHEMA if m == amodel.model_name)
for schemata in schema:
error = None
try:
schemata.add_django_object(amodel)
except AvroTypeException as error:
pass
assert error is None
...但是如果我要为每个可以进行 avro 模式化的模型编写测试,它们将完全相同,除了 given
装饰器的参数。我可以获得所有我有兴趣测试的模型 ContentTypeCache.list_models()
returns schema_name: model
的字典(是的,我知道,它不是列表)。但是我怎样才能生成像
这样的代码
for schema_name, model in ContentTypeCache.list_models().items():
@given(hypothetical(model))
def test_this_schema(self, amodel):
# Same logic as above
我基本上考虑过动态生成每个测试方法并将其直接附加到全局变量,但这听起来很难理解。我怎样才能为不同的 django 模型编写相同的基本参数测试,尽可能减少混乱的动态编程?
您可以使用 one_of
:
将其编写为单个测试
import hypothesis.strategies as st
@given(one_of([hypothetical(model) for model in ContentTypeCache.list_models().values()]))
def test_this_schema(self, amodel):
# Same logic as above
在这种情况下,您可能希望使用 @settings(max_examples=settings.default.max_examples * len(ContentTypeCache.list_models()))
之类的方法来增加测试的数量 运行,以便它 运行 的示例数量与 N 测试相同。
我通常会通过参数化测试并从内部借鉴策略来解决此类问题:
@pytest.mark.parametrize('model_type', list(ContentTypeCache.list_models().values()))
@given(data=st.data())
def test_amodel_schema(self, model_type, data):
amodel = data.draw(hypothetical(model_type))
...
我想使用假设来测试我们编写的用于从 Django 模型创建 avro 模式的工具。使用 django extra 为单个模型编写测试非常简单:
from avro.io import AvroTypeException
from hypothesis import given
from hypothesis.extra.django.models import models as hypothetical
from my_code import models
@given(hypothetical(models.Foo))
def test_amodel_schema(self, amodel):
"""Test a model through avro_utils.AvroSchema"""
# Get the already-created schema for the current model:
schema = (s for m, s in SCHEMA if m == amodel.model_name)
for schemata in schema:
error = None
try:
schemata.add_django_object(amodel)
except AvroTypeException as error:
pass
assert error is None
...但是如果我要为每个可以进行 avro 模式化的模型编写测试,它们将完全相同,除了 given
装饰器的参数。我可以获得所有我有兴趣测试的模型 ContentTypeCache.list_models()
returns schema_name: model
的字典(是的,我知道,它不是列表)。但是我怎样才能生成像
for schema_name, model in ContentTypeCache.list_models().items():
@given(hypothetical(model))
def test_this_schema(self, amodel):
# Same logic as above
我基本上考虑过动态生成每个测试方法并将其直接附加到全局变量,但这听起来很难理解。我怎样才能为不同的 django 模型编写相同的基本参数测试,尽可能减少混乱的动态编程?
您可以使用 one_of
:
import hypothesis.strategies as st
@given(one_of([hypothetical(model) for model in ContentTypeCache.list_models().values()]))
def test_this_schema(self, amodel):
# Same logic as above
在这种情况下,您可能希望使用 @settings(max_examples=settings.default.max_examples * len(ContentTypeCache.list_models()))
之类的方法来增加测试的数量 运行,以便它 运行 的示例数量与 N 测试相同。
我通常会通过参数化测试并从内部借鉴策略来解决此类问题:
@pytest.mark.parametrize('model_type', list(ContentTypeCache.list_models().values()))
@given(data=st.data())
def test_amodel_schema(self, model_type, data):
amodel = data.draw(hypothetical(model_type))
...