Glashammer provides two main helpers for testing web applications:
- The ability to extract tests from declarative Yaml files, and
- Helpers that create temporary Application instances with test clients for regular unit tests.
Sooner or later, you will want to write unit tests for your web applications and the support libraries that they are built on. Glashammer testing support is integrated with py.test. Of the test runners, py.test is the most powerful, and so we have decided to use it. This does not stop you from using nosetests, or plain unittest, but you will not have the flowery goodness that the Glashammer testing framework provides.
Glashammer installs a plugin (setuptools entrypoint provider) which provides the py.test extensions, so you will need to make sure you have installed py.test:
pip install py
Once py.test is installed, you can run the Glashammer testing suite with the command:
py.test
A full description of py.test is not presented here, but you should read the basics of the tutorials and examples here http://codespeak.net/py/dist/test/
From the root of your project. Py.test will find tests in your project and run them. Traditionally, these tests are placed in the top-level source directory in a directory called tests. You may also wish to place them within package directories.
This way, you can write conventional Python unit tests, and run them using py.test. The extensions to this that Glashammer provides are described later.
The Glashammer testing framework provides a “collector” for py.test to automatically detect Yaml files that are named “test*.yml”, and to read tests from within them. These files can go in the tests directory with other tests, or they can be placed alongside your app.yml file.
When the test runner locates a suitable file, it attempts to extract tests from it. An example test.yml file is presented here, before entering into discussion about it:
test_index:
path: /