Python 第一次测试成功后,Django中的后续测试失败

Python 第一次测试成功后,Django中的后续测试失败,python,django,unit-testing,tdd,Python,Django,Unit Testing,Tdd,当使用以下代码时,只有第二个测试用例将通过(因为它是第一个执行的),其余的将失败 型号.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) f

当使用以下代码时,只有第二个测试用例将通过(因为它是第一个执行的),其余的将失败

型号.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)
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")
test.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)
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的新手,因此非常感谢在这方面的任何帮助。

实际上,根据设计,每个测试都是独立运行的,因此每次测试后都会重新初始化DB;我不知道是否存在某个DB每次使用不同PK的问题,但无论是哪种情况,我都建议将.get(PK=1)与.all()[0]

进行交换。可能是
testApplication
没有1的PK,您可以尝试以下操作:

class ApplicationTests(TestCase):

    def setUp(self):
        self.testApplication = Application.objects.create(
            title="Application Title",
            description="Application Description",
            url="http://www.application-url.com"
        )

    def test_application_has_url(self):
        application = Application.objects.get(pk=self.testApplication.id)
        self.assertEqual(application.url, "http://www.application-url.com")

这样做似乎可以使前两个测试正常工作,但最后一个测试test_application_has_url(),仍然失败。“DoesNotExist:应用程序匹配查询不存在。”如果注释掉前两个测试(或只是重命名,使它们不以测试开头),第三个测试是否有效?