如何从webapp2请求处理程序单元测试传递给jinja2模板的模板变量

时间:2012-05-10 03:34:32

标签: unit-testing google-app-engine wsgi jinja2 webapp2

我以前从未做过单元测试。我想掌握它。

我正在尝试测试我的webapp2处理程序。为此,我认为向处理程序发送请求是个好主意,例如:

request = webapp2.Request.blank('/')
# Get a response for that request.
response = request.get_response(main.app)

问题是,响应主要只是一堆HTML等。

我想看看在处理程序转换为HTML之前从处理程序传递给我的jinja2模板的内容。

我希望我的测试能够处理处理程序类代码中的状态。我不想在响应处理程序中看到某些变量的样子,然后我想看看dict模板在传递给render_to_response()之前的样子是什么

我想测试这些变量是否具有正确的值。

到目前为止,这是我的测试代码,但我被卡住了因为response = request.get_response()只给了我一堆html而不是原始变量。

import unittest
#from google.appengine.ext import db
#from google.appengine.ext import testbed
#from google.appengine.datastore import datastore_stub_util
import main
import webapp2

class DemoTestCase(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testNothing(self):
        self.assertEqual(42, 21 + 21)

    def testHomeHandler(self):
        # Build a request object passing the URI path to be tested.
        # You can also pass headers, query arguments etc.
        request = webapp2.Request.blank('/')
        # Get a response for that request.
        response = request.get_response(main.app)

        # Let's check if the response is correct.
        self.assertEqual(response.status_int, 200)
        self.assertEqual(response.body, 'Hello, world!')


if __name__ == '__main__':
    unittest.main()

这是我的经纪人:

class HomeHandler(BaseHandler):
    def get(self, file_name_filter=None, category_filter=None):
        file_names = os.listdir('blog_posts')
        blogs = []

        get_line = lambda file_: file_.readline().strip().replace("<!--","").replace("-->","")

        for fn in file_names:
            with open('blog_posts/%s' % fn) as file_:
                heading = get_line(file_)
                link_name = get_line(file_)
                category = get_line(file_)

            date_ = datetime.strptime(fn.split("_")[0], "%Y%m%d")

            blog_dict = {'date': date_, 'heading': heading,
                         'link_name': link_name,
                         'category': category,
                         'filename': fn.replace(".html", ""),
                         'raw_file_name': fn}

            blogs.append(blog_dict)

        categories = Counter(d['category'] for d in blogs)
        templates = {'categories': categories,
                     'blogs': blogs,
                     'file_name_filter': file_name_filter,
                     'category_filter': category_filter}

        assert(len(file_names) == len(set(d['link_name'] for d in blogs)))

        self.render_template('home.html', **templates)

这是我的basehandler:

class BaseHandler(webapp2.RequestHandler):
    @webapp2.cached_property
    def jinja2(self):
        return jinja2.get_jinja2(app=self.app)

    def render_template(self, filename, **kwargs):
        #kwargs.update({})
        #TODO() datastore caching here for caching of (handlername, handler parameters, changeable parameters, app_upload_date)
        #TODO() write rendered page to its own html file, and just serve that whole file. (includes all posts). JQuery can show/hide posts.
        self.response.write(self.jinja2.render_template(filename, **kwargs))

也许我对如何进行单元测试有错误的想法,或者我应该以一种更容易测试的方式编写我的代码?或者是否有某种方式来获取我的代码状态?

如果有人要重新编写代码并更改变量名,那么测试就会中断..

请告知我的情况:X

2 个答案:

答案 0 :(得分:6)

您可以模拟BaseHandler.render_template方法并测试其参数。

请参阅this question以获取流行的Python模拟框架列表。

答案 1 :(得分:5)

感谢proppy的建议,我最终使用了模拟。

http://www.voidspace.org.uk/python/mock/

(mock包含在python 3中作为parttest或unittest.mock)

所以这是我的 main.py 代码,类似于我在webapp2中的代码:

注意而不是BaseHandler.render_template我有BaseHandler.say_yo

__author__ = 'Robert'

print "hello from main"

class BaseHandler():
    def say_yo(self,some_number=99):
        print "yo"
        return "sup"

class TheHandler(BaseHandler):
    def get(self, my_number=42):
        print "in TheHandler's get()"
        print self.say_yo(my_number)
        return "TheHandler's return string"

atest.py

__author__ = 'Robert'

import unittest
import main
from mock import patch

class DemoTestCase(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testNothing(self):
        self.assertEqual(42, 21 + 21)

    def testSomeRequests(self):
        print "hi"
        bh = main.BaseHandler()
        print bh.say_yo()

        print "1111111"

        with patch('main.BaseHandler.say_yo') as patched_bh:
            print dir(patched_bh)
            patched_bh.return_value = 'double_sup'
            bh2 = main.BaseHandler()
            print bh2.say_yo()
            print "222222"

        bh3 = main.BaseHandler()
        print bh3.say_yo()

        print "3333"

        th = main.TheHandler()
        print th.get()

        print "44444"
        with patch('main.BaseHandler.say_yo') as patched_bh:
            patched_bh.return_value = 'last_sup'
            th = main.TheHandler()
            print th.get()
            print th.get(123)
            print "---"
            print patched_bh.called
            print patched_bh.call_args_list
            print "555555"



if __name__ == '__main__':
    unittest.main()

此代码提供了大量输出,这是一个示例:

44444
in TheHandler's get()
last_sup
TheHandler's return string
in TheHandler's get()
last_sup
TheHandler's return string
---
True
[call(42), call(123)]
555555