After many debugging hours I got to the root of the problem: The problem seems to be the name of the temp_module
, that is, because I give all my temp modules the same name. This has something to do with the built-in dir
method, as the dir
method, which is called by testLoader.loadTestsFromModule(temp_module)
returns test objects names that were run before. I'm not sure why, but this is the reason for code behavior.
To solve this I deleted the module name: 'temp_module' from sys.modules
after using the module. There may be a cleaner way, but this works.
Here is the improved code that worked for me:
def _RunTestsList(self, lTestsPaths):
""" Runs all tests in lTestsPaths with the unittest module
"""
for sTestPath in lTestsPaths:
testLoader = unittest.TestLoader()
temp_module = imp.load_source('temp_module', sTestPath)
tstSuite = testLoader.loadTestsFromModule(temp_module)
unittest.TextTestRunner (verbosity=1).run(tstSuite)
del sys.modules['temp_module']