diff options
Diffstat (limited to 'coverage')
-rw-r--r-- | coverage/results.py | 33 | ||||
-rw-r--r-- | coverage/summary.py | 38 |
2 files changed, 54 insertions, 17 deletions
diff --git a/coverage/results.py b/coverage/results.py index 2374d3af..138782e6 100644 --- a/coverage/results.py +++ b/coverage/results.py @@ -35,11 +35,20 @@ class Analysis(object): exec1 = self.parser.first_lines(executed) self.missing = sorted(set(self.statements) - set(exec1)) + if self.coverage.data.has_arcs(): + n_branches = self.total_branches() + mba = self.missing_branch_arcs() + n_missing_branches = sum([len(v) for v in mba.values()]) + else: + n_branches = n_missing_branches = 0 + self.numbers = Numbers( n_files=1, n_statements=len(self.statements), n_excluded=len(self.excluded), n_missing=len(self.missing), + n_branches=n_branches, + n_missing_branches=n_missing_branches, ) def missing_formatted(self): @@ -95,6 +104,15 @@ class Analysis(object): return [l1 for l1,count in exit_counts.items() if count > 1] + def total_branches(self): + exit_counts = {} + for l1,l2 in self.arc_possibilities(): + if l1 not in exit_counts: + exit_counts[l1] = 0 + exit_counts[l1] += 1 + + return sum([count for count in exit_counts.values() if count > 1]) + def missing_branch_arcs(self): """Return arcs that weren't executed from branch lines. @@ -119,21 +137,30 @@ class Numbers(object): up statistics across files. """ - def __init__(self, n_files=0, n_statements=0, n_excluded=0, n_missing=0): + def __init__(self, n_files=0, n_statements=0, n_excluded=0, n_missing=0, + n_branches=0, n_missing_branches=0 + ): self.n_files = n_files self.n_statements = n_statements self.n_excluded = n_excluded self.n_missing = n_missing + self.n_branches = n_branches + self.n_missing_branches = n_missing_branches def _get_n_executed(self): """Returns the number of executed statements.""" return self.n_statements - self.n_missing n_executed = property(_get_n_executed) + def _get_n_executed_branches(self): + """Returns the number of executed branches.""" + return self.n_branches - self.n_missing_branches + n_executed_branches = property(_get_n_executed_branches) + def _get_pc_covered(self): """Returns a single percentage value for coverage.""" if self.n_statements > 0: - pc_cov = 100.0 * self.n_executed / self.n_statements + pc_cov = 100.0 * (self.n_executed + self.n_executed_branches) / (self.n_statements + self.n_branches) else: pc_cov = 100.0 return pc_cov @@ -145,6 +172,8 @@ class Numbers(object): nums.n_statements = self.n_statements + other.n_statements nums.n_excluded = self.n_excluded + other.n_excluded nums.n_missing = self.n_missing + other.n_missing + nums.n_branches = self.n_branches + other.n_branches + nums.n_missing_branches = self.n_missing_branches + other.n_missing_branches return nums def __radd__(self, other): diff --git a/coverage/summary.py b/coverage/summary.py index 00e4af87..e0e9eba7 100644 --- a/coverage/summary.py +++ b/coverage/summary.py @@ -12,6 +12,7 @@ class SummaryReporter(Reporter): def __init__(self, coverage, show_missing=True, ignore_errors=False): super(SummaryReporter, self).__init__(coverage, ignore_errors) self.show_missing = show_missing + self.branches = coverage.data.has_arcs() def report(self, morfs, omit_prefixes=None, outfile=None): """Writes a report summarizing coverage statistics per module.""" @@ -22,12 +23,19 @@ class SummaryReporter(Reporter): max_name = max([len(cu.name) for cu in self.code_units] + [5]) fmt_name = "%%- %ds " % max_name fmt_err = "%s %s: %s\n" - header = fmt_name % "Name" + " Stmts Exec Cover\n" - fmt_coverage = fmt_name + "% 6d % 6d % 5d%%\n" + header = (fmt_name % "Name") + " Stmts Exec" + fmt_coverage = fmt_name + "%6d %6d" + if self.branches: + header += " Branch BrExec" + fmt_coverage += " %6d %6d" + header += " Cover" + fmt_coverage += " %5d%%" if self.show_missing: - header = header.replace("\n", " Missing\n") - fmt_coverage = fmt_coverage.replace("\n", " %s\n") - rule = "-" * (len(header)-1) + "\n" + header += " Missing" + fmt_coverage += " %s" + rule = "-" * len(header) + "\n" + header += "\n" + fmt_coverage += "\n" if not outfile: outfile = sys.stdout @@ -42,12 +50,12 @@ class SummaryReporter(Reporter): try: analysis = self.coverage._analyze(cu) nums = analysis.numbers - args = ( - cu.name, nums.n_statements, nums.n_executed, - nums.pc_covered - ) + args = (cu.name, nums.n_statements, nums.n_executed) + if self.branches: + args += (nums.n_branches, nums.n_executed_branches) + args += (nums.pc_covered,) if self.show_missing: - args = args + (analysis.missing_formatted(),) + args += (analysis.missing_formatted(),) outfile.write(fmt_coverage % args) total += nums except KeyboardInterrupt: #pragma: no cover @@ -59,10 +67,10 @@ class SummaryReporter(Reporter): if total.n_files > 1: outfile.write(rule) - args = ( - "TOTAL", total.n_statements, total.n_executed, - total.pc_covered - ) + args = ("TOTAL", total.n_statements, total.n_executed) + if self.branches: + args += (total.n_branches, total.n_executed_branches) + args += (total.pc_covered,) if self.show_missing: - args = args + ("",) + args += ("",) outfile.write(fmt_coverage % args) |