Testing - renzon/zenwarch GitHub Wiki

On development world testing is a first class citizen nowadays. Besides there is no special architecture about it on ZenWArch, this section shows a approach to test your app on GAE and it can be used as a base to another approaches.

HTTP calls

Once our handlers are just ordinary python functions, testing them are very straightforward. Let's the see code of my_form.py:

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals


def index(_write_tmpl, name):
    _write_tmpl('templates/form.html', {'name': name})

To test this code, we would like to know the parameters that are sent to _write_template. More than that, we could actually render the template too, so if it has syntax errors, we can check it before deploying it to our customers:

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import unittest
import tmpl
from web import my_form


class FormTests(unittest.TestCase):
    def test_index(self):
        # Mocking dependencies
        params = {}

        def write_tmpl_mock(template_name, values):
            params['template_name'] = template_name
            params['values'] = values
            tmpl.render(template_name, values)

        # fake http call
        my_form.index(write_tmpl_mock, 'Renzo')

        # Assertions
        self.assertEqual('templates/form.html', params.get('template_name'), 'Wrong template')
        self.assertDictEqual({'name': 'Renzo'}, params.get('values'), 'Wrong template values')


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

So, as you can see, because our handler is just a function, we just need to call it as a regular python function to fake a http call.

Once our dependency injection system is simple, we can change the write_tmpl function when testing. We have mocked it so to check if we passed the correct template name, the correct template values. We called tmpl render function, so if there is some error on form.html template syntax, the test can show us on test phase.

You can see that ZenWArch approach is as simple as Flask. But once you don't have to import any framework code besides the router.py, we could easily convert this project to DJango, for example.

With such simplicity for testing and deploying for Google App Engine, it's easily implement a Continuous Delivery process using this environment.

I hope you enjoy it!

Renzo Nuccitelli

⚠️ **GitHub.com Fallback** ⚠️