I’m writing a python module and I would like to unit test it. I am new to python and somewhat bamboozled by the options available.
Currently, I would like to write my tests as doctests as I like the declarative rather than imperative style (however, feel free to disabuse me of this preference if it is misinformed). This raises a few questions, however:
- Where should I put the tests? In the same file as the code they are testing (or in docstrings for doctests)? Or is it considered better to separate them out into their own directory?
- How can I run all the tests in the whole module from the command-line in one go?
- How can I report the code coverage of the test suite?
- Any other best-practices I should be aware of for unit testing in python?
I believe I used
doctestmore extensively (way stretching its intended use boundaries) than any other open source developer, at least within a single project — all the tests in my gmpy project are doctests. It was brand new at the timegmpywas starting, it seemed a great little trick, and if something is worth doing it’s worth doing in excess — right?-)Wrong. Except for
gmpy, where redoing everything as proper unit tests would be too much rework, I’ve never made that mistake again: these days, I use unit tests as unit tests, and doctests just to check my docs, as they’ve always been meant to be used. What doctests do (compare an expected with an actual result for equality — that’s all) is just not a good or sound basis to build a solid test suite on. It was never intended otherwise.I would recommend you look at nose. The
unittestmodule in the new Python 2.7 is much richer and nicer, and if you’re stuck on 2.4, 2.5 or 2.6 you can still use the new features with the unittest2 which you can download and install;nosecomplementsunittestquite well.If you can’t stand unittest (but — give it a try, it grows on you!-), maybe try py.test, an alternative package with a pretty different philosophy.
But, please, don’t stretch
doctestto test stuff other than examples in docs! The exact-equality comparison will stand in your way far too often, as I’ve had to learn at my (metaphorical;-) expense ingmpy…