Flask Testing - why does coverage exclude import statements and decorators? Flask Testing - why does coverage exclude import statements and decorators? flask flask

Flask Testing - why does coverage exclude import statements and decorators?


This is the third question in the coverage.py FAQ:

Q: Why do the bodies of functions (or classes) show as executed, but the def lines do not?

This happens because coverage is started after the functions are defined. The definition lines are executed without coverage measurement, then coverage is started, then the function is called. This means the body is measured, but the definition of the function itself is not.

To fix this, start coverage earlier. If you use the command line to run your program with coverage, then your entire program will be monitored. If you are using the API, you need to call coverage.start() before importing the modules that define your functions.

The simplest thing to do is run you tests under coverage:

$ coverage run -m unittest discover

Your custom test script isn't doing much beyond what the coverage command line would do, it will be simpler just to use the command line.


For excluding the imports statements, you can add the following lines to .coveragerc

[report]exclude_lines =    # Ignore imports    from    import

but when I tried to add '@' for decorators, the source code within the scope of decorators was excluded. The coverage rate was wrong.There may be some other ways to exclude decorators.