How to make the simplest unittests in Python

Testing your code is nearly a requirement (even more so in Ruby). Unittests are now the most vital elements for evaluating the quality/viability of a project.
I was a little jealous of Ruby where you don’t have so much to write to implement unittests. Here is a simple example:

Now, using Nose, you can get even shorter code. If you do standard Python projects, you’ll use a file. To use nose, you do not even need to specify the path where to find the tests, just add two lines (tests_require and test_suite) to call nosetest:

Now, to add tests, you just have to create a directory named tests (in the root of your project, where your resides, and then add a python file()s. No need to add a to set the directory as a module. Now just add simple python files, like :

As you can see, no need to import anything for doing unittests, not even the standard python unittest module! That’s better than ruby! The downside of this is that nose is an ‘external’ package, so you’ll have to install it first (or set it as a dependency in your file, as shown above).

If you don’t use a, you can call nose directly from the command line, with ‘nosetest’.

Now, let’s find an equivalent to the really cool rspec ruby module!