我正在对webapp2应用程序进行单元测试,并希望编写一个模拟文件发布的测试。如何在单元测试中创建包含文件模拟内容的请求对象?在
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参数。它在文档http://webapp-improved.appspot.com/guide/testing.html#request-blank中说,通过使用它,请求方法被自动设置为POST,内容类型被设置为“application/x-www-form-urlencoded”。在
因此,在上面可以是:
相关问题 更多 >
编程相关推荐