diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-05-05 02:42:16 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-05-05 02:42:16 +0200 |
commit | 55cd834056e3a806f7cffead62e8cc4c23938ea3 (patch) | |
tree | 0e8312bd566aca82bc56d0f0caa244c8bed639bb | |
parent | 15b35646d95b617b04a2e556f1b0902d3f64c3cb (diff) | |
download | psutil-55cd834056e3a806f7cffead62e8cc4c23938ea3.tar.gz |
don't run namespaces test at runtime + refactor coverage test
-rw-r--r-- | psutil/tests/__init__.py | 36 | ||||
-rwxr-xr-x | psutil/tests/test_memory_leaks.py | 11 | ||||
-rw-r--r-- | psutil/tests/test_testutils.py | 6 |
3 files changed, 30 insertions, 23 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 91a519c1..28586fd3 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -1154,7 +1154,19 @@ class process_namespace: self._proc._init(self._proc.pid, _ignore_nsp=True) @classmethod - def _test(cls): + def test_class_coverage(cls, test_class, ls): + """Given a TestCase instance and a list of tuples checks that + the class defines the required test method names. + """ + for fun_name, _, _ in ls: + meth_name = 'test_' + fun_name + if not hasattr(test_class, meth_name): + msg = "%r class should define a '%s' method" % ( + test_class.__class__.__name__, meth_name) + raise AttributeError(msg) + + @classmethod + def test(cls): this = set([x[0] for x in cls.all]) ignored = set([x[0] for x in cls.ignored]) klass = set([x for x in dir(psutil.Process) if x[0] != '_']) @@ -1163,9 +1175,6 @@ class process_namespace: raise ValueError("uncovered Process class names: %r" % leftout) -process_namespace._test() - - class system_namespace: """A container that lists all the module-level, system-related APIs. Utilities such as cpu_percent() are excluded. Usage: @@ -1195,18 +1204,18 @@ class system_namespace: ('virtual_memory', (), {}), ] if HAS_CPU_FREQ: - getters.append(('cpu_freq', (), {'percpu': True})) + getters += [('cpu_freq', (), {'percpu': True})] if HAS_GETLOADAVG: - getters.append(('getloadavg', (), {})) + getters += [('getloadavg', (), {})] if HAS_SENSORS_TEMPERATURES: - getters.append(('sensors_temperatures', (), {})) + getters += [('sensors_temperatures', (), {})] if HAS_SENSORS_FANS: - getters.append(('sensors_fans', (), {})) + getters += [('sensors_fans', (), {})] if HAS_SENSORS_BATTERY: - getters.append(('sensors_battery', (), {})) + getters += [('sensors_battery', (), {})] if WINDOWS: - getters.append(('win_service_iter', (), {})) - getters.append(('win_service_get', ('alg', ), {})) + getters += [('win_service_iter', (), {})] + getters += [('win_service_get', ('alg', ), {})] ignored = [ ('process_iter', (), {}), @@ -1230,7 +1239,7 @@ class system_namespace: yield (fun, fun_name) @classmethod - def _test(cls): + def test(cls): this = set([x[0] for x in cls.all]) ignored = set([x[0] for x in cls.ignored]) # there's a separate test for __all__ @@ -1240,8 +1249,7 @@ class system_namespace: if leftout: raise ValueError("uncovered psutil mod name(s): %r" % leftout) - -system_namespace._test() + test_class_coverage = process_namespace.test_class_coverage def serialrun(klass): diff --git a/psutil/tests/test_memory_leaks.py b/psutil/tests/test_memory_leaks.py index 61049a91..b6845ac8 100755 --- a/psutil/tests/test_memory_leaks.py +++ b/psutil/tests/test_memory_leaks.py @@ -75,10 +75,8 @@ class TestProcessObjectLeaks(TestMemoryLeak): proc = thisproc def test_coverage(self): - p = psutil.Process() - ns = process_namespace(p) - for fun, name in ns.iter(ns.getters + ns.setters): - assert hasattr(self, "test_" + name), name + ns = process_namespace(None) + ns.test_class_coverage(self, ns.getters + ns.setters) @skip_if_linux() def test_name(self): @@ -326,9 +324,8 @@ class TestModuleFunctionsLeaks(TestMemoryLeak): """Test leaks of psutil module functions.""" def test_coverage(self): - ns = system_namespace - for fun, name in ns.iter(ns.all): - assert hasattr(self, "test_" + name), name + ns = system_namespace() + ns.test_class_coverage(self, ns.all) # --- cpu diff --git a/psutil/tests/test_testutils.py b/psutil/tests/test_testutils.py index 383b1470..73782079 100644 --- a/psutil/tests/test_testutils.py +++ b/psutil/tests/test_testutils.py @@ -432,7 +432,7 @@ class TestMemLeakClass(TestMemoryLeak): box = [] self.assertRaisesRegex( - AssertionError, r"1 unclosed fd\(s\) or handle\(s\)", + AssertionError, r"unclosed fd\(s\) or handle\(s\)", self.execute, fun, times=5, warmup_times=5) @@ -441,11 +441,13 @@ class TestTestingUtils(PsutilTestCase): def test_process_namespace(self): p = psutil.Process() ns = process_namespace(p) + ns.test() fun = [x for x in ns.iter(ns.getters) if x[1] == 'ppid'][0][0] self.assertEqual(fun(), p.ppid()) def test_system_namespace(self): - ns = system_namespace + ns = system_namespace() + ns.test() fun = [x for x in ns.iter(ns.getters) if x[1] == 'net_if_addrs'][0][0] self.assertEqual(fun(), psutil.net_if_addrs()) |