New-Village

月間ブログ。だいたい1カ月に1回は更新しているようです。

Selenium実行時の"unsupported operand type(s)"の消し方

Seleniumを使ったテスト実行時に表示される"unsupported operand type(s)"を消す方法のメモ書きです。

f:id:New-Village:20151108191847p:plain

 

エラー内容

テストを実行すると、テストの実行結果には影響がないものの、以下のエラーが表示されます。

(tutorial-env)→ mysite python manage.py test fts
Creating test database for alias 'default'...
Traceback (most recent call last):
File "/usr/lib/python3.4/wsgiref/handlers.py", line 137, in run self.result = application(self.environ, self.start_response)
File "/home/nitrous/tutorial-env/lib/python3.4/site-packages/django/test/testcases.py", line 1108, in __call__ return super(FSFilesHandler, self).__call__(environ, start_response)
File "/home/nitrous/tutorial-env/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 189, in __call__ response = self.get_response(request)
File "/home/nitrous/tutorial-env/lib/python3.4/site-packages/django/test/testcases.py", line 1091, in get_response return self.serve(request)
File "/home/nitrous/tutorial-env/lib/python3.4/site-packages/django/test/testcases.py", line 1103, in serve
return serve(request, final_rel_path, document_root=self.get_base_dir())
File "/home/nitrous/tutorial-env/lib/python3.4/site-packages/django/views/static.py", line 54, in serve
fullpath = os.path.join(document_root, newpath)
File "/home/nitrous/tutorial-env/lib/python3.4/posixpath.py", line 83, in join path += b 

TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'

 

原因と解消法

static.pyの23行目から始まるファンクションの引数"document_root"は、ファンクションが実行される前に何らかの変数をセットしておかないと"None"がセットします。何もセットしていないと、"None"と文字列を結合しようとしてType Errorが発生します。

setting.pyに、STATIC_ROOT変数を以下のとおりに追加すると解消されます。

(略)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '../static'))

 

参考