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.

+6  A: 

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 :)

Justin Ethier
Aah! I had no idea that it was to be done along the project. I will take care in future. Thanks for the link though.
+4  A: 

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. :)

Mark Byers
Well, I had no idea about that. Anyways, let it be next time then :)
+4  A: 

This is a tutorial for test-driven development in Python. Now, like Justin said, it's better to write your tests before or during coding, and that's how this tutorial assumes you're working, but I still think you'll find it helpful.

And here's part 2.

Thanks. I intend to read up.

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:

import unittest

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

def main():

if __name__ == "__main__":

Example 2:

def test_starting_out():
    assert 1 == 1

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

Example 1

$ cd /path/to/dir/
$ python

Example 2:

$ cd /path/to/dir/
$ py.test
Tim McNamara
Thanks for the is concise answer.
+1  A: 

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".

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.

+1 for "Working Effectivly with Legacy Code". It's all about code that don't have tests.
+1  A: 

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

Daniel Kluev
+2  A: 

unittest comes with the standatd library, but I would recomend you nosetests.

"nose extends unittest to make testing easier."

I would also recomend you pylint

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