From 43566aee12a177a7aef5d732abf768251e6b8a12 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 2 May 2017 18:26:25 +0300 Subject: [PATCH] [2.7] bpo-30223: Add Lib/test/__main__.py. (#1373) To unify running tests in Python 2.7 and Python 3, the test package can be run as a script. This is equivalent to running the test.regrtest module as a script. --- Doc/library/test.rst | 6 +++++- Lib/test/__main__.py | 3 +++ Lib/test/pickletester.py | 2 ++ Lib/test/regrtest.py | 25 +++++++++++++++---------- Lib/test/test_memoryio.py | 1 + Misc/NEWS | 4 ++++ 6 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 Lib/test/__main__.py diff --git a/Doc/library/test.rst b/Doc/library/test.rst index c32b93dae7..eef5d16c24 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -185,6 +185,10 @@ top-level directory where Python was built. On Windows, executing :program:`rt.bat` from your :file:`PCBuild` directory will run all regression tests. +.. versionchanged:: 2.7.14 + The :mod:`test` package can be run as a script: :program:`python -m test`. + This works the same as running the :mod:`test.regrtest` module. + :mod:`test.support` --- Utility functions for tests =================================================== @@ -195,7 +199,7 @@ tests. .. note:: The :mod:`test.test_support` module has been renamed to :mod:`test.support` - in Python 3.x and 2.7.13. The name ``test.test_support`` has been retained + in Python 3.x and 2.7.14. The name ``test.test_support`` has been retained as an alias in 2.7. The :mod:`test.support` module provides support for Python's regression diff --git a/Lib/test/__main__.py b/Lib/test/__main__.py new file mode 100644 index 0000000000..d5fbe159d7 --- /dev/null +++ b/Lib/test/__main__.py @@ -0,0 +1,3 @@ +from test import regrtest + +regrtest.main_in_temp_cwd() diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index 138e17e2ba..426839477a 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +from __future__ import absolute_import + import unittest import pickle import cPickle diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py index 8017de9794..cbd9185257 100755 --- a/Lib/test/regrtest.py +++ b/Lib/test/regrtest.py @@ -1645,16 +1645,8 @@ class _ExpectedSkips: assert self.isvalid() return self.expected -if __name__ == '__main__': - # findtestdir() gets the dirname out of __file__, so we have to make it - # absolute before changing the working directory. - # For example __file__ may be relative when running trace or profile. - # See issue #9323. - __file__ = os.path.abspath(__file__) - - # sanity check - assert __file__ == os.path.abspath(sys.argv[0]) - +def main_in_temp_cwd(): + """Run main() in a temporary working directory.""" # When tests are run from the Python build directory, it is best practice # to keep the test files in a subfolder. It eases the cleanup of leftover # files using command "make distclean". @@ -1677,3 +1669,16 @@ if __name__ == '__main__': # available from test_support.SAVEDCWD. with test_support.temp_cwd(TESTCWD, quiet=True): main() + +if __name__ == '__main__': + # findtestdir() gets the dirname out of __file__, so we have to make it + # absolute before changing the working directory. + # For example __file__ may be relative when running trace or profile. + # See issue #9323. + global __file__ + __file__ = os.path.abspath(__file__) + + # sanity check + assert __file__ == os.path.abspath(sys.argv[0]) + + main_in_temp_cwd() diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py index 2c18ad71f7..d29f1481d4 100644 --- a/Lib/test/test_memoryio.py +++ b/Lib/test/test_memoryio.py @@ -5,6 +5,7 @@ BytesIO -- for bytes from __future__ import unicode_literals from __future__ import print_function +from __future__ import absolute_import import unittest from test import test_support as support diff --git a/Misc/NEWS b/Misc/NEWS index ac4e1f320d..b510725a5d 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -148,6 +148,10 @@ Build Tests ----- +- bpo-30223: To unify running tests in Python 2.7 and Python 3, the test + package can be run as a script. This is equivalent to running the + test.regrtest module as a script. + - bpo-30207: To simplify backports from Python 3, the test.test_support module was converted into a package and renamed to test.support. The test.script_helper module was moved into the test.support package. -- 2.50.1