pytest-cov plugin reports imports and function definitions not being covered by tests

Solution 1:

You haven't shown the whole program, but I will guess that you are importing your product code at the top of this file. That means all of the top-level statements in the product code (import, class, def, etc) will have already run by the time the pytest-cov plugin starts the coverage measurement.

There are a few things you can do:

  1. Run pytest from the command line instead of in-process in your product code.
  2. Change this code to start pytest in a subprocess so that everything will be re-imported by pytest.

What you should not do: exclude import lines from coverage measurement. The .coveragerc you showed will ignore any line with the string "from" or "import" in it, which isn't what you want ("import" is in "do_important_thing()" for example!)