[Tutor] another unit-testing question: regex testing

Serdar Tumgoren zstumgoren at gmail.com
Tue Dec 15 23:56:20 CET 2009

Hi everyone,

To continue on the popular topic of unit tests, I was wondering if
it's generally preferable to use a single unit test or a series of
unit tests when testing against a regex pattern.

In my specific case, I started out with a single test that contained a
(growing) list of bad input data intended to fail when tried against a
regex pattern.

But then I started thinking: Isn't each unit test supposed to test one
thing and one thing only? So I broke up my list of inputs and devoted
a single unit test to each (see below for both of my iterations).

On the one hand, I can see the benefit of having individual unit tests
for each of these varying inputs, since I can then see precisely which
one failed when I run my tests.

On the other hand, this one-test-per-pattern seems like a bit of
overkill given that I built up this list of bad inputs sequentially.
In other words, I added the first bad input to my initial list (in
"testIDFormat"), then ran the tests; then I added another failing
input and ran the tests; and so on.

In that way, I figured I could gradually build up a list of known bad
inputs, and simply append any new bad inputs that crop up later on.

So my question -- in this particular case involving a regex, what
downsides are there of simply lumping all the known bad values into a
single unit test?

Hopefully that makes sense...


 class TestOffice(unittest.TestCase):

    def testIDFormat(self):
        "Office constructor fails unless ID format is 'LETTERS-DIGITS'"
        ids = ['ABERCROMBIE_1',
               'NEIL ABERCROMBIE-1',
       for id in ids:
            self.assertRaises(models.IDError, models.Office, id)


class TestOfficeBadIDFormat(unittest.TestCase):

    def testLowerCase(self):
        """Office constructor fails with lower-case name in ID"""

    def testNoDigits(self):
        """Office constructor fails with no digits at end of ID"""

    def testTooManyDigits(self):
        """Office constructor fails with more than 3 digits at end of ID"""

    def testWrongConnector(self):
        """Office constructor fails if connector is not a minus sign"""

    def testSpacesInID(self):
        """Office constructor fails if ID contains spaces"""
                          models.Office,'NEIL ABERCROMBIE-1')

    def testDotsInID(self):
        """Office constructor fails if ID contains dots"""

More information about the Tutor mailing list