Sure, testing helps ensure future quality and facilitate refactoring. But it also makes up-front development faster, particularly in smart editors and IDEs. Restarting your app and clicky-clicking in your browser is a drag.
In this step keep the same code as step01 but here we add some tests.
$ cd ../../creatingux; mkdir step02; cd step02
This ensures that you make the next directory in the right location.
Copy the following into step02/application.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('hello!')
def main():
config = Configurator()
config.add_view(hello_world)
app = config.make_wsgi_app()
return app
if __name__ == '__main__':
app = main()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
|
This is the same module as we saw in step01.
Copy the following into step02/tests.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import unittest
class ProjectorViewsUnitTests(unittest.TestCase):
def test_hello_world(self):
from application import hello_world
result = hello_world({})
self.assertEqual(result.body, b'hello!')
class ProjectorFunctionalTests(unittest.TestCase):
def setUp(self):
from application import main
app = main()
from webtest import TestApp
self.testapp = TestApp(app)
def test_it(self):
res = self.testapp.get('/', status=200)
self.assertTrue(b'hello' in res.body)
|
$ nosetests
You should see the following output:
..
--------------------------------------------------------
Ran 2 tests in 0.301s
OK
Unit tests are hard. Unit tests with a framework is even harder. The culture of Pyramid, though, is dedicated to full test coverage, and thus Pyramid works very hard to make test writing a productive experience.
Even if you don’t provide full test coverage, you will find that the most basic unit test will catch obvious errors quicker than re-clicking in your browser on every change. This is somewhat similar to setting up your editor or IDE to run pylint to let you know, before saving (much less before executing) if you have a bogus variable.
Functional tests are even easier to write, and for UX people, help on the part of the problem that is the focus.