在 django 单元测试中使用用户模型的问题

发布于 2024-09-01 07:45:46 字数 525 浏览 6 评论 0原文

我有以下 django 测试用例,它给了我错误:

class MyTesting(unittest.TestCase):
    def setUp(self):
        self.u1 = User.objects.create(username='user1')
        self.up1 = UserProfile.objects.create(user=self.u1)

    def testA(self):
        ...

    def testB(self):
        ...

当我运行测试时, testA 将成功通过,但在 testB 启动之前,我收到以下错误:

IntegrityError: column username is not unique

很明显它试图在每个测试用例之前创建 self.u1 并发现它已经存在于数据库中。如何在每个测试用例之后正确清理它,以便后续用例正确运行?

I have the following django test case that is giving me errors:

class MyTesting(unittest.TestCase):
    def setUp(self):
        self.u1 = User.objects.create(username='user1')
        self.up1 = UserProfile.objects.create(user=self.u1)

    def testA(self):
        ...

    def testB(self):
        ...

When I run my tests, testA will pass sucessfully but before testB starts, I get the following error:

IntegrityError: column username is not unique

It's clear that it is trying to create self.u1 before each test case and finding that it already exists in the Database. How do I get it to properly clean up after each test case so that subsequent cases run correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

八巷 2024-09-08 07:45:46

setUptearDown 调用 Unittest 上的方法每个测试用例之前和之后。定义 tearDown 方法删除创建的用户。

class MyTesting(unittest.TestCase):
    def setUp(self):
        self.u1 = User.objects.create(username='user1')
        self.up1 = UserProfile.objects.create(user=self.u1)

    def testA(self):
        ...

    def tearDown(self):
        self.up1.delete()
        self.u1.delete()

我还建议创建用户配置文件 使用 post_save 信号,除非您确实想为每个用户手动创建用户配置文件。

删除评论的后续

来自 Django 文档

当 Django 删除一个对象时,它
模拟 SQL 的行为
ON DELETE CASCADE 约束 -- 在
换句话说,任何具有
指向对象的外键
被删除
将同时被删除
它。

在您的情况下,用户配置文件指向用户,因此您应该先删除用户,同时删除配置文件。

setUp and tearDown methods on Unittests are called before and after each test case. Define tearDown method which deletes the created user.

class MyTesting(unittest.TestCase):
    def setUp(self):
        self.u1 = User.objects.create(username='user1')
        self.up1 = UserProfile.objects.create(user=self.u1)

    def testA(self):
        ...

    def tearDown(self):
        self.up1.delete()
        self.u1.delete()

I would also advise to create user profiles using post_save signal unless you really want to create user profile manually for each user.

Follow-up on delete comment:

From Django docs:

When Django deletes an object, it
emulates the behavior of the SQL
constraint ON DELETE CASCADE -- in
other words, any objects which had
foreign keys pointing at the object to
be deleted
will be deleted along with
it.

In your case, user profile is pointing to user so you should delete the user first to delete the profile at the same time.

月亮邮递员 2024-09-08 07:45:46

如果您希望 django 在每次测试运行后自动刷新测试数据库,那么您应该扩展 django.test.TestCase,而不是 django.utils.unittest.TestCase (因为您目前正在做)。

最好在每次测试后转储数据库,这样您就可以额外确保测试是一致的,但请注意,由于这种额外的开销,您的测试将运行得更慢。

请参阅“编写测试”Django 文档中的警告部分

If you want django to automatically flush the test database after each test is run then you should extend django.test.TestCase, NOT django.utils.unittest.TestCase (as you are doing currently).

It's good practice to dump the database after each test so you can be extra-sure you're tests are consistent, but note that your tests will run slower with this additional overhead.

See the WARNING section in the "Writing Tests" Django Docs.

各空 2024-09-08 07:45:46

准确地说,setUp 的存在就是为了在每个测试用例之前运行一次。

相反的方法,即在每个测试用例之后运行一次的方法,名为tearDown:这是您删除self.u1等的地方(大概是通过只需调用 self.u1.delete(),除非除了删除对象之外还有补充的专门清理要求)。

Precisely, setUp exists for the very purpose of running once before each test case.

The converse method, the one that runs once after each test case, is named tearDown: that's where you delete self.u1 etc (presumably by just calling self.u1.delete(), unless you have supplementary specialized clean-up requirements in addition to just deleting the object).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文