[Pythonmac-SIG] unittest Question

Skip Montanaro skip at pobox.com
Sat Jul 19 08:52:12 EDT 2003


    Josh> I don't have a command line, so when I run the program using
    Josh> unittest.main() I get the following output:


    Josh> ----------------------------------------------------------------------
    Josh> Ran 0 tests in 0.000s

    Josh> OK

What's the structure of your test module look like?  Your test case classes
must be subclasses of unittest.TestCase and any test methods you want run
must begin with "test" (or is it "test_"?).  If either is not the case,
unittest.main won't find and execute anything.

Have you looked at some of the unittest-based test cases in the Python
source?  Here's the entire code from the shortest one, Lib/test/test_grp.py:

    import grp
    import test_support
    import unittest


    class GroupDatabaseTestCase(unittest.TestCase):

        def setUp(self):
            self.groups = grp.getgrall()

        def test_getgrgid(self):
            entry = grp.getgrgid(self.groups[0][2])

        def test_getgrnam(self):
            entry = grp.getgrnam(self.groups[0][0])


    def test_main():
        test_support.run_unittest(GroupDatabaseTestCase)


    if __name__ == "__main__":
        test_main()

That can be slightly simplified to:

    import grp
    import unittest

    class GroupDatabaseTestCase(unittest.TestCase):

        def setUp(self):
            self.groups = grp.getgrall()

        def test_getgrgid(self):
            entry = grp.getgrgid(self.groups[0][2])

        def test_getgrnam(self):
            entry = grp.getgrnam(self.groups[0][0])

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

If I run it with Python 2.2 I get:

    test_getgrgid (__main__.GroupDatabaseTestCase) ... ok
    test_getgrnam (__main__.GroupDatabaseTestCase) ... ok

    ----------------------------------------------------------------------
    Ran 2 tests in 0.056s

    OK

Skip



More information about the Pythonmac-SIG mailing list