django.core.exceptions在python

时间:2016-06-27 16:51:00

标签: python django selenium data-migration

在我收到几乎相同的错误信息之前,我遇到了类似的问题,即

C:\Users\David J\optilab\optilab_project>python functional_tests.py
Traceback (most recent call last):
  File "functional_tests.py", line 4, in <module>
    from optilab.models import Design
  File "C:\Users\David J\optilab\optilab_project\optilab\models.py", line 10, in <module>
    class Design(models.Model):
  File "C:\Users\David J\optilab\optilab_project\optilab\models.py", line 11, in Design
    name = models.CharField(max_length=30)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1072, in __init__
    super(CharField, self).__init__(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 166, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 55, in __getattr__
    self._setup(name)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 41, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

但它正在制作,因为我从shell中错误地调用了我的测试。现在我正在尝试运行一个功能测试,在成功使用该命令之前,我已经做了好几次  "python functional_tests.py" 在我的项目文件夹中 - 但它产生上述错误消息。任何人都可以告诉我我做错了什么,或者为什么这可能以前有效但现在不行?感谢。

编辑: 这是functional_tests.py:

from selenium import webdriver
import unittest
from selenium.webdriver.common.keys import Keys
from optilab.models import Design
from automa.api import *

class HomeTest(unittest.TestCase):  

    def setUp(self):  
        self.browser = webdriver.Firefox()
        self.browser.get('http://localhost:8000/lab')
        self.new_design = self.browser.find_element_by_id("new_design")
        self.load_design = self.browser.find_element_by_id("load_test")
        self.design_name = self.browser.find_element_by_id("design_name")
        self.browser.implicitly_wait(3)

    def test_latest_designs_displayed_in_table(self):
        # 

        self.browser.implicitly_wait(3)
        self.design_table = self.browser.find_element_by_id("design_table")
        self.rows = self.browser.find_elements_by_tag_name('tr')
        self.assertTrue(
            any(row.text == 'My First Design' for row in rows),
            "New design did not appear in test table."
        )

    def test_entering_text_activates_new(self):
        # user enters name of design.  new becomes clickable.            
        self.browser.implicitly_wait(3)
        self.design_name.send_keys("sample_design")

    def test_new_test_can_be_saved_and_retrieved(self):
        name = "sample design"
        self.design_name.send_keys(name)
        self.new_design.click()
        self.browser.implicitly_wait(3)
        self.saved_design = Design.objects.get(name=design_name)

    def test_entering_name_of_previously_saved_test_activates_load(self): 
        # user enters the name of a previously saved test.  load button becomes clickable.
        design = Design(name="sample_design")
        saved_design = Design.objects.get(id=1).name
        self.browser.implicitly_wait(3)
        self.design_name.send_keys(saved_design)
        self.load_design.click()

    def test_entering_name_of_previously_saved_test_but_clicking_new_prompts_overwrite(self):
        # user entes name of saved test but clicks new.  dialogue box asks if test should be overwritten.
        previously_saved_design = Design.objects.get(id=1).name
        self.design_name.send_keys(previously_saved_design)                    

    def test_clicking_save_redirects_to_model(self):
        pass

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



if __name__ == '__main__':  

unittest.main()

2 个答案:

答案 0 :(得分:1)

脚本的开头看起来应该是这样的

import os, sys
sys.path.append(os.getcwd())
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "optilab.settings") # or whatever
import django
django.setup()


from selenium import webdriver
import unittest
from selenium.webdriver.common.keys import Keys
from optilab.models import Design
from automa.api import *  # try not to do this

答案 1 :(得分:0)

这是因为在尝试调用设置之前没有正确设置Django,如果没有django.setup()变量,则无法知道应该使用哪些项目设置,manage.py test调用对于导入是必不可少的模型和访问数据库。 (有关该方法,请参阅e4c5's answer

但是,这不是推荐的方法或最简单的方法。您可以像manage.py test中的单元测试一样调用功能测试,Django将为您自动设置所有内容,它甚至可以使用tempdb进行功能测试。如果你的功能测试包括功能测试中的插入和更新(你应该),tempdb会派上用场。

要使用此功能,您必须更改代码中的一些内容。

首先是文件名的问题,-pdoc)的默认模式是&#34; test * .py&#34;,您可以使用{{1}更改此内容}或--pattern但通常的解决方案是将functional_tests.py重命名为tests.py并将其放在functional_tests文件夹中。这样,您可以通过调用manage.py test functional_tests来调用功能测试。

此外,您应该考虑使用TestCasedjango.testdoc)中的LiveServerTestCasedoc)而不是从unittest导入TestCase省去了自己运行manage.py runserver的麻烦。