[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...

Regards,
Serdar

<< FIRST ITERATION >>>
 class TestOffice(unittest.TestCase):

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


<<< SECOND ITERATION >>>

class TestOfficeBadIDFormat(unittest.TestCase):

    def testLowerCase(self):
        """Office constructor fails with lower-case name in ID"""
        self.assertRaises(models.IDError,
                          models.Office,'abercrombie-1')

    def testNoDigits(self):
        """Office constructor fails with no digits at end of ID"""
        self.assertRaises(models.IDError,
                          models.Office,'ABERCROMBIE')

    def testTooManyDigits(self):
        """Office constructor fails with more than 3 digits at end of ID"""
        self.assertRaises(models.IDError,
                          models.Office,'ABERCROMBIEJR-1234')

    def testWrongConnector(self):
        """Office constructor fails if connector is not a minus sign"""
        self.assertRaises(models.IDError,
                          models.Office,'ABERCROMBIE_1')

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

    def testDotsInID(self):
        """Office constructor fails if ID contains dots"""
        self.assertRaises(models.IDError,
                          models.Office,'ABERCROMBIE.JR-1')


More information about the Tutor mailing list