nose: testing in Python made easy
I have recently been doing some quick experiments, and chose Python to do that, mostly due to a need for efficient system-level programming APIs. Python just excels at that.
One thing that always annoyed me in Python is the relative poor state of testing frameworks. See, Ruby has RSpec, Groovy/Java have EasyB, Scala has ScalaTest, and much more.
By contrast, Python comes bundled with the unittest module which is basically a straight port of JUnit to Python. This is not the most Python-esque API around, and writing fixture classes can be overkill at times, especially when you are used to the elegance of a BDD testing framework like EasyB. What really surprised me is that there are actually few testing frameworks for Python.
I initially wrote a few tests using unittest, but got quickly bored by the API and the headaches for having the testing code being in another tree of the project, while still being able to import the modules that I wrote.
Fortunately some clever people wrote nose, a testing framework that just works awesomely well. It is actually a tests launcher rather than a testing framework.
It scans the arborescence from where you invoke it, looking for any directory and python file have "test" in its filename. When it encounters such Python file, it will look inside for unittest.UnitTest classes, docstrings,... and any function that also has "test" as part of its name. This gives you a great deal of flexibility, as it will also look for "setup_*" and "teardown_*" functions.
The other selling point in nose is that no matter how deep your test Python scripts / classes are located within your source tree, packages / modules resolution is made from the invocation directory. This means that your main source code folder can have the modules directories as well as a "tests" folder, which is not a Python module, that you can structure cleanly (think separating unit_tests, functionnal_tests, ...) and that will have no issues resolving the modules. Neat.
Finally, nose is flexible through its plugins: you can get HTML output, JUnit-style XML tests reports for continuous integration servers integration, code coverage reports and much more.
If you code in Python, I highly recommend that you give it a try!
Related posts:
- A fresh breeze in the testing frameworks world
- JUnit and its clones are dead
- DecoratorInjector in Python by Yannick
- Python + mod_python: why use PHP again?
- ScalaTest in Maven
