Python 如何使用webapp2对文件帖子进行单元测试?
我正在对webapp2应用程序进行单元测试,并想编写一个模拟文件post的测试。如何在单元测试中创建包含文件模拟内容的请求对象Python 如何使用webapp2对文件帖子进行单元测试?,python,google-app-engine,unit-testing,webapp2,Python,Google App Engine,Unit Testing,Webapp2,我正在对webapp2应用程序进行单元测试,并想编写一个模拟文件post的测试。如何在单元测试中创建包含文件模拟内容的请求对象 import unittest import webapp2 import main file_contents = """id, first, last 1, Bruce, Banner 2, Tony, Stark """ class TestHandlers(unittest.TestCase): def test_hello(self):
import unittest
import webapp2
import main
file_contents = """id, first, last
1, Bruce, Banner
2, Tony, Stark
"""
class TestHandlers(unittest.TestCase):
def test_hello(self):
request = webapp2.Request.blank('/')
request.method = 'POST'
# Magic needed here.
#Put file_contents into a form parameter
response = request.get_response(main.app)
#Test that the returned text contains something from the posted file
self.assertEqual(True, "Bruce" in response.body)
看起来像空白方法包含一个命名的POST参数。文档中说,通过使用它,请求方法自动设置为POST,内容类型设置为“application/x-www-form-urlencoded”
因此,在上述情况下,它可能只是:post_contents = {'someVar':file_contents}
request = webapp2.Request.blank('/', POST=post_contents)
response = request.get_response(main.app)