如何用webapp2单元测试一篇文章?

2024-04-19 00:16:30 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: 文件theimportself应用程序内容mainresponse
1条回答
网友
1楼 · 发布于 2024-04-19 00:16:30

看起来像空白方法包含一个命名的POST参数。它在文档http://webapp-improved.appspot.com/guide/testing.html#request-blank中说,通过使用它,请求方法被自动设置为POST,内容类型被设置为“application/x-www-form-urlencoded”。在

因此,在上面可以是:

post_contents = {'someVar':file_contents}
request = webapp2.Request.blank('/', POST=post_contents)
response = request.get_response(main.app)

相关问题 更多 >