reliable unit test logging

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Oct 1 09:44:30 EDT 2007


Vyacheslav Maslov <vmaslov at swsoft.com> writes:

> I have many many many python unit test, which are used for testing
> some remote web service.

Part of your confusion comes from the fact that "test a remote
service" isn't what a unit test does.

A unit test is one that executes a very *limited* part of the code: it
tests a code unit, not the whole system, and makes a simple set of
assertions about the result. If there are accesses to remote services
over the network, that's far beyond the scope of a unit test.

I don't doubt that you may be using the Python standard library module
'unittest' to perform these tests. But they're not unit tests; they're
integration tests, or system tests, or performance tests, or something
else.

> Can someone explain my why so simple feature like logging of
> timestamps during test execution was not implemented in any
> extension?

Probably because the most important thing to know for the purpose of a
unit test is whether the yes/no assertions were violated. Knowing when
the tests start and finish isn't interesting. If start and finish
times *are* interesting for your tests, you're *not* doing unit
testing, but some other form of testing like performance tests.


That said, you *can* certainly instrument the unittest.TestCase with
timings if you want to use it for a performance test. Every instance
of a TestCase will invoke its setUp method to set up test fixtures,
and its tearDown method to tear down fixtures. You can use that hook
to implement logging.

Untested code, that should give you enough to try it out yourself:

    import unittest
    import logging

    logging.basicConfig(level=logging.INFO,
                        datefmt="%Y-%m-%d %H:%M:%S",
                        format="%(asctime)s:%(levelname)s:%(message)s")

    class TimedTestCase(unittest.TestCase):
        """ A test case that will log its start and end times """

        def setUp(self):
            """ Set up test fixtures """
            logging.info("test case setup")

        def tearDown(self):
            """ Tear down test fixtures """
            logging.info("test case teardown")

    class Test_FooBar(TimedTestCase):
        """ Test cases for FooBar """

        def test_slices_spam(self):
            """ FooBar should slice spam """
            self.failUnless(sends_spam)

        def test_eats_eggs(self):
            """ FooBar should eat eggs """
            self.failUnless(eats_eggs)

-- 
 \        "The World is not dangerous because of those who do harm but |
  `\          because of those who look at it without doing anything." |
_o__)                                                 —Albert Einstein |
Ben Finney



More information about the Python-list mailing list