Writing tests is an important part of building an application. Especially with dynamically typed languages such as Python, testing is one of the only ways to determine the correctness of your implementation. Python has built the concept of Unit Tests into the core library. Unit Testing is a technique for validating units of your program by making assertions about the behaviour of that code. If any assertions turn out to be false, then you know your code isn’t correct.
In the interest of making test as automatic as possible, Flask-Diamond uses Nose for test discovery, which will find and run all your tests without having to create a test harness. As an application becomes more developed, it may become necessary to build a test harness for the application. However, at the start of application development, it is usually easier to use test discovery in order to iterate faster.
Nose will automatically search through the /tests folder for any files starting with the name “test” that have functions in them that also start with “test”.
It is possible to invoke the testing subsystem from the command line. The following different testing methods are available in Flask-Diamond:
Find every test and run it.
Run tests identified with decorator @attr("single"). These “single” tests can be used to make testing much faster by skipping all of the other tests.
The following code snippet contains a simple test with the single attribute decorator applied to it.
from nose.plugins.attrib import attr from flask.ext.testing import TestCase from flask.ext.diamond.mixins.testing import DiamondTestCaseMixin class BasicTestCase(DiamondTestCaseMixin, TestCase): @attr("single") def test_basic(self): assert True
This functionality will watch the project folder for files to change. When a file has changed, it will re-run tests identified with @attr("single"). This feature is designed to make it very quick to get feedback on the performance of your code.