Skip to content

Commit

Permalink
fix #1478: add make command to re-run tests failed on last run
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Apr 4, 2019
1 parent c367b51 commit a5360cc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
the number of physical CPUs in case /proc/cpuinfo does not provide this info.
- 1458_: provide coloured test output. Also show failures on KeyboardInterrupt.
- 1464_: various docfixes (always point to python3 doc, fix links, etc.).
- 1478_: add make command to re-run tests failed on last run.

**Bug fixes**

Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ clean: ## Remove all build files.
*.egg-info \
*\$testfn* \
.coverage \
.failed-tests.txt \
.tox \
build/ \
dist/ \
Expand Down Expand Up @@ -151,6 +152,10 @@ test-by-name: ## e.g. make test-by-name ARGS=psutil.tests.test_system.TestSyste
${MAKE} install
@$(TEST_PREFIX) $(PYTHON) -m unittest -v $(ARGS)

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-coverage: ## Run test coverage.
${MAKE} install
# Note: coverage options are controlled by .coveragerc file
Expand Down
27 changes: 25 additions & 2 deletions psutil/tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@

import psutil
from psutil._common import memoize
from psutil.tests import safe_rmpath
from psutil.tests import TOX


HERE = os.path.abspath(os.path.dirname(__file__))
VERBOSITY = 1 if TOX else 2
FAILED_TESTS_FNAME = '.failed-tests.txt'
if os.name == 'posix':
GREEN = 1
RED = 2
Expand Down Expand Up @@ -157,15 +159,36 @@ def get_suite(name=None):
return suite


def run(name=None):
def get_suite_from_failed():
# ...from previously failed test run
with open(FAILED_TESTS_FNAME, 'rt') as f:
names = f.read().split()
suite = unittest.TestSuite()
for n in names:
suite.addTest(unittest.defaultTestLoader.loadTestsFromName(n))
return suite


def save_failed_tests(result):
if result.wasSuccessful():
return safe_rmpath(FAILED_TESTS_FNAME)
with open(FAILED_TESTS_FNAME, 'wt') as f:
for t in result.errors + result.failures:
tname = str(t[0])
f.write(tname + '\n')


def run(name=None, last_failed=False):
setup_tests()
runner = ColouredRunner(verbosity=VERBOSITY)
suite = get_suite_from_failed() if last_failed else get_suite(name)
try:
result = runner.run(get_suite(name))
result = runner.run(suite)
except (KeyboardInterrupt, SystemExit) as err:
print("received %s" % err.__class__.__name__, file=sys.stderr)
runner.result.printErrors()
sys.exit(1)
else:
save_failed_tests(result)
success = result.wasSuccessful()
sys.exit(0 if success else 1)
10 changes: 10 additions & 0 deletions scripts/internal/winmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ def clean():
"*.~",
"*__pycache__",
".coverage",
".failed-tests.txt",
".tox",
)
safe_rmtree("build")
Expand Down Expand Up @@ -439,6 +440,15 @@ def test_by_name():
sh("%s -m unittest -v %s" % (PYTHON, name))


@cmd
def test_failed():
"""Re-run tests which failed on last run."""
install()
test_setup()
sh('%s -c "import psutil.tests.runner as r; r.run(last_failed=True)"' % (
PYTHON))


@cmd
def test_script():
"""Quick way to test a script"""
Expand Down

0 comments on commit a5360cc

Please sign in to comment.