Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coloured tests #1459

Merged
merged 3 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ include psutil/arch/windows/services.h
include psutil/tests/README.rst
include psutil/tests/__init__.py
include psutil/tests/__main__.py
include psutil/tests/runner.py
include psutil/tests/test_aix.py
include psutil/tests/test_bsd.py
include psutil/tests/test_connections.py
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ test: ## Run all tests.

test-process: ## Run process-related API tests.
${MAKE} install
$(TEST_PREFIX) $(PYTHON) -m unittest -v psutil.tests.test_process
$(TEST_PREFIX) $(PYTHON) psutil/tests/test_process.py

test-system: ## Run system-related API tests.
${MAKE} install
$(TEST_PREFIX) $(PYTHON) -m unittest -v psutil.tests.test_system
$(TEST_PREFIX) $(PYTHON) psutil/tests/test_system.py

test-misc: ## Run miscellaneous tests.
${MAKE} install
Expand Down
50 changes: 4 additions & 46 deletions psutil/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
'APPVEYOR', 'DEVNULL', 'GLOBAL_TIMEOUT', 'MEMORY_TOLERANCE', 'NO_RETRIES',
'PYPY', 'PYTHON_EXE', 'ROOT_DIR', 'SCRIPTS_DIR', 'TESTFILE_PREFIX',
'TESTFN', 'TESTFN_UNICODE', 'TOX', 'TRAVIS', 'VALID_PROC_STATUSES',
'VERBOSITY',
"HAS_CPU_AFFINITY", "HAS_CPU_FREQ", "HAS_ENVIRON", "HAS_PROC_IO_COUNTERS",
"HAS_IONICE", "HAS_MEMORY_MAPS", "HAS_PROC_CPU_NUM", "HAS_RLIMIT",
"HAS_SENSORS_BATTERY", "HAS_BATTERY", "HAS_SENSORS_FANS",
Expand Down Expand Up @@ -127,8 +126,6 @@
MEMORY_TOLERANCE = 500 * 1024 # 500KB
# the timeout used in functions which have to wait
GLOBAL_TIMEOUT = 3 if TRAVIS or APPVEYOR else 0.5
# test output verbosity
VERBOSITY = 1 if os.getenv('SILENT') or TOX else 2
# be more tolerant if we're on travis / appveyor in order to avoid
# false positives
if TRAVIS or APPVEYOR:
Expand Down Expand Up @@ -802,9 +799,11 @@ class TestCase(unittest.TestCase):
# Print a full path representation of the single unit tests
# being run.
def __str__(self):
fqmod = self.__class__.__module__
if not fqmod.startswith('psutil.'):
fqmod = 'psutil.tests.' + fqmod
return "%s.%s.%s" % (
self.__class__.__module__, self.__class__.__name__,
self._testMethodName)
fqmod, self.__class__.__name__, self._testMethodName)

# assertRaisesRegexp renamed to assertRaisesRegex in 3.3;
# add support for the new name.
Expand All @@ -816,47 +815,6 @@ def __str__(self):
unittest.TestCase = TestCase


def _setup_tests():
if 'PSUTIL_TESTING' not in os.environ:
# This won't work on Windows but set_testing() below will do it.
os.environ['PSUTIL_TESTING'] = '1'
psutil._psplatform.cext.set_testing()


def get_suite():
testmods = [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')]
if "WHEELHOUSE_UPLOADER_USERNAME" in os.environ:
testmods = [x for x in testmods if not x.endswith((
"osx", "posix", "linux"))]
suite = unittest.TestSuite()
for tm in testmods:
# ...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():
_setup_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):
# testmodules = [os.path.splitext(x)[0] for x in os.listdir(HERE)
# if x.endswith('.py') and x.startswith('test_')]
_setup_tests()
name = os.path.splitext(os.path.basename(name))[0]
suite = unittest.TestSuite()
suite.addTest(unittest.defaultTestLoader.loadTestsFromName(name))
result = unittest.TextTestRunner(verbosity=VERBOSITY).run(suite)
success = result.wasSuccessful()
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.
Expand Down
2 changes: 1 addition & 1 deletion psutil/tests/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from urllib2 import urlopen

from psutil.tests import PYTHON_EXE
from psutil.tests import run_suite
from psutil.tests.runner import run_suite


HERE = os.path.abspath(os.path.dirname(__file__))
Expand Down
122 changes: 122 additions & 0 deletions psutil/tests/runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/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.

"""
Unit test runner, providing colourized output.
"""

