[Tutor] testing: doctest and unittest

Kent Johnson kent37 at tds.net
Fri Nov 11 14:50:29 CET 2005


Kent Johnson wrote:
> Alex Hunsley wrote:
>>Where do you seasoned pythonites see unittest and doctest in relation to 
>>each other? Do you only use one or the other?
> 
> 
> I think it is mostly personal preference. Doctest is nice where you
> create examples for others, maybe not so nice where you are creating
> exhaustive unit tests trying to exercise every corner case. unittest
> is perhaps easier to aggregate tests from multiple modules. Doctests
> integrate with unittest in Python 2.4. Personally I use unittest but
> I come from a Java background and learned TDD with JUnit which is
> related to unittest.

I have recently begun a project where I am using extreme test-driven development with unittest - writing very small tests, then writing the code to make the test pass [1]. In this case much of the code is testing access to data that I read from a text file. I think these tests would make very poor doctests because
- they are just exercising the accessors (attributes, actually) of a data class; they don't contribute much to understanding the class under test
- they rely on test files for the data, they won't run in isolation [2]

Most of my unit tests are crushingly boring reading, so for me I think unittest is a better fit.

[1] I like this style in general. In this case the programming is fairly exploratory - I have to figure out what data I need from the file, then figure out how to get it - which is an excellent fit for this style of programming. I figure out one bit of data, write a test for it, write the code to access the data, repeat.

[2] I know, some unit testing purists say unit tests shouldn't rely on external resources such as files and databases. I disagree :-) as long as the tests are fast I don't care what resources they use.

Kent

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list