factory_boy工厂的多重继承 - 似乎只继承了第一个父

时间:2015-10-24 07:38:28

标签: python inheritance multiple-inheritance 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中支持可能并不简单。)

1 个答案:

答案 0 :(得分:3)

这种方法适用于我,并且最终的子类都继承了相关的工厂和后期生成方法,因此我希望实际场景比上面的测试代码更复杂。这是我的实际代码:

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