import os
import sys
import unittest
from unittest import TestResult
from unittest import TextTestResult
from unittest import TextTestRunner

import psutil
from psutil.tests import TOX


HERE = os.path.abspath(os.path.dirname(__file__))
VERBOSITY = 1 if TOX else 2
GREEN = 1
RED = 2
BROWN = 94


def term_supports_colors(file=sys.stdout):
try:
import curses
assert file.isatty()
curses.setupterm()
assert curses.tigetnum("colors") > 0
except Exception:
return False
else:
return True


def hilite(s, color, bold=False):
"""Return an highlighted version of 'string'."""
attr = []
if color == GREEN:
attr.append('32')
elif color == RED:
attr.append('91')
elif color == BROWN:
attr.append('33')
else:
raise ValueError("unrecognized color")
if bold:
attr.append('1')
return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), s)


class ColouredResult(unittest.TextTestResult):

def addSuccess(self, test):
TestResult.addSuccess(self, test)
self.stream.writeln(hilite("OK", GREEN))

def addError(self, test, err):
TestResult.addError(self, test, err)
self.stream.writeln(hilite("ERROR", RED, bold=True))

def addFailure(self, test, err):
TestResult.addFailure(self, test, err)
self.stream.writeln(hilite("FAIL", RED))

def addSkip(self, test, reason):
TestResult.addSkip(self, test, reason)
self.stream.writeln(hilite("skipped: %s" % reason, BROWN))

def printErrorList(self, flavour, errors):
flavour = hilite(flavour, RED)
TextTestResult.printErrorList(self, flavour, errors)


class ColouredRunner(TextTestRunner):
resultclass = ColouredResult if term_supports_colors() else TextTestResult


def setup_tests():
if 'PSUTIL_TESTING' not in os.environ:
# This won't work on Windows but set_testing() below will do it.
os.environ['PSUTIL_TESTING'] = '1'
psutil._psplatform.cext.set_testing()


