在测试运行时,Django测试数据库看起来是空的 [英] Django Test Database looks empty while test is runnin

查看:402
本文介绍了在测试运行时,Django测试数据库看起来是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用本地PostgreSQL服务器的Django项目。
我正在使用调试器来调试我有的奇怪的错误。当调试器在其中一个测试中停止时,我尝试查看测试数据库。我看到这个新的数据库(test_project1),所有的方案都是按照它们定义的。但是所有的表都是空的。



我知道表不是空的:
- 我使用fixture,一些测试已经运行并返回数据。
- 后测试创建新用户返回201状态码。



但是,当我尝试使用pgAdmin3或psql访问数据库时,我看不到任何数据。



任何想法这里发生了什么?
Django是否使用某种复杂的现金机制?

解决方案

Django的 TestCase 将每个测试包装在自己的交易中 。所以你的数据库没有在你通过ORM发出请求时被使用。


I have a Django project that uses local PostgreSQL server. I'm using a debugger to debug some weird bugs that I have. While the debugger is stopped on one of the tests, I tried to look at the test database. I see this new database (test_project1) and all the schemes are defined as they should be. But all tables are empty.

I know that the tables are not empty: - I used fixture and some tests already ran and returned data. - A post test created new user returned 201 status code.

And yet, I see no data when I try to access the database with pgAdmin3 or psql.

Any idea what is going on here? Is there some kind of sophisticated cash mechanism that Django uses?

解决方案

Django's TestCase is wrapping every test in its own transaction. So your database is not being used any time you do a request through ORM.

这篇关于在测试运行时,Django测试数据库看起来是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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