Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 工厂男孩的多对多关系?_Unit Testing_Django Testing_Factory Boy - Fatal编程技术网

Unit testing 工厂男孩的多对多关系?

Unit testing 工厂男孩的多对多关系?,unit-testing,django-testing,factory-boy,Unit Testing,Django Testing,Factory Boy,我试图使用factory_boy测试两个Django模型之间的多对多关系。工厂文档似乎没有讨论这个问题,我很难找出我做错了什么。当我运行第一个测试时,我得到错误“AttributeError:‘Pizza’对象没有属性‘topping’”。我在第二次测试中得到了类似的错误 当我在调试器中运行测试时,我可以看到一个“toppings”对象,但它不知道如何从中获取名称。我是否正确定义了PizzaFactory的准备方法?当存在多对多关系时,如何从另一个表访问一个表中的名称 谢谢 models.py:

我试图使用factory_boy测试两个Django模型之间的多对多关系。工厂文档似乎没有讨论这个问题,我很难找出我做错了什么。当我运行第一个测试时,我得到错误“AttributeError:‘Pizza’对象没有属性‘topping’”。我在第二次测试中得到了类似的错误

当我在调试器中运行测试时,我可以看到一个“toppings”对象,但它不知道如何从中获取名称。我是否正确定义了PizzaFactory的准备方法?当存在多对多关系时,如何从另一个表访问一个表中的名称

谢谢

models.py: factories.py: tests.py
我相信您需要使用
@工厂。后一代
装饰器:

class PizzaFactory(factory.Factory):
    name = 'Vegetarian'

    @factory.post_generation
    def toppings(self, create, extracted, **kwargs):
        if not create:
            # Simple build, do nothing.
            return

        if extracted:
            # A list of groups were passed in, use them
            for topping in extracted:
                self.toppings.add(topping)
然后在tests.py
pizza=PizzaFactory.create中调用它(toppings=(topping1、topping2、tooping3))


从。

只需使用混合器即可:

from mixer.backend.django import mixer

# Generate toppings randomly
pizza = mixer.blend(Pizza, toppings=mixer.RANDOM)

# Set toppings
toppings = mixer.cycle(3).blend(Topping)
pizza = mixer.blend(Pizza, toppings=toppings)

# Generate toppings with name=tomato
pizze = mixer.blend(Pizza, toppings__name='tomato')

简单、可配置、更快、无模式、声明式,而且您在一些测试中得到了您想要的。

支持轶事--为什么我选择了
混音器
。。。最初,我被工厂男孩吸引。我喜欢在
factory\u boy
中,对于给定的
factory
您可以调用一个方法
attributes()
并获得一个
dict
返回,非常适合与一起使用。但是,
混合器的好处在于它可以帮助你保持干燥,因为它可以在飞行中做很多事情。这意味着,如果您的测试代码在将来发生更改,那么需要更改的测试代码就更少了!你们还支持吗?
from django.test import TestCase
import factory
from app.models import Topping, Pizza
from app.factories import ToppingFactory, PizzaFactory

class FactoryTestCase(TestCase):

    def test_pizza_has_mushrooms(self):
        pizza = PizzaFactory()
        self.assertTrue(pizza.topping.name, 'mushrooms')

    def test_mushrooms_on_pizza(self):
        topping = ToppingFactory()
        self.assertTrue(topping.pizza.name, 'Vegetarian')
class PizzaFactory(factory.Factory):
    name = 'Vegetarian'

    @factory.post_generation
    def toppings(self, create, extracted, **kwargs):
        if not create:
            # Simple build, do nothing.
            return

        if extracted:
            # A list of groups were passed in, use them
            for topping in extracted:
                self.toppings.add(topping)
from mixer.backend.django import mixer

# Generate toppings randomly
pizza = mixer.blend(Pizza, toppings=mixer.RANDOM)

# Set toppings
toppings = mixer.cycle(3).blend(Topping)
pizza = mixer.blend(Pizza, toppings=toppings)

# Generate toppings with name=tomato
pizze = mixer.blend(Pizza, toppings__name='tomato')