def get_suite():
testmods = [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')]
if "WHEELHOUSE_UPLOADER_USERNAME" in os.environ:
testmods = [x for x in testmods if not x.endswith((
"osx", "posix", "linux"))]
suite = unittest.TestSuite()
for tm in testmods:
# ...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_test_module_by_name(name):
# testmodules = [os.path.splitext(x)[0] for x in os.listdir(HERE)
# if x.endswith('.py') and x.startswith('test_')]
setup_tests()
name = os.path.splitext(os.path.basename(name))[0]
suite = unittest.TestSuite()
suite.addTest(unittest.defaultTestLoader.loadTestsFromName(name))
result = ColouredRunner(verbosity=VERBOSITY).run(suite)
success = result.wasSuccessful()
sys.exit(0 if success else 1)


def run_suite():
setup_tests()
result = ColouredRunner(verbosity=VERBOSITY).run(get_suite())
success = result.wasSuccessful()
sys.exit(0 if success else 1)
2 changes: 1 addition & 1 deletion psutil/tests/test_aix.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import re

from psutil import AIX
from psutil.tests import run_test_module_by_name
from psutil.tests import sh
from psutil.tests import unittest
import psutil
Expand Down Expand Up @@ -118,4 +117,5 @@ def test_net_if_addrs_names(self):


if __name__ == '__main__':
from psutil.tests.runner import run_test_module_by_name
run_test_module_by_name(__file__)
2 changes: 1 addition & 1 deletion psutil/tests/test_bsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from psutil.tests import MEMORY_TOLERANCE
from psutil.tests import reap_children
from psutil.tests import retry_before_failing
from psutil.tests import run_test_module_by_name
from psutil.tests import sh
from psutil.tests import unittest
from psutil.tests import which
Expand Down Expand Up @@ -550,4 +549,5 @@ def test_cpu_stats_ctx_switches(self):


if __name__ == '__main__':
from psutil.tests.runner import run_test_module_by_name
run_test_module_by_name(__file__)
2 changes: 1 addition & 1 deletion psutil/tests/test_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from psutil.tests import HAS_CONNECTIONS_UNIX
from psutil.tests import pyrun
from psutil.tests import reap_children
from psutil.tests import run_test_module_by_name
from psutil.tests import safe_rmpath
from psutil.tests import skip_on_access_denied
from psutil.tests import tcp_socketpair
Expand Down Expand Up @@ -523,4 +522,5 @@ def test_connection_constants(self):


if __name__ == '__main__':
from psutil.tests.runner import run_test_module_by_name
run_test_module_by_name(__file__)
2 changes: 1 addition & 1 deletion psutil/tests/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from psutil.tests import HAS_SENSORS_FANS
from psutil.tests import HAS_SENSORS_TEMPERATURES
from psutil.tests import is_namedtuple
from psutil.tests import run_test_module_by_name
from psutil.tests import safe_rmpath
from psutil.tests import skip_on_access_denied
from psutil.tests import TESTFN
Expand Down Expand Up @@ -654,4 +653,5 @@ def environ(self, ret, proc):


if __name__ == '__main__':
from psutil.tests.runner import run_test_module_by_name
run_test_module_by_name(__file__)
2 changes: 1 addition & 1 deletion psutil/tests/test_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from psutil.tests import reap_children
from psutil.tests import reload_module
from psutil.tests import retry_before_failing
from psutil.tests import run_test_module_by_name
from psutil.tests import safe_rmpath
from psutil.tests import sh
from psutil.tests import skip_on_not_implemented
Expand Down Expand Up @@ -2080,4 +2079,5 @@ def test_cat(self):


if __name__ == '__main__':
from psutil.tests.runner import run_test_module_by_name
run_test_module_by_name(__file__)
2 changes: 1 addition & 1 deletion psutil/tests/test_memory_leaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
from psutil.tests import HAS_SENSORS_FANS
from psutil.tests import HAS_SENSORS_TEMPERATURES
from psutil.tests import reap_children
from psutil.tests import run_test_module_by_name
from psutil.tests import safe_rmpath
from psutil.tests import skip_on_access_denied
from psutil.tests import TESTFN
Expand Down Expand Up @@ -607,4 +606,5 @@ def test_win_service_get_description(self):


if __name__ == '__main__':
from psutil.tests.runner import run_test_module_by_name
run_test_module_by_name(__file__)
2 changes: 1 addition & 1 deletion psutil/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
from psutil.tests import reload_module
from psutil.tests import retry
from psutil.tests import ROOT_DIR
from psutil.tests import run_test_module_by_name
from psutil.tests import safe_mkdir
from psutil.tests import safe_rmpath
from psutil.tests import SCRIPTS_DIR
Expand Down Expand Up @@ -1054,4 +1053,5 @@ def test_is_namedtuple(self):


if __name__ == '__main__':
from psutil.tests.runner import run_test_module_by_name
run_test_module_by_name(__file__)
2 changes: 1 addition & 1 deletion psutil/tests/test_osx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from psutil.tests import MEMORY_TOLERANCE
from psutil.tests import reap_children
from psutil.tests import retry_before_failing
from psutil.tests import run_test_module_by_name
from psutil.tests import sh
from psutil.tests import unittest

Expand Down Expand Up @@ -291,4 +290,5 @@ def test_sensors_battery(self):


if __name__ == '__main__':
from psutil.tests.runner import run_test_module_by_name
run_test_module_by_name(__file__)
2 changes: 1 addition & 1 deletion psutil/tests/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from psutil.tests import PYTHON_EXE
from psutil.tests import reap_children
from psutil.tests import retry_before_failing
from psutil.tests import run_test_module_by_name
from psutil.tests import sh
from psutil.tests import skip_on_access_denied
from psutil.tests import TRAVIS
Expand Down Expand Up @@ -437,4 +436,5 @@ def df(device):


if __name__ == '__main__':
from psutil.tests.runner import run_test_module_by_name
run_test_module_by_name(__file__)
2 changes: 1 addition & 1 deletion psutil/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
from psutil.tests import PYTHON_EXE
from psutil.tests import reap_children
from psutil.tests import retry_before_failing
from psutil.tests import run_test_module_by_name
from psutil.tests import safe_rmpath
from psutil.tests import sh
from psutil.tests import skip_on_access_denied
Expand Down Expand Up @@ -1608,4 +1607,5 @@ def test_kill_terminate(self):


if __name__ == '__main__':
from psutil.tests.runner import run_test_module_by_name
run_test_module_by_name(__file__)
2 changes: 1 addition & 1 deletion psutil/tests/test_sunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import psutil
from psutil import SUNOS
from psutil.tests import run_test_module_by_name
from psutil.tests import sh
from psutil.tests import unittest

Expand Down Expand Up @@ -42,4 +41,5 @@ def test_cpu_count(self):


if __name__ == '__main__':
from psutil.tests.runner import run_test_module_by_name
run_test_module_by_name(__file__)
Loading