在Django项目上进行Pybuilder集成测试

时间:2021-04-30 15:39:39

I'm trying to write integration tests with pybuilder on a Django web application, but I can't seem to make it work. An example error which occurs (from within the Django application):

我正在尝试在Django Web应用程序上使用pybuilder编写集成测试,但我似乎无法使其工作。发生的示例错误(来自Django应用程序内):

django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

DJANGO_SETTINGS_MODULE is defined in build.py, like so:

DJANGO_SETTINGS_MODULE在build.py中定义,如下所示:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")

The project is stored in src/main/python and integration tests are in src/integrationtest/python, like pybuilder's docs say it should be.

该项目存储在src / main / python中,集成测试在src / integrationtest / python中,就像pybuilder的docs所说的那样。

I've tried to manually set DJANGO_SETTINGS_MODULE in a lot of different places, including build.py and the test case itself. The same applies for settings.configure(). But it simply doesn't want to work and I don't know what I'm missing. I've ran out of ideas what to try.

我试图在很多不同的地方手动设置DJANGO_SETTINGS_MODULE,包括build.py和测试用例本身。这同样适用于settings.configure()。但它根本不想工作,我不知道我错过了什么。我已经没有想法要尝试什么了。

1 个解决方案

#1


1  

I've added os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") to the initialize function and I'm no longer getting this error.

我已经将os.environ.setdefault(“DJANGO_SETTINGS_MODULE”,“app.settings”)添加到初始化函数中,我不再收到此错误。

Full build.py listing:

完整的build.py列表:

from pybuilder.core import init, use_plugin
import os

use_plugin("python.core")
use_plugin("python.unittest")
use_plugin("python.django")

default_task = "publish"


@init
def initialize():
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

#1


1  

I've added os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") to the initialize function and I'm no longer getting this error.

我已经将os.environ.setdefault(“DJANGO_SETTINGS_MODULE”,“app.settings”)添加到初始化函数中,我不再收到此错误。

Full build.py listing:

完整的build.py列表:

from pybuilder.core import init, use_plugin
import os

use_plugin("python.core")
use_plugin("python.unittest")
use_plugin("python.django")

default_task = "publish"


@init
def initialize():
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")