python-factory_boy工厂的多重继承-仅似乎继承了第一个父对象
作者:互联网
我正在尝试从两个〜Factory factory_boy类继承,但是似乎只有第一个类是从继承的,而第二个类却被忽略了. (他们确实有一个共同的父母.)
class BasicFactory(factory.DjangoModelFactory):
# Meta class, some SubFactories and RelatedFactoriues, a post_generation method
class BasicFactoryWithExtraRelatedFactories(BasicFactory):
another_related = factory.RelatedFactory(SomethingElseFactory, 'basic', etc)
class BasicFactoryWithExtraPostGeneration(BasicFactory):
@factory.post_generation
def post(self, create, extracted, **kwargs):
"""
A replacement post_generation method
"""
class ExtraRelatedAndExtraPostGenerationFactory(BasicFactoryWithExtraRelatedFactories, BasicFactoryWithExtraPostGeneration)
"""
This seems to inherit the extra RelatedFactories, but not the extra post_generation method.
"""
class ExtraRelatedAndExtraPostGenerationFactory(BasicFactoryWithExtraPostGeneration, BasicFactoryWithExtraRelatedFactories)
"""
This seems to inherit the extra post_generation method, but not the extra RelatedFactories.
"""
有什么办法可以我都继承吗?
我知道这是一个艰巨的任务,但这将为解决我遇到的问题提供如此奇妙的解决方案,从而使我可以简单地创建具有多重继承的所有测试用例组合. (我认识到在factory_boy中支持它可能并不简单).
解决方法:
这种方法对我有用,并且两个最终子类都继承了相关的工厂和后生成方法,因此我希望实际方案比上面的测试代码更复杂.这是我的实际代码:
class BasicFactory(factory.DjangoModelFactory):
country = factory.SubFactory(CountryFactory)
code = factory.Sequence(lambda n: 'unit%d' % n)
name = factory.Sequence(lambda n: 'Unit %d' % n)
full_name = factory.Sequence(lambda n: 'Unit %d, etc.' % n)
unit_type = factory.SubFactory(GeographicUnitTypeFactory)
class Meta:
model = 'spatial.GeographicUnit'
class BasicFactoryWithExtraRelatedFactories(BasicFactory):
season = factory.RelatedFactory(SeasonFactory, 'geographicunit')
class BasicFactoryWithExtraPostGeneration(BasicFactory):
@factory.post_generation
def post(self, create, extracted, **kwargs):
"""
A replacement post_generation method
"""
self.name = self.name + ' after post'
class ExtraRelatedAndExtraPostGenerationFactory(BasicFactoryWithExtraRelatedFactories, BasicFactoryWithExtraPostGeneration):
"""
This seems to inherit the extra RelatedFactories, but not the extra post_generation method.
"""
class ExtraPostGenerationAndExtraRelatedFactory(BasicFactoryWithExtraPostGeneration, BasicFactoryWithExtraRelatedFactories):
"""
This seems to inherit the extra post_generation method, but not the extra RelatedFactories.
"""
from django.test import TestCase
class MyTestCase(TestCase):
def test_extrarelated(self):
obj = ExtraRelatedAndExtraPostGenerationFactory()
self.assertTrue(obj.name.endswith(' after post'))
self.assertEqual(Season.objects.filter(geographicunit=obj).count(), 1)
def test_extrapostgeneration(self):
obj = ExtraPostGenerationAndExtraRelatedFactory()
self.assertTrue(obj.name.endswith(' after post'))
self.assertEqual(Season.objects.filter(geographicunit=obj).count(), 1)
结果如下:
./manage.py test --settings=fdw.settings.sqlite spatial.tests.factories:MyTestCase
nosetests spatial.tests.factories:MyTestCase --nocapture --nologcapture --stopwatch-file=.nose-stopwatch-times-sqlite --verbosity=1
Creating test database for alias 'default'...
..
----------------------------------------------------------------------
Ran 2 tests in 0.037s
OK
标签:inheritance,factory-boy,multiple-inheritance,python 来源: https://codeday.me/bug/20191119/2036926.html