summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2009-10-19 06:55:03 -0400
committerNed Batchelder <ned@nedbatchelder.com>2009-10-19 06:55:03 -0400
commit832ee468e711e6d33fb49fb718e60a85c970a8ef (patch)
tree048ed03bbec8096b58f6123a5b81cba1ef684f5a /test
parent352324b4df27881776ceeb9584574081691a477e (diff)
downloadpython-coveragepy-git-832ee468e711e6d33fb49fb718e60a85c970a8ef.tar.gz
Start testing exceptions with arc measurements.
Diffstat (limited to 'test')
-rw-r--r--test/coveragetest.py17
-rw-r--r--test/test_arcs.py37
2 files changed, 45 insertions, 9 deletions
diff --git a/test/coveragetest.py b/test/coveragetest.py
index 635b05b2..b1f4644c 100644
--- a/test/coveragetest.py
+++ b/test/coveragetest.py
@@ -125,8 +125,7 @@ class CoverageTest(unittest.TestCase):
return sorted(arcs)
def check_coverage(self, text, lines=None, missing="", excludes=None,
- report="", arcs=None, arcs_missing=None,
- arcz=None, arcz_missing=None):
+ report="", arcz=None, arcz_missing="", arcz_unpredicted=""):
"""Check the coverage measurement of `text`.
The source `text` is run and measured. `lines` are the line numbers
@@ -134,6 +133,12 @@ class CoverageTest(unittest.TestCase):
are regexes to match against for excluding lines, and `report` is
the text of the measurement report.
+ For arc measurement, `arcz` is a string that can be decoded into arcs
+ in the code (see `arcz_to_arcs` for the encoding scheme),
+ `arcz_missing` are the arcs that are not executed, and
+ `arcs_unpredicted` are the arcs executed in the code, but not deducible
+ from the code.
+
"""
# We write the code into a file so that we can import it.
# Coverage wants to deal with things as modules with file names.
@@ -141,10 +146,11 @@ class CoverageTest(unittest.TestCase):
self.make_file(modname+".py", text)
+ arcs = arcs_missing = arcs_unpredicted = None
if arcz is not None:
arcs = self.arcz_to_arcs(arcz)
- if arcz_missing is not None:
- arcs_missing = self.arcz_to_arcs(arcz_missing)
+ arcs_missing = self.arcz_to_arcs(arcz_missing or "")
+ arcs_unpredicted = self.arcz_to_arcs(arcz_unpredicted or "")
# Start up Coverage.
cov = coverage.coverage(branch=(arcs_missing is not None))
@@ -194,6 +200,9 @@ class CoverageTest(unittest.TestCase):
if arcs_missing is not None:
self.assertEqual(analysis.arcs_missing(), arcs_missing)
+ if arcs_unpredicted is not None:
+ self.assertEqual(analysis.arcs_unpredicted(), arcs_unpredicted)
+
if report:
frep = StringIO()
cov.report(mod, file=frep)
diff --git a/test/test_arcs.py b/test/test_arcs.py
index 89b8921a..0a7850a8 100644
--- a/test/test_arcs.py
+++ b/test/test_arcs.py
@@ -14,13 +14,13 @@ class SimpleArcTest(CoverageTest):
a = 1
b = 2
""",
- arcs=[(-1,1), (1,2), (2,-1)], arcs_missing=[])
+ arcz=".1 12 2.")
self.check_coverage("""\
a = 1
b = 3
""",
- arcs=[(-1,1), (1,3), (3,-1)], arcs_missing=[])
+ arcz=".1 13 3.")
self.check_coverage("""\
a = 2
@@ -28,7 +28,7 @@ class SimpleArcTest(CoverageTest):
c = 5
""",
- arcs=[(-1,2), (2,3), (3,5),(5,-1)], arcs_missing=[])
+ arcz=".2 23 35 5.")
def test_function_def(self):
self.check_coverage("""\
@@ -37,7 +37,7 @@ class SimpleArcTest(CoverageTest):
foo()
""",
- arcs=[(-1,1), (-1,2),(1,4),(2,-1), (4,-1)], arcs_missing=[])
+ arcz=".1 .2 14 2. 4.")
def test_if(self):
self.check_coverage("""\
@@ -131,7 +131,34 @@ class LoopArcTest(CoverageTest):
assert a == 2 and i == 2 # 7
""",
arcz=".1 12 23 34 45 25 56 51 67 17 7.", arcz_missing="17 25")
-
+
+class ExceptionArcTest(CoverageTest):
+
+ def test_no_exception(self):
+ self.check_coverage("""\
+ a, b = 1, 1
+ try:
+ a = 3
+ except:
+ b = 5
+ assert a == 3 and b == 1
+ """,
+ arcz=".1 12 23 36 45 56 6.", arcz_missing="45 56")
+
+ def test_raise(self):
+ self.check_coverage("""\
+ a, b = 1, 1
+ try:
+ a = 3
+ raise Exception("Yikes!")
+ a = 5
+ except:
+ b = 7
+ assert a == 3 and b == 7
+ """,
+ arcz=".1 12 23 34 58 67 78 8.",
+ arcz_missing="58", arcz_unpredicted="46")
+
def xest_xx(self):
self.check_coverage("""\
""",