The Testipy framework |
:: Email ::
Download area
|
Testipy is a simple, lightweight testing framework, that I wrote for personal use. There is no reason for you to use it. Why did I write it, then? ... For my work, I wrote and maintain a large(ish) body of code. Currently, there are around 450 tests for this, using unittest. Over time, it became painfully clear that I missed two features: 1. Methods (non-tests) that run before and after the whole test case. Currently I'm using an ugly hack, that abuses the fact that tests are run alphabetically. So test_000 will be run first, and I use this to create temporary directories, etc. test_zzz will be run last and is used to clean up these directories. Not only is this ugly, but it has several drawbacks; for example, if an error occurs in test_000, all the other tests are run anyway, probably causing errors because things were not set up properly. 2. Methods that run before and after a specific test. I suppose I could use a try..finally construct, but this isn't ideal. The setUp() and tearDown() methods don't cut it, because they are run before and after *every* test, so they don't quite do what I want. Setting up things in the test case's __init__ isn't good either, because this isn't always executed at the right time (for example, in a test suite with multiple test cases). Enter Testipy. This is a very minimal framework, it has only ~250 lines of code, and it's not complete or perfect, but it does what I want. Here is what it does:
There's a TestCase class, much like unittest's. You stick test methods in it:
The actual tests are done using various methods, much like unittest's. Their usage is obvious. Here's an example:
Before the tests in a TestCase are called, the BeforeTestCase() and AfterTestCase() methods are called. These methods are optional. You can use them to set things up or do cleanups. So, the order is:
- BeforeTestCase()
Before any specific test, a similar procedure happens. Let's say we have a method
Similarly,
- BeforeTestFoo; or, if not present; BeforeTest; or, if not present, nothing Note that if BeforeTestCase fails, we exit with an error; no tests are run. Similarly, if the "before" method fails, the actual test isn't run. BeforeTest and AfterTest are similar to unittest's setUp and tearDown. The other "special" methods don't have an equivalent in unittest, as far as I can tell.
There's also a TestSuite class. You feed it testcases and run it:
Note: The Run() methods (currently) have two optional parameters, print_summary and print_report. The "report" prints a traceback in case of errors (after all the tests are done). The "summary" just lists the names of the tests, and whether they passed (OK), failed (FAIL) or had errors other than failed assertions (ERROR). Sample output:
If you happen to know any useful enhancements that do not compromise its simplicity, I'd like to hear about them. |