Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用webapp2对文件帖子进行单元测试?_Python_Google App Engine_Unit Testing_Webapp2 - Fatal编程技术网

Python 如何使用webapp2对文件帖子进行单元测试?

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):

我正在对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):

        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)