Now, you’re python skills could be improved, but how can you test it? Very simple, today I want to show you how to test modules and packages that you develop in your spare time or work time.
Testing in python can be made by the powerful unittest module. With this module (and yours), you can explore if your code is good or not good. The module must to be used with some kinds of tips & tricks, but I hope this example result helpful.
To start I want to show you some testing theory. The testing of a module is divided in testsuites, one testsuite is a set of testcases. Each testsuite tests one feature of your module, and each testcase must test one or more functions or methods of that feature. And example:
- testsuite1 <-> Feature 1
- testcase1
- testcase2
- testsuite2 <-> Feature 2
- testcase1
- testcase2
All of this is ok, but how to apply this to an example? First of all, when I develop a new module, I create a new test for that module in a test directory inside the module’s directory. In that directory, I store all the tests for a given set of related modules, and this is one example of one of that tests:
from sys import path
path.append ('..')from Module import Module, ModuleError
import unittest, logging
class ModuleMethods (unittest.TestCase):
logging.basicConfig (level = logging.DEBUG,
format = '%(asctime)s %(levelname)-8s %(message)s')
def setUp (self): # This is executed each time a test is launched
self.instance = Module (parameters)
def test_method1 (self): # First method, check value returned
r = self.instance.method1 (params)
logging.debug ('r = %s' % str (r))
self.failUnless (r[0] == 0)
def test_method2 (self): # Second method, check exception
self.failUnlessRaises (ModuleError, self.instance.method2, params)
def tearDown (self): # This is executed after each test finished
pass # Some test tearDown
def run (): # Running function of the test
suite = unittest.TestLoader ().loadTestsFromTestCase (ModuleMethods)
unittest.TextTestRunner (verbosity = 2).run (suite)
#Entry point of the test
if __name__ == '__main__':
run ()
This example shows you a simple example about creating a testcase, this is enough for a module or class with only a few functions or methods. This example creates a new class of testing (with unittest.TestCase as base class) and define some test_* methods, these methods are automatically invoked when the testRunner will be called in the run function.
In each method, a target method is tested, these methods are pretty simple, but you can make it a lot more complex ones. A definition for one of these methods is the following:
- Some configuration of the target.
- Target execution.
- Return value (or exception) handling to determine a FAIL or PASS test.
You can see functions like failUnless or failUnlessRaises that makes some conditional checks, if one or more fails, that test is considered FAIL. There are two types of FAIL:
- Error: An error is a test that raises some unexpected exception, this can be a wrong test or a problem in the target module code.
- Failure: A typical error of module code that doesn’t pass all checks made in the test.
For now, I think this information is enough. For further information about this testing framework, check this link.
See you! Comments welcomed.