Art of Unit Testing

Paul Moore paul.moore at uk.origin-it.com
Fri Aug 17 09:20:37 EDT 2001


On Fri, 17 Aug 2001 10:00:46 +0200, Boudewijn Rempt <boudewijn at tryllian.com>
wrote:

>I've found little value in writing unittests for GUI apps (and that 
>includes games), but lots of use for unittests in the underlying libraries. 

One thing that I've not seen pointed out anywhere (it doesn't even seem to be
in the manual!) is that to write simple tests is pretty much trivial:

import unittest

class Tests(unittest.TestCase):
    def test1(self):
        self.assertEquals(1,1)
    def test2(self):
        self.assertEquals(1,2)

if __name__=='__main__':
    unittest.main()

Basically, it looks like unittest.main() scans the code for classes derived
from unittest.TestCase, and runs all methods whose names start with 'test' as
test cases.

This makes writing a series of simple, repetitive, tests very straightforward.
Obviously, for more complex cases, you need the more complex infrastructure,
but I have to say that *not* emphasising how simple it is to write basic
tests, does put people off. (It certainly put me off!)

One point with this form of test: If I add a setUp() or a tearDown() method,
they are called before and after *each* test, rather than once at the start
and end of the suite of tests. But without this stuff being documented, I'm
not sure if this should be considered "correct"...

Paul.




More information about the Python-list mailing list