Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:第二个Selenium测试失败,服务器错误为500_Python_Django_Selenium_Testing_Functional Testing - Fatal编程技术网

Python Django:第二个Selenium测试失败,服务器错误为500

Python Django:第二个Selenium测试失败,服务器错误为500,python,django,selenium,testing,functional-testing,Python,Django,Selenium,Testing,Functional Testing,我试图向Django项目添加一些selenium测试,但是第二个测试总是失败,出现服务器错误(500)。由于两个测试的开始完全相同,我认为它一定与设置和拆卸方法有关。有人能帮忙吗?谢谢 from django.test import LiveServerTestCase from selenium import webdriver from selenium.webdriver.common.keys import Keys from django.contrib.auth.models imp

我试图向Django项目添加一些selenium测试,但是第二个测试总是失败,出现
服务器错误(500)
。由于两个测试的开始完全相同,我认为它一定与
设置
拆卸
方法有关。有人能帮忙吗?谢谢

from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.contrib.auth.models import User 
from selenium.webdriver.support.ui import Select
class UserTest(LiveServerTestCase):
    def setUp(self):
        User.objects.create_user(username='user', password='pass', email='test@test.com')
        self.browser = webdriver.Chrome()

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

    def changeSelector(self, browser, value):
        mealSelector = Select(browser.find_element_by_id('mealsToday'))
        mealSelector.select_by_visible_text(str(value))

    def login_user(self):
        self.browser.get(self.live_server_url)
        self.timeout(5)
        self.assertIn('Animals', self.browser.title)
        # Log in
        login_button = self.browser.find_element_by_id('login').click()
        self.browser.find_element_by_id('id_username').send_keys('user')
        self.browser.find_element_by_id('id_password').send_keys('pass')

    def timeout(self, time_to_sleep):
        import time
        time.sleep(time_to_sleep)

    def test_one_test(self):
        self.login_user()

    def test_two_test(self):
        self.login_user()
编辑:我应该提到的是,第一次测试运行良好,并返回成功。第一次测试之后的任何测试在启动时失败,错误为500

编辑2:运行测试时看到的内容:

======================================================================
FAIL: test_two_test (functional_tests.tests.UserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/functional_tests/tests.py", line 34, in test_two_test
    self.login_user()
  File "/functional_tests/tests.py", line 20, in login_user
    self.assertIn('Animals', self.browser.title)
AssertionError: 'Animals' not found in 'http://localhost:8081/'
即使这个最小的代码也会失败:

from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.contrib.auth.models import User 
from selenium.webdriver.support.ui import Select
class UserTest(StaticLiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()

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

    def login_user(self):
        self.browser.get(self.live_server_url)
        self.assertIn('Animals', self.browser.title)
    def test_one_test(self):
        self.login_user()

    def test_two_test(self):
        self.login_user()

在第二个方法中第二次调用
get
时,我可以看到500错误存在,并且没有正确加载任何内容。为什么会这样?

很难说没有看到完整的回溯,但是,这可能是因为
create\u user()
调用-它无法创建具有现有用户名的用户。尝试移动
setUpClass()下的
create\u user()


在一些代码之后,可以向我显示错误(测试套件设置
DEBUG=False
以更接近模拟真实环境)


然后我看到代码爆炸了,因为没有系统预期的一行。这是因为我在迁移脚本中添加了此行。这通过了第一次测试,因为所有迁移脚本都是在测试开始时运行的,但是在第一次测试之后删除了所有数据之后,就再也不会添加这些数据了。

也许,它对某些人会有用

我在使用
LiveServerTestCase
测试Selenium时遇到问题,调试是正确的,所有关于最后答案的问题都是确定的

但是,早些时候我尝试在Heroku上部署我的web应用程序,我的settings.py中有这样一行代码:

django\u heroku.settings(locals())

但是当我删除它时,我没有发现这个错误。

很抱歉没有回溯-这很难,因为我在测试中没有得到任何结果-只有500个结果。我不知道如何得到回溯。有什么想法吗?@skaz运行测试时,您在控制台上看到了什么?您是否配置了日志记录?我将编辑我的帖子并向您展示我看到的内容。我没有配置日志记录——我正在做一个教程,还没有达到这个程度。我发现很难找到django类的文档。当我在谷歌上搜索“LIveServerTestCase文档”时,我总是被带到一些类似于教程的类如何工作的解释中,但在任何地方都找不到实际的类定义。这在任何地方都存在吗?谢谢。这里发生了一些更基本的事情-即使我删除了登录代码,对
浏览器的简单调用也会失败,并出现相同的错误。
class UserTest(LiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        User.objects.create_user(username='user', password='pass', email='test@test.com')
        super(UserTest, cls).setUpClass()

    def setUp(self):
        self.browser = webdriver.Chrome()