【发布时间】:2019-04-25 21:17:55
【问题描述】:
我正在尝试为我的 Django 网站上的上传构建一些测试。它允许上传多个文件,所以我需要测试何时上传多个文件。
测试一个文件效果很好:
from django.test import Client
def test_stuff(self):
with open('....\file.csv','rb') as fp:
c = Client()
response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com', 'file': fp})
但是尝试使用文件列表不起作用。
def test_stuff(self):
file_list = # get list of file paths to open
myfiles = []
for file in file_list:
with open('....\file.csv','rb') as fp:
myfiles.append(fp)
c = Client()
response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com', 'file':myfiles})
也没有:
def test_stuff(self):
file_list = # get list of file paths to open
myfiles = []
for file in file_list:
with open('....\file.csv','rb') as fp:
myfiles.append(fp)
c = Client()
response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com',}, files={'file':myfiles})
或
def test_stuff(self):
file_list = # get list of file paths to open
myfiles = []
for file in file_list:
with open('....\file.csv','rb') as fp:
myfiles.append(fp)
c = Client()
response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com'}, files=myfiles)
我的视图从request.POST.get('myfiles') 获取文件,但FILES 为空。
有没有办法使用 django 测试客户端发布多个文件或者我应该使用其他东西?
经过修改,更准确
【问题讨论】:
-
Reddit 问题中的一条评论告诉我这不是文件列表,因此我查看了生产代码中的 AJAX 调用,并意识到它正在构建一个集合。我今天将在 python 中尝试一下,看看它是否有效。
标签: python django testing post integration-testing