[Python-Dev] Unit testing (again)

Chris McDonough chrism@digicool.com
Tue, 13 Feb 2001 00:29:01 -0500


Andrew,

Here's a sample of PyUnit stuff that I think illustrates what you're asking
for...

from unittest import TestCase, makeSuite, TextTestRunner

class Test(TestCase):
    def setUp(self):
        self.t = {2:2}

    def tearDown(self):
        del self.t

    def testGetItemFails(self):
        self.assertRaises(KeyError, self._getitemfail)

    def _getitemfail(self):
        return self.t[1]

    def testGetItemSucceeds(self):
        assert self.t[2] == 2

def main():
    suite = makeSuite(Test, 'test')
    runner = TextTestRunner()
    runner.run(suite)

if __name__ == '__main__':
    main()

Execution happens like this:

call setUp()
call testGetItemFails()
print test results
call tearDown()

call setUp()
call testGetItemSucceeds()
print test results
call tearDown()

end

Isn't this almost exactly what you want?  Or am I completely missing the
point?

----- Original Message -----
From: "Andrew Kuchling" <akuchlin@cnri.reston.va.us>
To: <python-dev@python.org>
Sent: Monday, February 12, 2001 10:52 PM
Subject: Re: [Python-Dev] Unit testing (again)


> On Mon, Feb 12, 2001 at 06:16:19PM -0500, Jeremy Hylton wrote:
> >We can write a collection of helper functions for this, right?
> >    self.verify(sequenceElementsThatSame(l1, l2))
>
> Pretty much; nothing too difficult.
>
> >Maybe I'd be less opposed if I could understand why it's desirable to
> >continue executing a method where something has already failed
> >unexpectedly.  After the first exception, something is broken and
>
> In this style of unit test, you have setup() and shutdown() methods that
> create and destroy the test objects afresh for each test case, so cases
> aren't big long skeins of assertions that will all break given a single
> failure.  Instead they're more like 1) call a method that changes an
> object's state, 2) call accessors or get attributes to check invariants
are
> what you expect.  It can be useful to know that .get_parameter_value()
> raises an exception while .get_parameter_type() doesn't, or whatever.
>
> --amk
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev@python.org
> http://mail.python.org/mailman/listinfo/python-dev
>