541
votes

I completed my first proper project in Python and now my task is to write tests for it.

Since this is the first time I did a project, this is the first time I would be writing tests for it.

The question is, how do I start? I have absolutely no idea. Can anyone point me to some documentation/ tutorial/ link/ book that I can use to start with writing tests (and I guess unit testing in particular)

Any advice will be welcomed on this topic.

7
It's never too late to write tests if that's you're intention. Better to have some than none for all that complain... - Asken
Here's a good test-driven development book that's out for free online: chimera.labs.oreilly.com/books/1234000000754/index.html - Will
good resource I stumbled upon https://www.jeffknupp.com/blog/2013/12/09/improve-your-python-understanding-unit-testing/. As a newbie to python, I found it understandable. - ferry
The Hitchhiker's Guide to Python has a brief overview of tools for unit testing: python-guide-pt-br.readthedocs.io/en/latest/writing/tests - Anton Tarasenko
The previous comment should be rated higher, since the guide also has an example code repository on github.com/kennethreitz/samplemod which is a great place to get started, too. - setempler

7 Answers

108
votes

If you're brand new to using unittests, the simplest approach to learn is often the best. On that basis along I recommend using py.test rather than the default unittest module.

Consider these two examples, which do the same thing:

Example 1 (unittest):

import unittest

class LearningCase(unittest.TestCase):
    def test_starting_out(self):
        self.assertEqual(1, 1)

def main():
    unittest.main()

if __name__ == "__main__":
    main()

Example 2 (pytest):

def test_starting_out():
    assert 1 == 1

Assuming that both files are named test_unittesting.py, how do we run the tests?

Example 1 (unittest):

cd /path/to/dir/
python test_unittesting.py

Example 2 (pytest):

cd /path/to/dir/
py.test
73
votes

The free Python book Dive Into Python has a chapter on unit testing that you might find useful.

If you follow modern practices you should probably write the tests while you are writing your project, and not wait until your project is nearly finished.

Bit late now, but now you know for next time. :)

43
votes

There are, in my opinion, three great Python testing frameworks that are good to check out:

  • unittest - module comes standard with all Python distributions
  • nose - can run unittest tests, and has less boilerplate.
  • pytest - also runs unittest tests, has less boilerplate, better reporting, and lots of cool extra features

To get a good comparison of all of these, read through the introductions to each at Start Here - Python Testing. There are also extended articles on fixtures, and more there.

35
votes

The docs for unittest would be a good place to start.

Also, it is a bit late now, but in the future please consider writing unit tests before or during the project itself. That way you can use them to test as you go along, and (in theory) you can use them as regression tests, to verify that your code changes have not broken any existing code. This would give you the full benefit of writing test cases :)

27
votes

unittest comes with the standard library, but I would recommend you nosetests.

"nose extends unittest to make testing easier."

I would also recommend you pylint

"Analyzes Python source code looking for bugs and signs of poor quality."

8
votes

As others already replied, it's late to write unit tests, but not too late. The question is whether your code is testable or not. Indeed, it's not easy to put existing code under test, there is even a book about this: Working Effectively with Legacy Code (see key points or precursor PDF).

Now writing the unit tests or not is your call. You just need to be aware that it could be a tedious task. You might tackle this to learn unit-testing or consider writing acceptance (end-to-end) tests first, and start writing unit tests when you'll change the code or add new feature to the project.

3
votes

nosetests is a brilliant solution for unit testing in Python. It supports both unittest based testcases and doctests, and gets you started with it with just a simple configuration file.