0

我在创建测试数据库时遇到问题,并且由于找不到数据库而出错。我真的不确定出了什么问题以及为什么没有创建数据库

pytest.ini

[pytest]
DJANGO_SETTINGS_MODULE=testing.test_settings
addopts = --nomigrations --cov=. --cov-report=html

test_settings.py

from settings import *

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3_testing'),
        }
    }

在我的测试中,我运行命令,py.test --reuse-db但我仍然没有找到创建的 db

测试.py

@pytest.mark.django_db
class TestUsers:

    def test_user(self,client):
        response = client.get("/test", follow=True)
        assert len(response.context['list'])==1

这会抛出我 AssertionError 并且错误是 在此处输入图像描述

创建测试数据库的任何帮助都会有所帮助

4

1 回答 1

1

我不确定 pytest.ini 是否会传递环境变量。试试这样跑

DJANGO_SETTINGS_MODULE=testing.test_settings py.test --reuse-db

另外我希望您知道创建的数据库的名称是像字典一样生成'test_' + DATABASES['NAME']或取自TEST 字典

在您的屏幕截图中,失败的测试是test_with_client,但您正在向我们展示test_user测试的代码。

于 2016-06-24T09:23:00.810 回答