Django测试数据库不会自动刷新 [英] Django test database not auto-flushing

查看:332
本文介绍了Django测试数据库不会自动刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆单元测试文件,所有这些文件都由django.test.TestCase类组成。

I have a bunch of unit test files, all of which consist of django.test.TestCase classes.

写了一个shell脚本来取消注释/评论文件导入我的__init__.py文件,所以我可以运行测试从某些测试文件,基于命令行参数我给它。我也可以一次性运行所有测试文件的所有测试(用于回归测试)

Wrote myself a little shell script to uncomment/comment test file imports in my __init__.py file, so I can run tests from certain test files, based off the command line arguments I give it. I am also able to run all the tests of all the test files in one go (for regression testing purposes).

我有一个测试文件,有一些JSON灯具并且第一个测试检查某个模型/表中有3个记录(由JSON fixture定义)。

I have this one test file that has some JSON fixtures and the first test checks that a certain model/table has 3 records in it (defined by the JSON fixture).

所以这里是问题:当我运行这个测试文件自身的测试通过了飞行的颜色,但是当我运行测试与所有其他测试,我提到的特定测试用例失败。

So here is the problem: when I run this test file on its own its tests pass with flying colours, but when I run the test with all other tests, that particular test case I mentioned, fails.

当我运行所有测试中,数据库表示表/模型中有6条记录,但应该只有3个(从夹具),就像当测试文件本身运行时一样。

When I run all the tests, the database says there are 6 records in the table/model, but there should only be 3 (from the fixture), like when the test file is run by itself.

我也尝试使用其他几个测试文件(不是全部)运行该测试文件,并通过。所以唯一的时候它不是,当所有的测试文件运行时。

I also tried running the that test file with a few other test files (not all) and it passes. So the only time it doesn't, is when all the test files are run.

对我来说,这似乎是一个Django或PostgreSQL(我正在使用的DB)中的错误,因为Django TestCases不应该在每个测试方法之间自动刷新/重置数据库,更不用说测试类?

To me this seems like a bug in Django or PostgreSQL (DB I am using), because aren't Django TestCases supposed to auto-flush/reset the database between each test method, let alone test class?

推荐答案

这可能是由于Django中TestCase和TransactionTestCase之间的清理功能有所不同。在Django 1.5之前,需要在TestCases之后运行TransactionTestCases(并且Djangos testunner为您执行)。这应该是固定在1.5 ,所以尝试再次运行测试...

This is likely due to the difference in how cleanup is done between TestCase and TransactionTestCase in Django. Before Django 1.5 TransactionTestCases needed to be run after TestCases (and Djangos testunner did that for you). This should be fixed in 1.5 though, so try running your tests again there...

这篇关于Django测试数据库不会自动刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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