[Edu-sig] UnitTest?
David MacQuigg
macquigg at ece.arizona.edu
Mon Feb 28 02:37:10 CET 2011
As usual, Pilgrim does an excellent job of explaining his topic. I
haven't looked at unittest for years, but Chapter 9 in his new book
explains it well.
That said, I still prefer doctest over unittest.
1) doctests can be introduced on day one, without much explanation.
Pilgrim waits until Chapter 9 to introduce the unittest module, and then
the presentation depends on students already knowing how to define
classes. At that point, it is much harder to get students to change
their habits, and start writing tests for even the simplest functions.
Even worse, students who stop short of learning OOP, and that includes
most who are not intending to be programmers, those students will never
appreciate the value of including tests as an integral part of writing a
program.
2) doctests look just like what students will enter in an interactive
session, while testing some function they have just written.
Cut-and-paste a successful test from the interpreter to the editor
window, and you are done.
3) doctests serve two functions - documentation and testing. Start with
a simple well-written doctest to make the purpose of a function crystal
clear. Then add numerous unit tests, one at a time as you think of
them, placing them in a separate test function to a avoid a conflict
between clarity and completeness. Refine your doctests to make the
explanation even more clear, but don't throw away your old doctests.
Just move them to the unit test function.
4) The explanatory doctests are what motivate students to write any
tests at all. Without this, writing tests gets put off to the end, or
never done.
Google [python doctest] for lots of interesting discussion. Two very
helpful links:
http://blog.tplus1.com/index.php/2008/07/14/python-doctests-seem-underappreciated/
http://stackoverflow.com/questions/361675/python-doctest-vs-unittest
See pykata.org for examples that make extensive use of doctests for both
explanation and unit testing.
On 2/26/11 11:25 AM, Laura Creighton wrote:
> In a message of Sat, 26 Feb 2011 10:28:58 MST, David MacQuigg writes:
>> In my opinion, the unittest framework is way too cumbersome for an
>> introductory course. Doctests are simple and self-explanatory.
>> Students should get in the habit of writing a doctest for every function
>> they write, even before the function itself is written.
> I think this is a reason to use py.test or nose rather than unittest,
> but not a reason to use doctest. I'm very fond of Mark Pilgrim's Unit
> testing chapter in Dive into Python
> http://diveintopython.org/unit_testing/index.html (2.x)
> http://diveintopython3.org/unit-testing.html (3.x)
>
> which is easy to adapt to a less verbose testing framework.
>
> Laura
>
>
>
--
************************************************************ *
* David MacQuigg, PhD email: macquigg at ece.arizona.edu * *
* Research Associate phone: USA 520-721-4583 * * *
* ECE Department, University of Arizona * * *
* 9320 East Mikelyn Lane * * *
*http://purl.net/macquigg Tucson, Arizona 85710 *
************************************************************ *
More information about the Edu-sig
mailing list