[Pyunit-interest] loadTestFromName masks import errors
Brought to you by:
purcell
From: Terrel S. <tsh...@jd...> - 2003-02-01 02:48:45
|
I have a UnitTests driver that searches the current directory and all subdirectories for files matching UnitTest_*.py, converts the file names to module names, and passes these module names to unittest.main(). This works nicely except for one very nasty wrinkle: The code in loadTestsFromName (unitest.py:440+) --------- module = __import__(string.join(parts_copy,'.')) break except ImportError: del parts_copy[-1] if not parts_copy: raise --------- masks ImportErrors when loading the modules. If I have 50 UnitTest_*.py files, I am not going to notice that 3 modules failed to import, and I confidently believe that all of my tests passed because unittest.py reported no failures. If I manually run the individual module, the failure shows up before unittest.py even becomes involved, but I can't do that for each of 50 modules. (That is why I wrote the driver). My solution is to save the ImportError exception info in a TestCase class and re-raise it when the test is run. (I don't know how this plays with the persistent test runners, but It Works For Me(TM).) Index: unittest.py =================================================================== --- unittest.py (revision 315) +++ unittest.py (working copy) @@ -440,8 +440,14 @@ module = __import__(string.join(parts_copy,'.')) break except ImportError: - del parts_copy[-1] - if not parts_copy: raise + class ImportFailure(TestCase): + exci = sys.exc_info() + def test_import(self): + c,v,t = self.exci + raise c,v,t + + return self.loadTestsFromTestCase(ImportFailure) + parts = parts[1:] obj = module for part in parts: |