lightweight unit testing in python
Question: I'm considering using Python to teach introductory programming, and I'm
looking for a lightweight unit testing framework. I've taken a look at
unittest, and--as far as I can tell--it looks insanely _non_ -lightweight.

For example, here's what I'd like to be able to write:

    
    
    import unittest
    
    def f(x):
      return x+2
    
    checkEqual(f(3),5)
    

... and _nothing else_. To show you where I'm coming from, this is what I'd
write in Racket's beginning student language:

    
    
    (define (f x)
      (+ x 2))
    
    (check-expect (f 3) 5)
    

... and that's it. Surely someone's written this tool, and I'm just not
finding it?

(Apologies in advance for any appearance of flame-baiting. This is a serious
question.)

SELF-EDIT:

Before anyone points this out: yes, I could write def checkEqual(a,b):
print(a==b) ; I'm looking for something with a bit more: it should be able to
check numbers with tolerances, it should have support for printing only failed
test cases, it should be able to tell you how many test cases failed. Again,
I'm confident that this code can be written; I'm just trying to avoid re-
inventing the wheel.


Answer: I would recommend [Doctest](http://docs.python.org/library/doctest.html).

Your example would look like:

    
    
    def f(x):
        """
        >>> f(3)
        5
        """
        return x + 2
    

Why?:

  1. It's super simple: "when I run this thing I should get this answer back"
  2. It works on the function level - which might allow you to introduce testing even before classes
  3. Mirrors the interactive Python experience.

