Python Coverage suddenly dropping

I’m working on a GUI written in Python using PyQt5, and I’m preparing the coverage report for SonarQube.

The coverage was initially 43%. Then it suddenly dropped after I changed the main file, called app.py. All I did was add the following:

if __name__ == "__main__":
    main()

I added this because without it, it would display the GUI when importing app.py.
The main() simply instantiates the application, and also contains instructions to show the main window. With the above addition, the coverage drops to 14%. If I remove the above, the coverage goes right back to 43%. Any ideas why the coverage would suddenly drop significantly upon adding this tiny change, and how I could prevent it from dropping this much?

Versions:

  • Python 3.10.2
  • Coverage.py, version 6.3.3 with C extension
  • PyQt5 5.15.4

Hey there.

Does this change only happen in SonarQube, or also in the coverage report itself? SonarQube doesn’t produce the coverage report – it only reads what is passed to it.

If the coverage is dropping in the raw coverage report, your best source of support would be going back to the coverage tool itself.