Issue 4886: test/regrtest.py contains error on import (original) (raw)

I want to use the included test.regrtest (located in $pythondir/lib/python[ver]/test) to regression test some of my own scripts located in directory myDir. The script has some nice configurability features to skip some tests based on machine configurations, etc. that a vanilla unittest suite doesn't include automatically. However, it seems that the script regrtest.py has an error following the import line.

Here's my setup:

/sample.py /myDir /init.py /test_file1.py /test_file2.py /...

in sample.py:

{{{ #!python #!/usr/bin/env python import test.regrtest as rt rt.main(tests = ['myDir.test_file1','myDir.test_file2'],
quiet = False, verbose = True) }}}

running sample.py yields:

{{{ #!sh myDir.test_file1 myDir.test_file1 skipped -- No module named myDir.test_file1 myDir.test_file2 myDir.test_file2 skipped -- No module named myDir.test_file2 2 tests skipped: myDir.test_file1 myDir.test_file2 2 skips unexpected on win32: myDir.test_file1 myDir.test_file2 }}}

This is due to the code snippet in regrtest.py around line 554:

{{{ #!python ... if test.startswith('test.'): abstest = test else: # Always import it from the test package abstest = 'test.' + test the_package = import(abstest, globals(), locals(), []) the_module = getattr(the_package, test) ... }}}

should be changed to: {{{ #!python ... if test.startswith('test.'): abstest = test modName = test[len('test.'):] else: # Always import it from the test package abstest = 'test.' + test modName = test the_package = import(abstest, globals(), locals(), []) the_module = getattr(the_package, modName) ... }}}

This way, the the_module will correctly find the module name in 'the_package'.

A further recommendation: the main() module should be able to work with test directories with name other than 'test.*'. Otherwise, users wishing to use the main() on other directories will have to name them 'test.' Depending on the user's directory's position in sys.path (before or after $pythondir/lib/python[ver]), regrtest.main() will either fail due to the 'from test import ...' statements being shadowed by the user's 'test' directory or the user's 'test' directory being shadowed by the standard library's.