使用 Factoryboy SubFactory 找不到夹具
fixture not found using Factoryboy SubFactory
我在构建带有子工厂的工厂来测试 Django 模型时出错。
使用型号:
class Space(ExportModelOperationsMixin('space'), models.Model):
name = models.CharField(max_length=128, default='Default')
class ShoppingListEntry(models.Model):
food = models.ForeignKey(Food)
space = models.ForeignKey(Space)
class Food(models.Model):
name = models.CharField(max_length=128)
description = models.TextField(default='', blank=True)
space = models.ForeignKey(Space)
和赛程:
class SpaceFactory(factory.django.DjangoModelFactory):
name = factory.LazyAttribute(lambda x: faker.word())
class FoodFactory(factory.django.DjangoModelFactory):
name = factory.LazyAttribute(lambda x: faker.sentence(nb_words=3))
description = factory.LazyAttribute(lambda x: faker.sentence(nb_words=10))
space = factory.SubFactory(SpaceFactory)
class ShoppingListEntryFactory(factory.django.DjangoModelFactory):
food = factory.SubFactory(FoodFactory, space=factory.SelfAttribute('..space'))
space = factory.SubFactory(SpaceFactory)
并测试
register(SpaceFactory, 'space_1')
register(ShoppingListEntryFactory, 'shopping_list_entry', space=LazyFixture('space_1'))
def test_list_space(shopping_list_entry):
assert 1 == 1
抛出以下错误
Failed with Error: [undefined]failed on setup with
def test_list_space(sle_1):
file <string>, line 2: source code not available
file <string>, line 2: source code not available
E fixture 'food__name' not found
我正在努力找出解决此问题的方法。
用 pytest fixture 替换寄存器工厂 register(ShoppingListEntryFactory, ...)
解决了这个问题。
@pytest.fixture
def shopping_list_entry(space_1):
return ShoppingListEntryFactory.create(space=space_1)
我在构建带有子工厂的工厂来测试 Django 模型时出错。 使用型号:
class Space(ExportModelOperationsMixin('space'), models.Model):
name = models.CharField(max_length=128, default='Default')
class ShoppingListEntry(models.Model):
food = models.ForeignKey(Food)
space = models.ForeignKey(Space)
class Food(models.Model):
name = models.CharField(max_length=128)
description = models.TextField(default='', blank=True)
space = models.ForeignKey(Space)
和赛程:
class SpaceFactory(factory.django.DjangoModelFactory):
name = factory.LazyAttribute(lambda x: faker.word())
class FoodFactory(factory.django.DjangoModelFactory):
name = factory.LazyAttribute(lambda x: faker.sentence(nb_words=3))
description = factory.LazyAttribute(lambda x: faker.sentence(nb_words=10))
space = factory.SubFactory(SpaceFactory)
class ShoppingListEntryFactory(factory.django.DjangoModelFactory):
food = factory.SubFactory(FoodFactory, space=factory.SelfAttribute('..space'))
space = factory.SubFactory(SpaceFactory)
并测试
register(SpaceFactory, 'space_1')
register(ShoppingListEntryFactory, 'shopping_list_entry', space=LazyFixture('space_1'))
def test_list_space(shopping_list_entry):
assert 1 == 1
抛出以下错误
Failed with Error: [undefined]failed on setup with
def test_list_space(sle_1):
file <string>, line 2: source code not available
file <string>, line 2: source code not available
E fixture 'food__name' not found
我正在努力找出解决此问题的方法。
用 pytest fixture 替换寄存器工厂 register(ShoppingListEntryFactory, ...)
解决了这个问题。
@pytest.fixture
def shopping_list_entry(space_1):
return ShoppingListEntryFactory.create(space=space_1)