我有一个应用程序,允许用户上传文件并将其显示在页面上。我创建了一个测试类,其中包含一个方法,用于检查上载的文件(本地存储)是否与POST请求返回的文件相同。此测试在本地工作,但由于某种原因,当我在TravisCI上运行测试时,POST似乎没有返回文件,并且构建失败。
我认为错误是因为我使用python模块whitenoise来管理生产中的静态文件。由于whitenoise将上传的文件存储在与平常不同的地方,因此django.test.Client.post()
可能不知道在哪里查找文件。在我安装whitenoise之前,测试正在研究TravisCI,所以这似乎就是这种情况。
如果我将whitenoise软件包留在requirments.txt文件中以安装在TravisCI构建服务器上,则会出现这种情况,但是当我尝试从文件中删除软件包时,即使我删除了该文件,也会出现以下错误来自项目设置的本地版本的whitenoise设置:
======================================================================
ERROR: test_uploaded_file (photoViewer.test_upload.FileUploadTestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/travis/build/loremIpsum1771/test_ci/photoViewer/test_upload.py", line 16, in test_uploaded_file
response = c.post('/', {'image': fp})
File "/home/travis/virtualenv/python2.7.14/lib/python2.7/site-packages/django/test/client.py", line 548, in post
secure=secure, **extra)
File "/home/travis/virtualenv/python2.7.14/lib/python2.7/site-packages/django/test/client.py", line 350, in post
secure=secure, **extra)
File "/home/travis/virtualenv/python2.7.14/lib/python2.7/site-packages/django/test/client.py", line 416, in generic
return self.request(**r)
File "/home/travis/virtualenv/python2.7.14/lib/python2.7/site-packages/django/test/client.py", line 483, in request
response = self.handler(environ)
File "/home/travis/virtualenv/python2.7.14/lib/python2.7/site-packages/django/test/client.py", line 131, in __call__
self.load_middleware()
File "/home/travis/virtualenv/python2.7.14/lib/python2.7/site-packages/django/core/handlers/base.py", line 80, in load_middleware
middleware = import_string(middleware_path)
File "/home/travis/virtualenv/python2.7.14/lib/python2.7/site-packages/django/utils/module_loading.py", line 20, in import_string
module = import_module(module_path)
File "/opt/python/2.7.14/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named whitenoise.middleware
为什么Django在没有安装时仍在寻找whitenoise模块?如何在本地运行(在构建服务器上)时禁用它,以便测试方法中的post请求实际返回文件?
测试类:
from __future__ import unicode_literals
import os
from django.test import Client
import unittest
MYDIR = os.path.dirname(__file__)
class FileUploadTestClass(unittest.TestCase):
def test_uploaded_file(self):
c = Client()
originalFilePath = os.path.join(MYDIR, '../live-static/static-root/Keep-Calm-and-Carry-On.jpg')
with open(originalFilePath, "rb") as fp:
response = c.post('/', {'image': fp})
fp.seek(0)
self.assertEqual( fp.read(),response.content)
本地设置文件
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"photoViewer",
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
# '/var/www/static/',
]
STATIC_ROOT = os.path.join(BASE_DIR,"live-static","static-root")
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR,"live-static","media-root")
CORS_REPLACE_HTTPS_REFERER = False
HOST_SCHEME = "http://"
SECURE_PROXY_SSL_HEADER =None
SECURE_SSL_REDIRECT = False
SESSION_COOKIE_SECURE = False
CSRF_COOKIE_SECURE = False
SECURE_HSTS_SECONDS = None
SECURE_HSTS_INCLUDE_SUBDOMAINS = False
SECURE_FRAME_DENY = False