简体   繁体   English

Django 使用默认数据库进行单元测试而不是测试数据库

[英]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

Additional Notes补充说明

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.

相关问题 Django数据库路由器将表发送到默认数据库,而不是正确的表 - Django DB Router Sending Table to default DB instead of Correct One 是否需要在单元测试用例中更改数据库-python django - Is need to change DB in unit test cases - python django Django客户端测试(获取,发布)响应从主数据库加载数据,而不是从测试数据库加载夹具 - Django client test (get, post) response load data from main db instead fixtures loaded in test db FactoryBoy 正在访问普通数据库而不是测试数据库 - FactoryBoy is accessing normal DB instead of TEST DB Django单元测试的数据库权限 - DB Permissions with Django unit testing Django app:由于django.db.utils.IntegrityError,单元测试失败 - Django app : unit tests fails because of django.db.utils.IntegrityError Flask 单元测试——模拟 Aerospike DB - Flask unit tests -- mocking up Aerospike DB Heroku不允许我重命名数据库,因此无法运行Django单元测试? - Heroku won't allow me to rename DB and thus can't run Django unit tests? Django 参数化测试的 Django_db 标记 - Django_db mark for django parametrized tests ProgrammingError:(1146,“表&#39;test_ <DB> 。 <TABLE> &#39;不存在&#39;)运行Django的单元测试时 - ProgrammingError: (1146, “Table 'test_<DB>.<TABLE>' doesn't exist”) when running unit test for Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM