示例模型.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)