django.core.exceptions 在 python 中进行 运行 selenium 功能测试时出现配置错误

django.core.exceptions ImproperlyConfigured error when running selenium functional test in python

我之前遇到过与此类似的问题,我收到了一条几乎相同的错误消息,即

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()

您的脚本开头应如下所示

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

发生这种情况是因为 Django 在尝试调用设置之前没有正确设置,它无法知道在没有 "DJANGO_SETTINGS_MODULE" 变量的情况下应该使用哪个项目设置,并且 django.setup() 调用是必不可少的导入模型和访问数据库。 (有关该方法,请参阅

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

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

首先是文件名的问题,manage.py test(doc) 的默认格式是"test*.py",您可以用-p--pattern 但通常的解决方案是将 functional_tests.py 重命名为 tests.py 并将其放在 functional_tests 文件夹中。这样你就可以通过调用 manage.py test functional_tests.

来调用功能测试

此外,您应该考虑使用 TestCase(doc) from django.test or even LiveServerTestCase(doc) 而不是从 unittest 导入 TestCase,这样可以避免您自己 运行 manage.py runserver 的麻烦。