Skip to content

Commit 43566ae

Browse files
[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.
1 parent 2c7085f commit 43566ae

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

Doc/library/test.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ top-level directory where Python was built. On Windows, executing
185185
:program:`rt.bat` from your :file:`PCBuild` directory will run all regression
186186
tests.
187187

188+
.. versionchanged:: 2.7.14
189+
The :mod:`test` package can be run as a script: :program:`python -m test`.
190+
This works the same as running the :mod:`test.regrtest` module.
191+
188192

189193
:mod:`test.support` --- Utility functions for tests
190194
===================================================
@@ -195,7 +199,7 @@ tests.
195199
.. note::
196200

197201
The :mod:`test.test_support` module has been renamed to :mod:`test.support`
198-
in Python 3.x and 2.7.13. The name ``test.test_support`` has been retained
202+
in Python 3.x and 2.7.14. The name ``test.test_support`` has been retained
199203
as an alias in 2.7.
200204

201205
The :mod:`test.support` module provides support for Python's regression

Lib/test/__main__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from test import regrtest
2+
3+
regrtest.main_in_temp_cwd()

Lib/test/pickletester.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import
3+
24
import unittest
35
import pickle
46
import cPickle

Lib/test/regrtest.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,16 +1645,8 @@ def getexpected(self):
16451645
assert self.isvalid()
16461646
return self.expected
16471647

1648-
if __name__ == '__main__':
1649-
# findtestdir() gets the dirname out of __file__, so we have to make it
1650-
# absolute before changing the working directory.
1651-
# For example __file__ may be relative when running trace or profile.
1652-
# See issue #9323.
1653-
__file__ = os.path.abspath(__file__)
1654-
1655-
# sanity check
1656-
assert __file__ == os.path.abspath(sys.argv[0])
1657-
1648+
def main_in_temp_cwd():
1649+
"""Run main() in a temporary working directory."""
16581650
# When tests are run from the Python build directory, it is best practice
16591651
# to keep the test files in a subfolder. It eases the cleanup of leftover
16601652
# files using command "make distclean".
@@ -1677,3 +1669,16 @@ def getexpected(self):
16771669
# available from test_support.SAVEDCWD.
16781670
with test_support.temp_cwd(TESTCWD, quiet=True):
16791671
main()
1672+
1673+
if __name__ == '__main__':
1674+
# findtestdir() gets the dirname out of __file__, so we have to make it
1675+
# absolute before changing the working directory.
1676+
# For example __file__ may be relative when running trace or profile.
1677+
# See issue #9323.
1678+
global __file__
1679+
__file__ = os.path.abspath(__file__)
1680+
1681+
# sanity check
1682+
assert __file__ == os.path.abspath(sys.argv[0])
1683+
1684+
main_in_temp_cwd()

Lib/test/test_memoryio.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from __future__ import unicode_literals
77
from __future__ import print_function
8+
from __future__ import absolute_import
89

910
import unittest
1011
from test import test_support as support

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ Build
148148
Tests
149149
-----
150150

151+
- bpo-30223: To unify running tests in Python 2.7 and Python 3, the test
152+
package can be run as a script. This is equivalent to running the
153+
test.regrtest module as a script.
154+
151155
- bpo-30207: To simplify backports from Python 3, the test.test_support
152156
module was converted into a package and renamed to test.support. The
153157
test.script_helper module was moved into the test.support package.

0 commit comments

Comments
 (0)