与其他测试用例一起运行时,Django单元测试失败 [英] Django unit tests failing when run with other test cases

查看:101
本文介绍了与其他测试用例一起运行时,Django单元测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与Django单元测试的行为不一致。在使用sqlite的开发计算机上,如果我分别在两个应用程序上运行测试,则测试通过,但是如果我运行 manage.py test 一次测试所有内容,则开始在两个测试上,单元测试始终失败。

I'm getting inconsistent behavior with Django unit tests. On my development machine using sqlite, if I run tests on my two apps separately the tests pass, but if I run manage.py test to test everything at once, I start getting unit test failures consistently on two tests.

在使用Postgres的登台服务器上,我有一个特殊的测试可以单独测试(例如, manage.py test MyApp.tests.MyTestCase.testSomething ),但在运行整个测试用例(例如 manage.py test MyApp.tests.TestCase )。

On my staging server which uses Postgres, I have a particular test that works when testing it individually (e.g. manage.py test MyApp.tests.MyTestCase.testSomething), but fails when running the entire test case (e.g. manage.py test MyApp.tests.TestCase).

其他与StackOverflow相关的问题似乎有两个解决方案:

Other related StackOverflow questions seem to have two solutions:


  1. 使用Django TestCase,而不是等效的Python

  2. 使用TransactionTestCase,以确保每次测试后都能正确清理数据库。

我都尝试都无济于事。出于沮丧,我也尝试使用django-nose代替,但是我看到了相同的错误。我使用的是Django 1.6。

I've tried both to no avail. Out of frustration, I also tried using django-nose instead, but I was seeing the same errors. I'm on Django 1.6.

推荐答案

今天,通过一系列测试,我发生了同样的事情。我有23个常规 django.test.TestCase 测试,然后有一个 django.contrib.staticfiles.testing.StaticLiveServerTestCase 测试。那是最终的测试,当与其他测试一起运行时,总是会失败,但会自行通过。

I had the same thing happening today with a series of tests. I had 23 regular django.test.TestCase tests and then one django.contrib.staticfiles.testing.StaticLiveServerTestCase test. It was that final test that would always fail when ran with the rest of them but pass on its own.

在23个常规 TestCase 测试中,我确实实现了常规 TestCase 的子类,因此我可以提供特定于我的应用程序进行测试的一些常见功能。在 tearDown 方法中,我未能调用super方法。一旦我在 tearDown 方法中调用了super方法,它就起作用了。因此,这里的课程是检查以确保您正在清理方法。

In the 23 regular TestCase tests I really had implemented a subclass of the regular TestCase so that I could provide some common functionality specific to my application to the tests. In the tearDown methods I had failed to call the super method. Once I called the super method in the tearDown method, it worked. So the lesson here was to check to make sure you are cleaning up your methods.

这篇关于与其他测试用例一起运行时,Django单元测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