将django.test.LiveServerTestCase的live_server_url与另一个字符串连接时,TypeError [英] TypeError when concatenating django.test.LiveServerTestCase's live_server_url with another string

查看:344
本文介绍了将django.test.LiveServerTestCase的live_server_url与另一个字符串连接时,TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试根据 self.live_server_url 构造一个字符串,我得到python TypeError 消息。例如,我已经尝试了以下字符串结构(下面的形式1和2),但我体验到相同的 TypeError 。我想要的字符串是附加了/ lists的Live Server URL。注意:实际测试确实成功创建了一个服务器,我可以手动访问服务器,更具体地说,我可以手动访问我正在以编程方式构建的确切URL(例如'http:/ / localhost:8081 / lists ')。

Whenever I try to construct a string based on self.live_server_url, I get python TypeError messages. For example, I've tried the following string constructions (form 1 & 2 below), but I experience the same TypeError. My desired string is the Live Server URL with "/lists" appended. NOTE: the actual test does succeed to create a server and I can manually access the server, and more specifically, I can manually access the exact URL that I'm trying to build programmatically (e.g. 'http://localhost:8081/lists').

TypeError

# FORM 1
lists_live_server_url = '%s%s' % (self.live_server_url, '/lists')

# FORM 2
lists_live_server_url = '{0}{1}'.format(self.live_server_url, '/lists')

self.browser.get(lists_live_server_url)

这个表单没有python错误(没有附加到字符串),虽然我的测试失败(正如我预期的,因为它没有访问 / lists )。

There is no python error with this form (nothing appended to string), albeit my test fails (as I would expect since it isn't accessing /lists).

self.browser.get(self.live_server_url)

这是python错误我得到了。

Here is the python error that I'm getting.

/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py test functional_tests.lists_tests.LiveNewVisitorTest.test_can_start_a_list_and_retrieve_it_later /Users/myusername/PycharmProjects/mysite_proj
    Testing started at 11:55 AM ...
    Creating test database for alias 'default'...
    Traceback (most recent call last):
      File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 137, in run
        self.result = application(self.environ, self.start_response)
      File "/usr/local/lib/python3.4/site-packages/django/test/testcases.py", line 1104, in __call__
        return super(FSFilesHandler, self).__call__(environ, start_response)
      File "/usr/local/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
        response = self.get_response(request)
      File "/usr/local/lib/python3.4/site-packages/django/test/testcases.py", line 1087, in get_response
        return self.serve(request)
      File "/usr/local/lib/python3.4/site-packages/django/test/testcases.py", line 1099, in serve
        return serve(request, final_rel_path, document_root=self.get_base_dir())
      File "/usr/local/lib/python3.4/site-packages/django/views/static.py", line 54, in serve
        fullpath = os.path.join(document_root, newpath)
      File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/posixpath.py", line 82, in join
        path += b
    TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'

我不知不觉中尝试修改 live_server_url ,导致这些 TypeError s?我如何以编程方式构建一个字符串 live_server_url +/ lists

Am I unknowingly attempting to modify the live_server_url, which is leading to these TypeErrors? How could I programmatically build a string of live_server_url + "/lists"?

这是我的测试尝试...

Here is the test that I am attempting...

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.test import LiveServerTestCase

class LiveNewVisitorTest(LiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.implicitly_wait(3)

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

    def test_can_start_a_list_and_retrieve_it_later(self):
        #self.browser.get('http://localhost:8000/lists')
        #self.browser.get('http://www.google.com')
        #lists_live_server_url = '%s%s' % (self.live_server_url, '/lists')
        #lists_live_server_url = '{0}{1}'.format(self.live_server_url, '/lists')
        lists_live_server_url = self.live_server_url
        self.browser.get(lists_live_server_url)

        self.assertIn('To-Do', self.browser.title)
        header_text = self.browser.find_element_by_tag_name('h1').text
        self.assertIn('To-Do', header_text)


推荐答案

在Reddit上看到这个讨论具有相同的错误追溯

基本上,这不是任何在 Selenium 测试,而是与项目的静态文件配置。

Basically, this is not a problem with anything within the Selenium tests but rather with your project's static file configuration.

从您的问题,我相信 Traceback 是:

From your question, I believe the key line within the Traceback is:

File "/usr/local/lib/python3.4/site-packages/django/views/static.py", line 54, in serve
    fullpath = os.path.join(document_root, newpath) 

这行表示th在 django.views.static 中尝试不成功的 os.path.join

This line indicates that an unsuccessful os.path.join is being attempted within django.views.static.

在项目的 settings.py 文件中设置 STATIC_ROOT ,你应该是好的。

Set STATIC_ROOT in your project's settings.pyfile and you should be good.

这篇关于将django.test.LiveServerTestCase的live_server_url与另一个字符串连接时,TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