【问题标题】:How do you unit test a file post with webapp2?您如何使用 webapp2 对文件发布进行单元测试?
【发布时间】:2014-01-22 20:12:20
【问题描述】:

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

【问题讨论】:

    标签: python google-app-engine unit-testing webapp2


    【解决方案1】:

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

    所以在上面它可能只是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      相关资源
      最近更新 更多