If one test module imports another, then nosetests will import the same module twice; once as the proper tests.full.path.name and once as just name.
To replicate, mkdir tests, then write the following into tests/a.py:
import sys
import traceback
sys.stderr.write("A %s executing as %s, recognized as %s in sys.modules\n\n\n" % (__file__, __name__, sys.modules[__name__]))
traceback.print_stack()
sys.stderr.write("\n\n\n")
class TestCases:
def test_a(self):
sys.stderr.write("TEST A\n")
And the following into tests/b.py:
import sys
sys.stderr.write("B %s executing as %s, recognized as %s in sys.modules\n" % (__file__, __name__, sys.modules[__name__]))
import tests.a
class TestCases:
def test_a(self):
sys.stderr.write("TEST B\n")
Then running nosetests tests/a.py tests/b.py gives the output:
A /big_share2/pacific/user/obenkiki/npl/todox/tests/a.py executing as a, recognized as <module 'a' from '/big_share2/pacific/user/obenkiki/npl/todox/tests/a.py'> in sys.modules
File "/common/pkgs/python/3.6.0/bin/nosetests", line 11, in <module>
sys.exit(run_exit())
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/core.py", line 121, in __init__
**extra_args)
File "/common/pkgs/python/3.6.0/lib/python3.6/unittest/main.py", line 93, in __init__
self.parseArgs(argv)
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/core.py", line 179, in parseArgs
self.createTests()
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/core.py", line 193, in createTests
self.test = self.testLoader.loadTestsFromNames(self.testNames)
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/loader.py", line 481, in loadTestsFromNames
return unittest.TestLoader.loadTestsFromNames(self, names, module)
File "/common/pkgs/python/3.6.0/lib/python3.6/unittest/loader.py", line 219, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/common/pkgs/python/3.6.0/lib/python3.6/unittest/loader.py", line 219, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/common/pkgs/python/3.6.0/lib/python3.6/imp.py", line 234, in load_module
return load_source(name, filename, file)
File "/common/pkgs/python/3.6.0/lib/python3.6/imp.py", line 172, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 675, in _load
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/big_share2/pacific/user/obenkiki/npl/todox/tests/a.py", line 5, in <module>
traceback.print_stack()
B /big_share2/pacific/user/obenkiki/npl/todox/tests/b.py executing as b, recognized as <module 'b' from '/big_share2/pacific/user/obenkiki/npl/todox/tests/b.py'> in sys.modules
A /big_share2/pacific/user/obenkiki/npl/todox/tests/a.py executing as tests.a, recognized as <module 'tests.a' from '/big_share2/pacific/user/obenkiki/npl/todox/tests/a.py'> in sys.modules
File "/common/pkgs/python/3.6.0/bin/nosetests", line 11, in <module>
sys.exit(run_exit())
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/core.py", line 121, in __init__
**extra_args)
File "/common/pkgs/python/3.6.0/lib/python3.6/unittest/main.py", line 93, in __init__
self.parseArgs(argv)
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/core.py", line 179, in parseArgs
self.createTests()
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/core.py", line 193, in createTests
self.test = self.testLoader.loadTestsFromNames(self.testNames)
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/loader.py", line 481, in loadTestsFromNames
return unittest.TestLoader.loadTestsFromNames(self, names, module)
File "/common/pkgs/python/3.6.0/lib/python3.6/unittest/loader.py", line 219, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/common/pkgs/python/3.6.0/lib/python3.6/unittest/loader.py", line 219, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/common/pkgs/python/3.6.0/lib/python3.6/site-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/common/pkgs/python/3.6.0/lib/python3.6/imp.py", line 234, in load_module
return load_source(name, filename, file)
File "/common/pkgs/python/3.6.0/lib/python3.6/imp.py", line 172, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 675, in _load
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/big_share2/pacific/user/obenkiki/npl/todox/tests/b.py", line 5, in <module>
import tests.a
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/big_share2/pacific/user/obenkiki/npl/todox/tests/a.py", line 5, in <module>
traceback.print_stack()
TEST A
.TEST B
.
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
Note that the a module is imported twice, which isn't what I expected. Is there a workaround?
My nosetests --version says 1.3.7, if that matters.
If one test module imports another, then
nosetests
will import the same module twice; once as the propertests.full.path.name
and once as justname
.To replicate,
mkdir tests
, then write the following intotests/a.py
:And the following into
tests/b.py
:Then running
nosetests tests/a.py tests/b.py
gives the output:Note that the
a
module is imported twice, which isn't what I expected. Is there a workaround?My
nosetests --version
says1.3.7
, if that matters.Thanks,
Oren.