小编Pao*_*olo的帖子

Django 单元测试 - 创建对象的 ID

示例模型.py

模型.py

class Food(models.Model):
    name = models.CharField(max_length=50, verbose_name='Food')

    def __str__(self):
        return self.name
Run Code Online (Sandbox Code Playgroud)

假设我已经编写了单元测试:

from django.test import TestCase
from myapp.models import Food

class TestWhateverFunctions(TestCase):
    """
    This class contains tests for whatever functions.
    """

    def setUp(self):
        """
        This method runs before the execution of each test case.
        """
        Food.objects.create(name='Pizza') # Will the created object have id of 1?
        Food.objects.create(name='Pasta') # Will the created object have id of 2?

    def test_if_food_is_pizza(self):
        """
        Test if the given food is pizza.
        """
        food = …
Run Code Online (Sandbox Code Playgroud)

python django django-unittest

4
推荐指数
1
解决办法
4321
查看次数

标签 统计

django ×1

django-unittest ×1

python ×1