I did similar things with the unittest framework by writing a function that creates and returns a test class. Then this function can take any parameters you want and configure the test class accordingly. You can also configure the __doc__ attribute of test functions to receive customized messages during test execution.
I quickly knocked down the following sample code to illustrate this. Instead of performing any actual testing, it uses the random module to reject some tests for demonstration purposes. When they are created, classes are inserted into the global namespace, so a call to unittest.main() will pick them up. Depending on how you run your tests, you may do something different with the generated classes.
import os import unittest
Run Example:
$ python tests.py -v Test parsing of file1.html ... ok Test parsing of file2.html ... ok Test parsing of file3.html ... ok Test parsing of file4.html ... ok Test parsing of tests/file5.html ... ok Test parsing of tests/file6.html ... FAIL Test parsing of tests/file7.html ... ok Test parsing of tests/file8.html ... ok ====================================================================== FAIL: Test parsing of tests/file6.html ---------------------------------------------------------------------- Traceback (most recent call last): File "generic.py", line 16, in test_file self.assertEquals(0, 1, 'Parsing failed.') AssertionError: Parsing failed. ---------------------------------------------------------------------- Ran 8 tests in 0.004s FAILED (failures=1)
Blair
source share