3

I am trying to inherit from two ~Factory factory_boy classes, but it seems only the first class is inherited from, and the second is ignored. (They do have a common parent.)

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.
    """

Is there any way I can inherit both?

This is a tall order, I know, but it would enable such a fantastically beautiful solution to a problem I've got, whereby I could create all my required combinations of test cases simply with multiple inheritance. (I recognise that it is probably not 'simple' to support in factory_boy.)

4

1 回答 1

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
于 2015-11-09T05:26:04.130 回答