save() and html_report() prevent further metric gathering

Issue #448 resolved
Michael Crouch created an issue

When you call it seems to prevent anything else from being read in, for example, html reports. For example, the following program shows 2 lines covered (the bodies of f1 and f2):

import coverage

cov = coverage.Coverage()

def f1():
    print 2

def f2():
    print 3


However, if you uncomment the "" line, it only shows the body of f1() as covered -- it doesn't record the body of f2().

cov.html_report() has the same problem when it's run between f1() and f2().

I'm not sure if this behavior is intended, but the documentation of save() and html_report() don't mention it.

I encountered this behavior while trying to find a "quick and dirty" solution for finding coverage information for the server side of a client/server application. I added cov.start() to the server initialization and added cov.html_report() to an existing server call, with the plan on just making that call to write out the currently-collected coverage information.

Comments (5)

  1. Matthew Boehm

    Thanks for reporting this, I agree that this is confusing and needs to be fixed.

    Looking at the code, it appears that many of these functions (report, html_report, xml_report, save, combine, annotate) end up calling get_data, which resets the collector. While this side effect is documented in get_data, it's not documented in any of these other functions.

    As I see it, there are 3 options:

    1. Update the documentation for all these functions to say that they reset the collector
    2. Add a reset_collector=True parameter to these functions
    3. Update the functions so that they don't reset the collector

    My guess is that option 3 would break other code out there, but I'll let others weigh in on this.

  2. Log in to comment