[Numpy-discussion] doctest improvements patch (and possible regressions)

Fernando Perez fperez.net@gmail....
Thu Dec 10 20:27:56 CST 2009

On Wed, Dec 9, 2009 at 6:04 AM, Michael Droettboom <mdroe@stsci.edu> wrote:
> Sorry, just commenting on the parts I feel competent in :)  But I think
> this is a great improvement.  It would be nice to start doing doctests
> as a matter of course to keep the docs accurate.

Indeed.  From the sidelines, thanks a lot to Paul for this work!

Speaking of testing, here's a bit of code that I've had for a while
pending for ipython, but which I think could be very useful for numpy
too.  Feel free to rip only the parts you need...

The point of this code is to get a few nose-style features that
produce normal unittests.  In particular, it introduces support for
single functions to be tests (something nose does but unittest
doesn't), and most importantly, parametric tests that can be debugged
meaningfully.  A simple example:

def is_smaller(i,j):
    assert i<j,"%s !< %s" % (i,j)

class Tester(ParametricTestCase):

    def test_parametric(self):
        yield is_smaller(3, 4)
        x, y = 1, 2
        yield is_smaller(x, y)

def test_par_standalone():
    yield is_smaller(3, 4)
    x, y = 1, 2
    yield is_smaller(x, y)

Both the class-based and the function-based tests are parametric.  The
key difference with nose is that you do

yield assertion(values)

instead of

yield (assertion, values)

When the test suite executes, it's calling *your* code, and if the
test fails, the stack you end up inspecting is your stack.  With nose,
you return the unevaluated arguments assertion and nose calls it
later.  If the test fails, you're stuck debugging inside the nose
stack, with no useful information about your test other than the
values, and your original stack is gone.

I hope this helps those writing numpy/scipy tests.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: lightunit.tgz
Type: application/x-gzip
Size: 5604 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/numpy-discussion/attachments/20091210/9bd77b76/attachment.tgz 

More information about the NumPy-Discussion mailing list