【发布时间】:2015-01-12 14:17:14
【问题描述】:
使用以下代码时,只有第二个测试用例会通过(因为它是先执行的),其余的会失败。
models.py
from django.db import models
class Application(models.Model):
title = models.CharField(max_length=50)
description = models.CharField(max_length=160)
url = models.URLField(max_length=255)
test.py
from django.test import TestCase
from application.models import Application
class ApplicationTests(TestCase):
def setUp(self):
testApplication = Application(
title="Application Title",
description="Application Description",
url="http://www.application-url.com"
)
testApplication.save()
def test_application_has_title(self):
application = Application.objects.get(pk=1)
self.assertEqual(application.title, "Application Title")
def test_application_has_description(self):
application = Application.objects.get(pk=1)
self.assertEqual(application.description, "Application Description")
def test_application_has_url(self):
application = Application.objects.get(pk=1)
self.assertEqual(application.url, "http://www.application-url.com")
对我来说,似乎在第一次测试后该对象已从数据库中删除,但这不应该发生。我对 Django 很陌生,因此非常感谢您提供任何帮助。
【问题讨论】:
标签: python django unit-testing tdd