summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-04-24 19:38:21 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-04-24 19:38:21 +0200
commit1da757c4408911dae34a707b7a63df89cc530727 (patch)
tree586bcea3fa996e9644d0bab88b4336eae9cbbd82
parent1d6df8d52530c6143dcd0260bbcfd384d1315c8f (diff)
downloadpsutil-1018-test-refactoring.tar.gz
#1018: enable 'python -m psutil.tests' to run tests1018-test-refactoring
-rwxr-xr-x.ci/travis/run.sh4
-rw-r--r--Makefile8
-rw-r--r--appveyor.yml4
-rw-r--r--docs/index.rst12
-rw-r--r--psutil/tests/README.rst25
-rw-r--r--psutil/tests/__init__.py30
-rwxr-xr-xpsutil/tests/__main__.py14
-rwxr-xr-xpsutil/tests/runner.py37
-rwxr-xr-xsetup.py15
-rw-r--r--tox.ini2
10 files changed, 81 insertions, 70 deletions
diff --git a/.ci/travis/run.sh b/.ci/travis/run.sh
index 0f453dd7..2bc0dfe9 100755
--- a/.ci/travis/run.sh
+++ b/.ci/travis/run.sh
@@ -19,9 +19,9 @@ python setup.py develop
# run tests (with coverage)
if [[ $PYVER == '2.7' ]] && [[ "$(uname -s)" != 'Darwin' ]]; then
- coverage run psutil/tests/runner.py --include="psutil/*" --omit="test/*,*setup*"
+ coverage run psutil/tests/__main__.py --include="psutil/*" --omit="test/*,*setup*"
else
- python psutil/tests/runner.py
+ python psutil/tests/__main__.py
fi
if [ "$PYVER" == "2.7" ] || [ "$PYVER" == "3.6" ]; then
diff --git a/Makefile b/Makefile
index 40a5495c..65762df6 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@
# You can set the variables below from the command line.
PYTHON = python
-TSCRIPT = psutil/tests/runner.py
+TSCRIPT = psutil/tests/__main__.py
ARGS =
# List of nice-to-have dev libs.
@@ -121,11 +121,11 @@ test: install
# Test psutil process-related APIs.
test-process: install
- $(PYTHON) -m unittest -v psutil.tests.test_process
+ $(PYTHON) -m unittest -v psutil.test.test_process
# Test psutil system-related APIs.
test-system: install
- $(PYTHON) -m unittest -v psutil.tests.test_system
+ $(PYTHON) -m unittest -v psutil.test.test_system
# Test misc.
test-misc: install
@@ -144,7 +144,7 @@ test-platform: install
$(PYTHON) psutil/tests/test_`$(PYTHON) -c 'import psutil; print([x.lower() for x in ("LINUX", "BSD", "OSX", "SUNOS", "WINDOWS") if getattr(psutil, x)][0])'`.py
# Run a specific test by name, e.g.
-# make test-by-name psutil.tests.test_system.TestSystemAPIs.test_cpu_times
+# make test-by-name psutil.test.test_system.TestSystemAPIs.test_cpu_times
test-by-name: install
@$(PYTHON) -m unittest -v $(ARGS)
diff --git a/appveyor.yml b/appveyor.yml
index 896f550f..af7a6319 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -90,7 +90,7 @@ build: off
test_script:
- "%WITH_COMPILER% %PYTHON%/python -V"
- - "%WITH_COMPILER% %PYTHON%/python psutil/tests/runner.py"
+ - "%WITH_COMPILER% %PYTHON%/python psutil/tests/__main__.py"
after_test:
- "%WITH_COMPILER% %PYTHON%/python setup.py bdist_wheel"
@@ -118,7 +118,7 @@ only_commits:
psutil/_pswindows.py
psutil/arch/windows/*
psutil/tests/__init__.py
- psutil/tests/runner.py
+ psutil/tests/__main__.py
psutil/tests/test_memory_leaks.py
psutil/tests/test_misc.py
psutil/tests/test_process.py
diff --git a/docs/index.rst b/docs/index.rst
index fabfb22b..72c2881c 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -2193,6 +2193,18 @@ Q&A
in python as
`os.getloadavg <https://docs.python.org/2/library/os.html#os.getloadavg>`__
+Running tests
+=============
+
+There are two ways of running tests. If psutil is already installed use::
+
+ $ python -m psutil.tests
+
+You can use this method as a quick way to make sure psutil fully works on your
+platform. If you have a copy of the source code you can also use::
+
+ $ make test
+
Development guide
=================
diff --git a/psutil/tests/README.rst b/psutil/tests/README.rst
index 2ad91c14..637fb7dd 100644
--- a/psutil/tests/README.rst
+++ b/psutil/tests/README.rst
@@ -1,19 +1,24 @@
Instructions for running tests
==============================
-- The recommended way to run tests (also on Windows) is to cd into psutil root
- directory and run ``make test``.
+* There are two ways of running tests. If psutil is already installed:
-- Depending on the Python version, dependencies for running tests include
+ python -m psutil.tests
+
+ If you have a copy of the source code:
+
+ make test
+
+* Depending on the Python version, dependencies for running tests include
``ipaddress``, ``mock`` and ``unittest2`` modules.
- On Windows also ``pywin32`` and ``wmi`` modules are recommended
- (although optional).
- Run ``make setup-dev-env`` to install all deps (also on Windows).
+ On Windows you'll also need ``pywin32`` and ``wmi`` modules.
+ If you have a copy of the source code you can run ``make setup-dev-env`` to
+ install all deps (also on Windows).
-- To run tests on all supported Python versions install tox
+* To run tests on all supported Python versions install tox
(``pip install tox``) then run ``tox`` from psutil root directory.
-- Every time a commit is pushed tests are automatically run on Travis
+* Every time a commit is pushed tests are automatically run on Travis
(Linux, OSX) and appveyor (Windows):
- - https://travis-ci.org/giampaolo/psutil/
- - https://ci.appveyor.com/project/giampaolo/psutil
+ * https://travis-ci.org/giampaolo/psutil/
+ * https://ci.appveyor.com/project/giampaolo/psutil
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index 05d2de2e..84eafe9e 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -127,6 +127,7 @@ TESTFN_UNICODE = TESTFN + u"-ƒőő"
# --- paths
+HERE = os.path.abspath(os.path.dirname(__file__))
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
SCRIPTS_DIR = os.path.join(ROOT_DIR, 'scripts')
@@ -560,11 +561,23 @@ class TestCase(unittest.TestCase):
unittest.TestCase = TestCase
-def retry_before_failing(retries=NO_RETRIES):
- """Decorator which runs a test function and retries N times before
- actually failing.
- """
- return retry(exception=AssertionError, timeout=None, retries=retries)
+def get_suite():
+ testmodules = [os.path.splitext(x)[0] for x in os.listdir(HERE)
+ if x.endswith('.py') and x.startswith('test_') and not
+ x.startswith('test_memory_leaks')]
+ suite = unittest.TestSuite()
+ for tm in testmodules:
+ # ...so that the full test paths are printed on screen
+ tm = "psutil.tests.%s" % tm
+ suite.addTest(unittest.defaultTestLoader.loadTestsFromName(tm))
+ return suite
+
+
+def run_suite():
+ """Run unit tests."""
+ result = unittest.TextTestRunner(verbosity=VERBOSITY).run(get_suite())
+ success = result.wasSuccessful()
+ sys.exit(0 if success else 1)
def run_test_module_by_name(name):
@@ -578,6 +591,13 @@ def run_test_module_by_name(name):
sys.exit(0 if success else 1)
+def retry_before_failing(retries=NO_RETRIES):
+ """Decorator which runs a test function and retries N times before
+ actually failing.
+ """
+ return retry(exception=AssertionError, timeout=None, retries=retries)
+
+
def skip_on_access_denied(only_if=None):
"""Decorator to Ignore AccessDenied exceptions."""
def decorator(fun):
diff --git a/psutil/tests/__main__.py b/psutil/tests/__main__.py
new file mode 100755
index 00000000..5f7bb528
--- /dev/null
+++ b/psutil/tests/__main__.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""
+Run unit tests. This is invoked by:
+
+$ python -m psutil.tests
+"""
+
+from psutil.tests import run_suite
+run_suite()
diff --git a/psutil/tests/runner.py b/psutil/tests/runner.py
deleted file mode 100755
index 88bcd620..00000000
--- a/psutil/tests/runner.py
+++ /dev/null
@@ -1,37 +0,0 @@
-#!/usr/bin/env python
-
-# Copyright (C) 2007-2016 Giampaolo Rodola' <g.rodola@gmail.com>.
-# Use of this source code is governed by MIT license that can be
-# found in the LICENSE file.
-
-"""Script for running all test files (except memory leaks tests)."""
-
-import os
-import sys
-
-from psutil.tests import unittest
-from psutil.tests import VERBOSITY
-
-
-def get_suite():
- HERE = os.path.abspath(os.path.dirname(__file__))
- testmodules = [os.path.splitext(x)[0] for x in os.listdir(HERE)
- if x.endswith('.py') and x.startswith('test_') and not
- x.startswith('test_memory_leaks')]
- suite = unittest.TestSuite()
- for tm in testmodules:
- # ...so that "make test" will print the full test paths
- tm = "psutil.tests.%s" % tm
- suite.addTest(unittest.defaultTestLoader.loadTestsFromName(tm))
- return suite
-
-
-def main():
- # run tests
- result = unittest.TextTestRunner(verbosity=VERBOSITY).run(get_suite())
- success = result.wasSuccessful()
- sys.exit(0 if success else 1)
-
-
-if __name__ == '__main__':
- main()
diff --git a/setup.py b/setup.py
index 58a46c83..3f2fd0df 100755
--- a/setup.py
+++ b/setup.py
@@ -9,7 +9,6 @@ running processes and system utilization (CPU, memory, disks, network)
in Python.
"""
-import atexit
import contextlib
import io
import os
@@ -195,13 +194,6 @@ elif LINUX:
suffix='.c', delete=False, mode="wt") as f:
f.write("#include <linux/ethtool.h>")
- @atexit.register
- def on_exit():
- try:
- os.remove(f.name)
- except OSError:
- pass
-
compiler = UnixCCompiler()
try:
with silenced_output('stderr'):
@@ -211,6 +203,11 @@ elif LINUX:
return ("PSUTIL_ETHTOOL_MISSING_TYPES", 1)
else:
return None
+ finally:
+ try:
+ os.remove(f.name)
+ except OSError:
+ pass
ETHTOOL_MACRO = get_ethtool_macro()
@@ -270,7 +267,7 @@ def main():
license='BSD',
packages=['psutil', 'psutil.tests'],
ext_modules=extensions,
- test_suite="psutil.tests.runner.get_suite",
+ test_suite="psutil.tests.get_suite",
tests_require=['ipaddress', 'mock', 'unittest2'],
zip_safe=False, # http://stackoverflow.com/questions/19548957
# see: python setup.py register --list-classifiers
diff --git a/tox.ini b/tox.ini
index be87cf08..20b9f229 100644
--- a/tox.ini
+++ b/tox.ini
@@ -24,7 +24,7 @@ setenv =
TOX = 1
commands =
- python psutil/tests/runner.py
+ python psutil/tests/__main__.py
git ls-files | grep \\.py$ | xargs flake8
# suppress "WARNING: 'git' command found but not installed in testenv