diff options
author | Denis Laxalde <denis.laxalde@logilab.fr> | 2016-03-03 12:01:09 +0100 |
---|---|---|
committer | Denis Laxalde <denis.laxalde@logilab.fr> | 2016-03-03 12:01:09 +0100 |
commit | 66606c8f9c3ea276d61c1a5ac5fe7819ce6410f1 (patch) | |
tree | 9c664af5a0b57e58869159cb2760c370f6f56b11 | |
parent | e80d8ec5a0f6865912a442a75bae2cc587cfd064 (diff) | |
download | logilab-common-66606c8f9c3ea276d61c1a5ac5fe7819ce6410f1.tar.gz |
Move coverage utility from pytest to testlib
This allows client libraries to avoid importing pytest module which has
spurious monkeypatches of unittest.
Drop deprecated API about coverage control.
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | logilab/common/pytest.py | 59 | ||||
-rw-r--r-- | logilab/common/testlib.py | 37 | ||||
-rw-r--r-- | test/unittest_pytest.py | 15 |
4 files changed, 42 insertions, 74 deletions
@@ -1,6 +1,11 @@ ChangeLog for logilab.common ============================ +UNRELEASED -- 1.2.0 + + * pytest: TraceController class, pause_tracing and resume_tracing + functions, deprecated from 0.63.1, got removed + 2015-10-12 -- 1.1.0 * configuration: have a stable order for sections (#298658) diff --git a/logilab/common/pytest.py b/logilab/common/pytest.py index c1dca63..11647b7 100644 --- a/logilab/common/pytest.py +++ b/logilab/common/pytest.py @@ -121,7 +121,6 @@ import types import inspect import traceback from inspect import isgeneratorfunction, isclass -from contextlib import contextmanager from random import shuffle from itertools import dropwhile @@ -130,7 +129,7 @@ from logilab.common import textutils from logilab.common import testlib, STD_BLACKLIST # use the same unittest module as testlib from logilab.common.testlib import unittest, start_interactive_mode -from logilab.common.deprecation import deprecated +from logilab.common.testlib import nocoverage, pause_trace, replace_trace # bwcompat from logilab.common.debugger import Debugger, colorize_source import doctest @@ -152,62 +151,6 @@ except ImportError: CONF_FILE = 'pytestconf.py' -## coverage pausing tools - -@contextmanager -def replace_trace(trace=None): - """A context manager that temporary replaces the trace function""" - oldtrace = sys.gettrace() - sys.settrace(trace) - try: - yield - finally: - # specific hack to work around a bug in pycoverage, see - # https://bitbucket.org/ned/coveragepy/issue/123 - if (oldtrace is not None and not callable(oldtrace) and - hasattr(oldtrace, 'pytrace')): - oldtrace = oldtrace.pytrace - sys.settrace(oldtrace) - - -def pause_trace(): - """A context manager that temporary pauses any tracing""" - return replace_trace() - -class TraceController(object): - ctx_stack = [] - - @classmethod - @deprecated('[lgc 0.63.1] Use the pause_trace() context manager') - def pause_tracing(cls): - cls.ctx_stack.append(pause_trace()) - cls.ctx_stack[-1].__enter__() - - @classmethod - @deprecated('[lgc 0.63.1] Use the pause_trace() context manager') - def resume_tracing(cls): - cls.ctx_stack.pop().__exit__(None, None, None) - - -pause_tracing = TraceController.pause_tracing -resume_tracing = TraceController.resume_tracing - - -def nocoverage(func): - """Function decorator that pauses tracing functions""" - if hasattr(func, 'uncovered'): - return func - func.uncovered = True - - def not_covered(*args, **kwargs): - with pause_trace(): - return func(*args, **kwargs) - not_covered.uncovered = True - return not_covered - -## end of coverage pausing tools - - TESTFILE_RE = re.compile("^((unit)?test.*|smoketest)\.py$") def this_is_a_testfile(filename): """returns True if `filename` seems to be a test file""" diff --git a/logilab/common/testlib.py b/logilab/common/testlib.py index 9fafdfa..ae6ebfc 100644 --- a/logilab/common/testlib.py +++ b/logilab/common/testlib.py @@ -44,6 +44,7 @@ __docformat__ = "restructuredtext en" # disable camel case warning # pylint: disable=C0103 +from contextlib import contextmanager import sys import os, os.path as osp import re @@ -78,7 +79,7 @@ from logilab.common.decorators import cached, classproperty from logilab.common import textutils -__all__ = ['unittest_main', 'find_tests'] +__all__ = ['unittest_main', 'find_tests', 'nocoverage', 'pause_trace'] DEFAULT_PREFIXES = ('test', 'regrtest', 'smoketest', 'unittest', 'func', 'validation') @@ -199,6 +200,40 @@ def start_interactive_mode(result): break +# coverage pausing tools ##################################################### + +@contextmanager +def replace_trace(trace=None): + """A context manager that temporary replaces the trace function""" + oldtrace = sys.gettrace() + sys.settrace(trace) + try: + yield + finally: + # specific hack to work around a bug in pycoverage, see + # https://bitbucket.org/ned/coveragepy/issue/123 + if (oldtrace is not None and not callable(oldtrace) and + hasattr(oldtrace, 'pytrace')): + oldtrace = oldtrace.pytrace + sys.settrace(oldtrace) + + +pause_trace = replace_trace + + +def nocoverage(func): + """Function decorator that pauses tracing functions""" + if hasattr(func, 'uncovered'): + return func + func.uncovered = True + + def not_covered(*args, **kwargs): + with pause_trace(): + return func(*args, **kwargs) + not_covered.uncovered = True + return not_covered + + # test utils ################################################################## diff --git a/test/unittest_pytest.py b/test/unittest_pytest.py index 7966111..48e36ce 100644 --- a/test/unittest_pytest.py +++ b/test/unittest_pytest.py @@ -81,21 +81,6 @@ class ModuleFunctionTC(TestCase): with replace_trace(tracefn): myfn() - def test_legacy_pause_resume_tracing(self): - def tracefn(frame, event, arg): - pass - - with replace_trace(tracefn): - pause_tracing() - self.assertIs(sys.gettrace(), None) - with replace_trace(tracefn): - pause_tracing() - self.assertIs(sys.gettrace(), None) - resume_tracing() - self.assertIs(sys.gettrace(), tracefn) - self.assertIs(sys.gettrace(), None) - resume_tracing() - self.assertIs(sys.gettrace(), tracefn) if __name__ == '__main__': unittest_main() |