Commits

Stefan H. Holek committed 9e86ca4

Purge modules under test from sys.modules prior to running tests. Fixes #301.

Comments (0)

Files changed (1)

setuptools/command/test.py

 from distutils.errors import DistutilsOptionError
 import sys
 from pkg_resources import *
+from pkg_resources import _namespace_packages
 from unittest import TestLoader, main
 
 class ScanningLoader(TestLoader):
 
     def run_tests(self):
         import unittest
+
+        # Purge modules under test from sys.modules. The test loader will
+        # re-import them from the build location. Required when 2to3 is used
+        # with namespace packages.
+        if sys.version_info >= (3,) and getattr(self.distribution, 'use_2to3', False):
+            module = self.test_args[-1].split('.')[0]
+            if module in _namespace_packages:
+                del_modules = []
+                if module in sys.modules:
+                    del_modules.append(module)
+                module += '.'
+                for name in sys.modules:
+                    if name.startswith(module):
+                        del_modules.append(name)
+                map(sys.modules.__delitem__, del_modules)
+
         loader_ep = EntryPoint.parse("x="+self.test_loader)
         loader_class = loader_ep.load(require=False)
         cks = loader_class()