运行测试时出现“请求的设置,但未配置设置”错误

时间:2019-04-15 19:51:20

标签: python django python-3.x unit-testing testing

我为每个环境(开发,测试和生产)设置了一个不同的设置文件,所有这些文件都继承自基本文件。我的配置看起来像这样

myproject
    settings
        base.py
        dev.py
        test.py
        prod.py

在manage.py中,我有

def main():
    if 'test' in sys.argv:
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings.test")
    elif os.environ['DJANGO_DEVELOPMENT'] == 'true':
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings.dev")
    else:
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings.prod")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

但是当我跑步

python -m unittest discover

我明白了

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/__main__.py", line 18, in <module>
    main(module=None)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 100, in __init__
    self.parseArgs(argv)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 124, in parseArgs
    self._do_discovery(argv[2:])
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 244, in _do_discovery
    self.createTests(from_discovery=True, Loader=Loader)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 154, in createTests
    self.test = loader.discover(self.start, self.pattern, self.top)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 347, in discover
    tests = list(self._find_tests(start_dir, pattern))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 412, in _find_tests
    yield from self._find_tests(full_path, pattern, namespace)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 412, in _find_tests
    yield from self._find_tests(full_path, pattern, namespace)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 404, in _find_tests
    full_path, pattern, namespace)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 458, in _find_test_path
    return self.loadTestsFromModule(module, pattern=pattern), False
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 123, in loadTestsFromModule
    if isinstance(obj, type) and issubclass(obj, case.TestCase):
  File "/Users/davea/Documents/workspace/frontpage_project/venv/lib/python3.7/site-packages/django/utils/functional.py", line 213, in inner
    self._setup()
  File "/Users/davea/Documents/workspace/frontpage_project/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 42, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested settings, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

我该怎么做才能正确调用我的测试?

0 个答案:

没有答案