将测试文件转换为包含__init__.py的包的问题

时间:2015-10-19 01:52:46

标签: python django

我正在关注本教程:

http://www.marinamele.com/taskbuster-django-tutorial/create-home-page-with-tdd-staticfiles-templates-settings

当我跑步时:

(tb_test)metersk:functional_tests metersky$ python test_all_users.py
.
----------------------------------------------------------------------
Ran 1 test in 2.313s

OK

测试按预期工作。测试是:

from selenium import webdriver
import unittest


class NewVisitorTest(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(3)

    def tearDown(self):
        self.browser.quit()

    def test_it_worked(self):
        self.browser.get('http://localhost:8000')
        self.assertIn('Welcome to Django', self.browser.title)

if __name__ == '__main__':
    unittest.main(warnings='ignore')

但是,当我按照步骤将空白__init__.py文件添加到目录中时,我会像这样运行测试:python manage.py test functional_tests

我遇到以下失败:

Traceback (most recent call last):
  File "/Users/metersky/code/taskbuster_project/taskbuster/settings/base.py", line 20, in get_env_variable
    return os.environ[var_name]
  File "/Users/metersky/.virtualenvs/tb_test/bin/../lib/python3.4/os.py", line 633, in __getitem__
    raise KeyError(key) from None
KeyError: 'SECRET_KEY'


  File "/Users/metersky/code/taskbuster_project/taskbuster/settings/__init__.py", line 2, in <module>
    from .base import *
  File "/Users/metersky/code/taskbuster_project/taskbuster/settings/base.py", line 31, in <module>
    SECRET_KEY = get_env_variable('SECRET_KEY')
  File "/Users/metersky/code/taskbuster_project/taskbuster/settings/base.py", line 23, in get_env_variable
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: set the SECRET_KEY environment variable

我不太明白如何以不同于命令行的方式调用测试脚本会阻止程序查找密钥环境变量。当我自己调用脚本时,它显然有效。

文件夹结构:

├── TaskBuster.sublime-project
├── TaskBuster.sublime-workspace
├── db.sqlite3
├── functional_tests
│   ├── __init__.py
│   └── test_all_users.py
├── manage.py
├── requirements
│   ├── base.txt
│   ├── development.txt
│   ├── production.txt
│   └── testing.txt
└── taskbuster
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-34.pyc
    │   ├── settings.cpython-34.pyc
    │   ├── urls.cpython-34.pyc
    │   └── wsgi.cpython-34.pyc
    ├── db.sqlite3
    ├── settings
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-34.pyc
    │   │   ├── base.cpython-34.pyc
    │   │   └── development.cpython-34.pyc
    │   ├── base.py
    │   ├── development.py
    │   ├── production.py
    │   ├── staging.py
    │   └── testing.py
    ├── static
    │   ├── css
    │   │   ├── bootstrap-theme.css
    │   │   ├── bootstrap-theme.css.map
    │   │   ├── bootstrap-theme.min.css
    │   │   ├── bootstrap.css
    │   │   ├── bootstrap.css.map
    │   │   ├── bootstrap.min.css
    │   │   └── main.css
    │   ├── favicon.ico
    │   ├── fonts
    │   │   ├── glyphicons-halflings-regular.eot
    │   │   ├── glyphicons-halflings-regular.svg
    │   │   ├── glyphicons-halflings-regular.ttf
    │   │   └── glyphicons-halflings-regular.woff
    │   ├── img
    │   └── js
    │       ├── main.js
    │       └── vendor
    │           ├── bootstrap.js
    │           ├── bootstrap.min.js
    │           ├── jquery-1.11.2.min.js
    │           ├── modernizr-2.8.3-respond-1.4.2.min.js
    │           └── npm.js
    ├── templates
    │   ├── 404.html
    │   ├── base.html
    │   ├── humans.txt
    │   └── robots.txt
    ├── urls.py
    └── wsgi.py

0 个答案:

没有答案