Many Python modules preserve an internal state without defining classes, e.g. logging
maintains several loggers accessible via getLogger()
.
How do you test such a module?
Using the standard unittest
tools, I would like the various tests inside a TestCase
class to re-import my module-under-test so that each time it loses its context. Can this be done?
This will reimport the module as new for you:
% test.py Test.test_logging
passes:but
% test.py Test.test_logging2
does not:since the internal state of
logging
has been reset.