gpt4 book ai didi

python - django.core.exceptions 在 python 中运行 selenium 功能测试时出现 ImproperlyConfigured 错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:28:03 25 4
gpt4 key购买 nike

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

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

关于python - django.core.exceptions 在 python 中运行 selenium 功能测试时出现 ImproperlyConfigured 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38059271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com