How to get Django doctests to work again

Not so long ago i started to play with Web development framework Django. After more than 10 years spent in marketing and public relations i felt strong need to do something REAL with my own hands (i started my professional career as a software developer back in 1992).

After launching first tiny project (small site that helps you understand background processes running on your Mac) i wanted to follow best practice and get serious about testing. So i wrote my first doctests, they worked until one day...

...my Django doctests did not work at all, they did not even execute. When i test my application (python manage.py test  <application name>) it shows "Run 0 tests". What's wrong?

Googling for insight i found article named Django Testing Gothas, so as i understand there are FUNDAMENTAL problem with Django doctests: they do not execute if there is a single error within a single line.

In my case there was
   >>>line="test string"
which failed all 200 lines of my Django doctests, because
   >>> line="test string"
was expected (this is matter of single spacing character after >>>).

Django (in fact- Python) won't notify you that you cannot execute your tests if there is a problem in doctests. It will ignore all doctests because of single error in single line. I find it ridiculous. I perfectly understand that doctests are just a comments in code however- it is not acceptable that after a explicit command (test) to apply them, a one invalid line can make all your valid lines in doctests disappear without a single warning.

So if one day your Django doctests don't work, make sure that your doctests are 100% executable. Also- make sure that you have not tests.py AND tests directory within application directory- you should choose only one option, otherwise both can silently fail.

And my small victory out of this is better understanding of Python/Django doctests behavior. 
Filed under  //   django   testing  

About