[英]Django using default db for unit tests instead of test db
When I run manage.py test
django is using my default database instead of a test one.当我运行
manage.py test
django 时使用的是我的默认数据库而不是测试数据库。
Am I misunderstanding something from the docs?我是否误解了文档中的某些内容?
According to the docs , the default behaviour is that, "separate, blank databases are created for the tests" .根据文档,默认行为是“为测试创建单独的空白数据库” 。
I set the "TEST: NAME" value just to make sure, but the docs state that's only needed if I want to configure the test database name instead of the default test_{dbname}
我设置“TEST:NAME”值只是为了确保,但只有在我想配置测试数据库名称而不是默认的
test_{dbname}
时才需要文档 state
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"HOST": "0.0.0.0",
"NAME": "mydb_db",
"USER": "postgres",
"PASSWORD": "postgres",
"PORT": 5432,
"TEST": {"NAME": "test_mydb_db"},
}
}
from unittest import TestCase
class SimpleTest(TestCase):
def test_get_user_authorized(self):
client = Client()
breakpoint()
>>> (Pdb) from django.db import connection
>>> (Pdb) connection.settings_dict['NAME']
'mydb_db'
If I read or create data in the unit test, data is from mydb_db
and not test_mydb_db
as I expected.如果我在单元测试中读取或创建数据,数据来自
mydb_db
而不是我预期的test_mydb_db
。
Database is being setup using docker compose, but I don't think that should affect anything:正在使用 docker compose 设置数据库,但我认为这不会影响任何事情:
services:
db:
container_name: postgres
image: postgres:9.6
restart: always
volumes:
- postgres_data:/var/lib/postgresql/data/
ports:
- "5432:5432"
environment:
POSTGRES_DB: mydb_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
PGPASSWORD: postgres
Answering my own question here.在这里回答我自己的问题。 Figured it out right after posting it.
发帖后马上想通了。 Looks like I missed an important point from the docs
看起来我错过了文档中的一个重要点
SimpleTestCase
must inherit from django.test.TestCase
and not unittest.TestCase
for the test database to be properly created. SimpleTestCase
必须继承自django.test.TestCase
而不是unittest.TestCase
才能正确创建测试数据库。
Changing my code to this solved it:将我的代码更改为此解决了它:
from django.test import TestCase
class SimpleTest(TestCase):
def test_get_user_authorized(self):
client = Client()
breakpoint()
>>> (Pdb) from django.db import connection
>>> (Pdb) connection.settings_dict['NAME']
'test_mydb_db'
Docs are not explicitly stating that using unittest will not create a test db, but it does recommend using django's TestCase if db is needed: 文档没有明确说明使用 unittest 不会创建测试数据库,但如果需要 db,它确实建议使用 django 的 TestCase:
If your tests rely on database access such as creating or querying models, be sure to create your test classes as subclasses of django.test.TestCase rather than unittest.TestCase.
如果您的测试依赖于数据库访问,例如创建或查询模型,请务必将您的测试类创建为 django.test.TestCase 而不是 unittest.TestCase 的子类。
Using unittest.TestCase avoids the cost of running each test in a transaction and flushing the database, but if your tests interact with the database their behavior will vary based on the order that the test runner executes them.
使用 unittest.TestCase 避免了在事务中运行每个测试并刷新数据库的成本,但是如果您的测试与数据库交互,它们的行为将根据测试运行器执行它们的顺序而有所不同。 This can lead to unit tests that pass when run in isolation but fail when run in a suite.
这可能导致单元测试在单独运行时通过,但在套件中运行时失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.