Skip to content

Commit

Permalink
Fix @coverage_with_hotshot output to match @coverage exactly
Browse files Browse the repository at this point in the history
The "%d lines were not executed." was missing.

Add tests for hotshot-based decorators, to be enabled only if hotshot is
available (under Python 3 it's not).
  • Loading branch information
mgedmin committed Dec 2, 2014
1 parent db7036d commit 11ab33a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 8 additions & 1 deletion profilehooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,11 @@ def __init__(self, fn):
def __call__(self, *args, **kw):
"""Profile a singe call to the function."""
self.ncalls += 1
return self.profiler.runcall(self.fn, args, kw)
old_trace = sys.gettrace()
try:
return self.profiler.runcall(self.fn, args, kw)
finally:
sys.settrace(old_trace)

def atexit(self):
"""Stop profiling and print profile information to sys.stderr.
Expand Down Expand Up @@ -518,6 +522,9 @@ def atexit(self):
fs.mark(lineno)
reader.close()
print(fs)
never_executed = fs.count_never_executed()
if never_executed:
print("%d lines were not executed." % never_executed)


class TraceFuncCoverage:
Expand Down
24 changes: 22 additions & 2 deletions test_profilehooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ def run_exitfuncs():
fn(*args, **kw)


def skipIf(condition, reason):
def decorator(fn):
if condition:
fn.__doc__ = 'Test skipped: %s' % reason
return fn
return decorator


class TestCase(unittest.TestCase):

maxDiff = None
Expand Down Expand Up @@ -72,9 +80,11 @@ def sample_fn(self, x, y, z):
else:
return "%s %s %s" % (x, y, z)

decorator = staticmethod(profilehooks.coverage)

def setUp(self):
super(TestCoverage, self).setUp()
self.sample_fn = profilehooks.coverage(self.sample_fn)
self.sample_fn = self.decorator(self.sample_fn)

def test_coverage(self):
self.sample_fn(1, 1, 1)
Expand All @@ -100,6 +110,15 @@ def sample_fn(self, x, y, z):
""".format(linenumber)))


if profilehooks.hotshot is not None:
class TestCoverageWithHotShot(TestCoverage):
decorator = staticmethod(profilehooks.coverage_with_hotshot)

def tearDown(self):
super(TestCoverageWithHotShot, self).tearDown()
os.unlink('sample_fn.cprof')


def doctest_coverage_when_source_is_not_available(self):
"""Test for coverage.
Expand Down Expand Up @@ -217,6 +236,7 @@ def doctest_profile_recursive_function():
"""


@skipIf(profilehooks.hotshot is None, 'hotshot is not available')
def doctest_profile_with_hotshot():
"""Test for profile
Expand Down Expand Up @@ -376,7 +396,7 @@ def additional_tests():
optionflags = (doctest.REPORT_ONLY_FIRST_FAILURE |
doctest.ELLIPSIS)
return unittest.TestSuite([
unittest.makeSuite(TestCoverage),
unittest.defaultTestLoader.loadTestsFromName(__name__),
doctest.DocTestSuite(setUp=setUp, tearDown=tearDown,
optionflags=optionflags),
])
Expand Down

0 comments on commit 11ab33a

Please sign in to comment.