From c6a47d49e3ac32de75859e3439cd0fe96f0c4460 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 23 May 2014 17:04:08 -0400 Subject: Add branch misses to stdout report --- coverage/results.py | 14 ++++++++++++++ coverage/summary.py | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/coverage/results.py b/coverage/results.py index ce9e0fa4..676ba716 100644 --- a/coverage/results.py +++ b/coverage/results.py @@ -83,6 +83,20 @@ class Analysis(object): ) return sorted(missing) + def arcs_missing_formatted(self): + """ The missing branch arcs, formatted. + + Returns a string like "1->2, 1->3, 16->20" + + """ + arcs = self.missing_branch_arcs() + pairs = [] + for line, exits in arcs.iteritems(): + for exit in exits: + pair = '%d->%d' % (line, exit) + pairs.append(pair) + return ', '.join(pairs) + def arcs_unpredicted(self): """Returns a sorted list of the executed arcs missing from the code.""" possible = self.arc_possibilities() diff --git a/coverage/summary.py b/coverage/summary.py index c99c5303..3588d31b 100644 --- a/coverage/summary.py +++ b/coverage/summary.py @@ -37,6 +37,8 @@ class SummaryReporter(Reporter): if self.config.show_missing: header += " Missing" fmt_coverage += " %s" + if self.branches: + fmt_coverage += ", Branches: %s" rule = "-" * len(header) + "\n" header += "\n" fmt_coverage += "\n" @@ -60,6 +62,8 @@ class SummaryReporter(Reporter): args += (nums.pc_covered_str,) if self.config.show_missing: args += (analysis.missing_formatted(),) + if self.branches: + args += (analysis.arcs_missing_formatted(),) outfile.write(fmt_coverage % args) total += nums except KeyboardInterrupt: # pragma: not covered -- cgit v1.2.1 From 8cfec5d9e75dd95a313af08b7f0dcd27c3585371 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 26 May 2014 13:26:02 -0400 Subject: Tests for --branch --show-missing summary; update AUTHORS.txt --- AUTHORS.txt | 1 + tests/test_summary.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/AUTHORS.txt b/AUTHORS.txt index d7e2d276..a30f377f 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -43,3 +43,4 @@ Sigve Tjora Mark van der Wal Zooko Wilcox-O'Hearn Christoph Zwerschke +Steve Leonard diff --git a/tests/test_summary.py b/tests/test_summary.py index 79e32169..61bdf37a 100644 --- a/tests/test_summary.py +++ b/tests/test_summary.py @@ -142,6 +142,55 @@ class SummaryTest(CoverageTest): self.assertEqual(self.last_line_squeezed(report), "mybranch 5 0 2 1 86%") + def test_report_show_missing(self): + self.make_file("mymissing.py", """\ + def missing(x, y): + if x: + print("x") + return x + if y: + print("y") + return x + return y + missing(0, 1) + """) + out = self.run_command("coverage run mymissing.py") + self.assertEqual(out, 'y\n') + report = self.report_from_command("coverage report --show-missing") + + # Name Stmts Miss Cover Missing + # ----------------------------------------- + # tests/tmp 9 3 67% 3-4, 8 + + self.assertEqual(self.line_count(report), 3) + self.assertIn("mymissing ", report) + self.assertEqual(self.last_line_squeezed(report), + "mymissing 9 3 67% 3-4, 8") + + def test_report_show_missing_branches(self): + self.make_file("mybranch.py", """\ + def branch(x, y): + if x: + print("x") + if y: + print("y") + return x + branch(1, 0) + """) + out = self.run_command("coverage run --branch mybranch.py") + self.assertEqual(out, 'x\n') + report = self.report_from_command("coverage report --show-missing") + + # pylint: disable=C0301 + # Name Stmts Miss Branch BrMiss Cover Missing + # ------------------------------------------------------- + # mybranch 7 1 4 2 73% 5, Branches: 2->4, 4->5 + + self.assertEqual(self.line_count(report), 3) + self.assertIn("mybranch ", report) + self.assertEqual(self.last_line_squeezed(report), + "mybranch 7 1 4 2 73% 5, Branches: 2->4, 4->5") + def test_dotpy_not_python(self): # We run a .py file, and when reporting, we can't parse it as Python. # We should get an error message in the report. -- cgit v1.2.1 From ad32797629dfe2fe18d7301203e7afb2fcaa15d7 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 26 May 2014 13:42:02 -0400 Subject: Fix formatting when no missing lines; improve tests --- coverage/results.py | 4 +++- coverage/summary.py | 10 +++++++--- tests/test_summary.py | 37 ++++++++++++++++++++++++++++++++----- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/coverage/results.py b/coverage/results.py index 676ba716..94785ca0 100644 --- a/coverage/results.py +++ b/coverage/results.py @@ -90,8 +90,10 @@ class Analysis(object): """ arcs = self.missing_branch_arcs() + line_exits = sorted(arcs.iteritems(), key=lambda (x, _): x) pairs = [] - for line, exits in arcs.iteritems(): + for line, exits in line_exits: + exits = sorted(exits) for exit in exits: pair = '%d->%d' % (line, exit) pairs.append(pair) diff --git a/coverage/summary.py b/coverage/summary.py index 3588d31b..1e55a7d4 100644 --- a/coverage/summary.py +++ b/coverage/summary.py @@ -38,7 +38,7 @@ class SummaryReporter(Reporter): header += " Missing" fmt_coverage += " %s" if self.branches: - fmt_coverage += ", Branches: %s" + fmt_coverage += "%sBranches: %s" rule = "-" * len(header) + "\n" header += "\n" fmt_coverage += "\n" @@ -61,9 +61,13 @@ class SummaryReporter(Reporter): args += (nums.n_branches, nums.n_missing_branches) args += (nums.pc_covered_str,) if self.config.show_missing: - args += (analysis.missing_formatted(),) + missing_fmtd = analysis.missing_formatted() + args += (missing_fmtd,) if self.branches: - args += (analysis.arcs_missing_formatted(),) + separator = "" + if missing_fmtd: + separator = ", " + args += (separator, analysis.arcs_missing_formatted(),) outfile.write(fmt_coverage % args) total += nums except KeyboardInterrupt: # pragma: not covered diff --git a/tests/test_summary.py b/tests/test_summary.py index 61bdf37a..7fd11a0e 100644 --- a/tests/test_summary.py +++ b/tests/test_summary.py @@ -160,7 +160,7 @@ class SummaryTest(CoverageTest): # Name Stmts Miss Cover Missing # ----------------------------------------- - # tests/tmp 9 3 67% 3-4, 8 + # mymissing 9 3 67% 3-4, 8 self.assertEqual(self.line_count(report), 3) self.assertIn("mymissing ", report) @@ -175,21 +175,48 @@ class SummaryTest(CoverageTest): if y: print("y") return x - branch(1, 0) + branch(1, 1) """) out = self.run_command("coverage run --branch mybranch.py") - self.assertEqual(out, 'x\n') + self.assertEqual(out, 'x\ny\n') + report = self.report_from_command("coverage report --show-missing") + + # Name Stmts Miss Branch BrMiss Cover Missing + # ------------------------------------------------------- + # tests/tmp 7 0 4 2 82% Branches: 2->4, 4->6 + + self.assertEqual(self.line_count(report), 3) + self.assertIn("mybranch ", report) + self.assertEqual(self.last_line_squeezed(report), + "mybranch 7 0 4 2 82% Branches: 2->4, 4->6") + + def test_report_show_missing_branches_and_lines(self): + self.make_file("mybranch.py", """\ + def branch(x, y, z): + if x: + print("x") + if y: + print("y") + if z: + if x and y: + print("z") + return x + branch(1, 1, 0) + """) + out = self.run_command("coverage run --branch mybranch.py") + self.assertEqual(out, 'x\ny\n') report = self.report_from_command("coverage report --show-missing") # pylint: disable=C0301 # Name Stmts Miss Branch BrMiss Cover Missing # ------------------------------------------------------- - # mybranch 7 1 4 2 73% 5, Branches: 2->4, 4->5 + # tests/tmp 10 2 8 5 61% 7-8, Branches: 2->4, 4->6, 6->7, 7->8, 7->9 self.assertEqual(self.line_count(report), 3) self.assertIn("mybranch ", report) self.assertEqual(self.last_line_squeezed(report), - "mybranch 7 1 4 2 73% 5, Branches: 2->4, 4->5") + "mybranch 10 2 8 5 61% " + "7-8, Branches: 2->4, 4->6, 6->7, 7->8, 7->9") def test_dotpy_not_python(self): # We run a .py file, and when reporting, we can't parse it as Python. -- cgit v1.2.1 From 232b546e7325be1626f940e5358fe468f3f06872 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 26 May 2014 13:44:54 -0400 Subject: minor comment fix --- tests/test_summary.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_summary.py b/tests/test_summary.py index 7fd11a0e..88be7761 100644 --- a/tests/test_summary.py +++ b/tests/test_summary.py @@ -183,7 +183,7 @@ class SummaryTest(CoverageTest): # Name Stmts Miss Branch BrMiss Cover Missing # ------------------------------------------------------- - # tests/tmp 7 0 4 2 82% Branches: 2->4, 4->6 + # mybranch 7 0 4 2 82% Branches: 2->4, 4->6 self.assertEqual(self.line_count(report), 3) self.assertIn("mybranch ", report) @@ -210,7 +210,7 @@ class SummaryTest(CoverageTest): # pylint: disable=C0301 # Name Stmts Miss Branch BrMiss Cover Missing # ------------------------------------------------------- - # tests/tmp 10 2 8 5 61% 7-8, Branches: 2->4, 4->6, 6->7, 7->8, 7->9 + # mybranch 10 2 8 5 61% 7-8, Branches: 2->4, 4->6, 6->7, 7->8, 7->9 self.assertEqual(self.line_count(report), 3) self.assertIn("mybranch ", report) -- cgit v1.2.1