diff --git a/.ci/travis/run.sh b/.ci/travis/run.sh index 818386336..9a4afa62f 100755 --- a/.ci/travis/run.sh +++ b/.ci/travis/run.sh @@ -20,9 +20,9 @@ python setup.py develop # run tests (with coverage) if [[ $PYVER == '2.7' ]] && [[ "$(uname -s)" != 'Darwin' ]]; then - PSUTIL_TESTING=1 python -Wa -m coverage run psutil/tests/__main__.py + PSUTIL_TESTING=1 python -Wa -m coverage run psutil/tests/runner.py else - PSUTIL_TESTING=1 python -Wa psutil/tests/__main__.py + PSUTIL_TESTING=1 python -Wa psutil/tests/runner.py fi if [ "$PYVER" == "2.7" ] || [ "$PYVER" == "3.6" ]; then diff --git a/Makefile b/Makefile index 812ba9c2c..0dadfca3f 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # You can set the variables below from the command line. PYTHON = python3 -TSCRIPT = psutil/tests/__main__.py +TSCRIPT = psutil/tests/runner.py ARGS = # List of nice-to-have dev libs. DEPS = \ @@ -155,7 +155,7 @@ test-by-name: ## e.g. make test-by-name ARGS=psutil.tests.test_system.TestSyste test-failed: ## Re-run tests which failed on last run ${MAKE} install - $(TEST_PREFIX) $(PYTHON) -c "import psutil.tests.runner as r; r.run(last_failed=True)" + $(TEST_PREFIX) $(PYTHON) $(TSCRIPT) --last-failed test-coverage: ## Run test coverage. ${MAKE} install diff --git a/make.bat b/make.bat index ae897aa5f..8e60811cc 100644 --- a/make.bat +++ b/make.bat @@ -28,7 +28,7 @@ if "%PYTHON%" == "" ( ) if "%TSCRIPT%" == "" ( - set TSCRIPT=psutil\tests\__main__.py + set TSCRIPT=psutil\tests\runner.py ) rem Needed to locate the .pypirc file and upload exes on PyPI. diff --git a/psutil/tests/README.rst b/psutil/tests/README.rst index b647d5138..9b870d5b1 100644 --- a/psutil/tests/README.rst +++ b/psutil/tests/README.rst @@ -4,13 +4,12 @@ Instructions for running tests * There are two ways of running tests. As a "user", if psutil is already installed and you just want to test it works:: - python -m psutil.tests --install-deps # install test deps python -m psutil.tests As a "developer", if you have a copy of the source code and you wish to hack on psutil:: - make setup-dev-env # install test deps (+ other things) + make setup-dev-env # install missing third-party deps make test * To run tests on all supported Python versions install tox diff --git a/psutil/tests/__main__.py b/psutil/tests/__main__.py index 9dd0804cc..d5cd02eb1 100755 --- a/psutil/tests/__main__.py +++ b/psutil/tests/__main__.py @@ -6,89 +6,8 @@ """ Run unit tests. This is invoked by: - $ python -m psutil.tests """ -import contextlib -import optparse -import os -import sys -import tempfile -try: - from urllib.request import urlopen # py3 -except ImportError: - from urllib2 import urlopen - -from psutil.tests import PYTHON_EXE -from psutil.tests.runner import run - - -HERE = os.path.abspath(os.path.dirname(__file__)) -GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py" -TEST_DEPS = [] -if sys.version_info[:2] == (2, 6): - TEST_DEPS.extend(["ipaddress", "unittest2", "argparse", "mock==1.0.1"]) -elif sys.version_info[:2] == (2, 7) or sys.version_info[:2] <= (3, 2): - TEST_DEPS.extend(["ipaddress", "mock"]) - - -def install_pip(): - try: - import pip # NOQA - except ImportError: - import ssl - f = tempfile.NamedTemporaryFile(suffix='.py') - with contextlib.closing(f): - print("downloading %s to %s" % (GET_PIP_URL, f.name)) - if hasattr(ssl, '_create_unverified_context'): - ctx = ssl._create_unverified_context() - else: - ctx = None - kwargs = dict(context=ctx) if ctx else {} - req = urlopen(GET_PIP_URL, **kwargs) - data = req.read() - f.write(data) - f.flush() - - print("installing pip") - code = os.system('%s %s --user' % (PYTHON_EXE, f.name)) - return code - - -def install_test_deps(deps=None): - """Install test dependencies via pip.""" - if deps is None: - deps = TEST_DEPS - deps = set(deps) - if deps: - is_venv = hasattr(sys, 'real_prefix') - opts = "--user" if not is_venv else "" - install_pip() - code = os.system('%s -m pip install %s --upgrade %s' % ( - PYTHON_EXE, opts, " ".join(deps))) - return code - - -def main(): - usage = "%s -m psutil.tests [opts]" % PYTHON_EXE - parser = optparse.OptionParser(usage=usage, description="run unit tests") - parser.add_option("-i", "--install-deps", - action="store_true", default=False, - help="don't print status messages to stdout") - - opts, args = parser.parse_args() - if opts.install_deps: - install_pip() - install_test_deps() - else: - for dep in TEST_DEPS: - try: - __import__(dep.split("==")[0]) - except ImportError: - sys.exit("%r lib is not installed; run %s -m psutil.tests " - "--install-deps" % (dep, PYTHON_EXE)) - run() - - +from .runner import main main() diff --git a/psutil/tests/runner.py b/psutil/tests/runner.py index 589117b80..2e9264bd4 100755 --- a/psutil/tests/runner.py +++ b/psutil/tests/runner.py @@ -12,6 +12,7 @@ """ from __future__ import print_function +import optparse import os import sys import unittest @@ -145,3 +146,17 @@ def run(name=None, last_failed=False): save_failed_tests(result) success = result.wasSuccessful() sys.exit(0 if success else 1) + + +def main(): + usage = "python3 -m psutil.tests [opts]" + parser = optparse.OptionParser(usage=usage, description="run unit tests") + parser.add_option("--last-failed", + action="store_true", default=False, + help="only run last failed tests") + opts, args = parser.parse_args() + run(last_failed=opts.last_failed) + + +if __name__ == '__main__': + main() diff --git a/scripts/internal/winmake.py b/scripts/internal/winmake.py index aba3595cf..f54211d93 100755 --- a/scripts/internal/winmake.py +++ b/scripts/internal/winmake.py @@ -31,7 +31,7 @@ PYTHON = sys.executable else: PYTHON = os.getenv('PYTHON', sys.executable) -TEST_SCRIPT = 'psutil\\tests\\__main__.py' +TEST_SCRIPT = 'psutil\\tests\\runner.py' GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py" PY3 = sys.version_info[0] == 3 HERE = os.path.abspath(os.path.dirname(__file__)) diff --git a/tox.ini b/tox.ini index 2698eb6a8..b148642ba 100644 --- a/tox.ini +++ b/tox.ini @@ -18,7 +18,7 @@ deps = setenv = TOX = 1 -commands = python psutil/tests/__main__.py +commands = python psutil/tests/runner.py usedevelop = True