December 24th 2008 I realized that dutest module reached 100 downloads from PyPI. This unexpected news, inspired me to write mini-tutorial entries in order to explain how to use it under different circumstances. In those articles you will be able to discover all the cool features it supports in order to enhance the integration between the two standard testing frameworks written for Python. This is just the beginning ...
¿What is dutest?Tha main goal of this module is to take advantages offered by both unittest and doctest. The former is implementation of the XUnit paradigm. Thus it has a lot of brothers (e.g. JUnit, SUnit ... and, if I continue I'd have to mention more than 50 implementations written for different programming languages and frameworks. So much success is the result of a good design, flexibility, and an incredible capacity to be integrated with other systems and frameworks. However, writing test cases using this style might be a little bit hard : you need to write a class, and a couple other things. Besides very simple tasks like to ensure that an exception is raised after a statement is executed, is not very intuitive.
On the other hand, doctest offers a different approach. Even if it lacks all the aforementioned features it also has a few virtues. This module is an incredible example of metaprogramming and simplicity. Tests are written directly in the documetation of functions, classes and modules under test. They look just like statements executed during interactive sessions using the interpreter (statement + result, statement + result ...). Hence doctests may be very useful for programmers using a library because they know exactly how to use it and the expected results. For the developpers it is useful to ensure that it behaves just like stated in the specifications. So if test cases dont fail and users pay attention to the documentation then everything will work just fine ! .
The major advantage of doctest is the readabiliy of the tests. The only thing you need to know in order to write doctest is the syntax of the programming language, and the expected outputs of the system ... so it's evident to ask : How could I take advantage of both approaches ? The answer is simple : Use dutest module.
If you need any help, illustrative examples or hints, don't hesitate: come back and visit this blog, read the articles, and post your own comments. And remember if you ask me about any particular feature you want to know about how to write tests in Python , and I'll always be there for you.