Code coverage

andrea.sterbini (167800 points)
488 897 1728
asked Nov 20, 2021 in Programming in Python by andrea.sterbini (167,800 points)
edited Nov 20, 2021 by andrea.sterbini

To help you discovering if some part of your code has NOT been executed by tests (and should be hand-checked) I will add a code-coverage report to the correctness log.

To do this check locally, you can:

  • install the plugin pytest-cov (conda install pytest-cov)
  • run tests with coverage active ( pytest test_01.py --cov=program01 --cov-report=html )
  • read the report (firefox htmlcov/index.html)
  • the lines highlighted in red have not been executed by any test but could be in a future test. Check these lines carefully for errors.

3 Answers

gianluca5539 (9730 points)
2 4 42
answered Nov 20, 2021 by gianluca5539 (9,730 points)
Thank you very much!
gabrimat (5520 points)
2 2 23
answered Nov 20, 2021 by gabrimat (5,520 points)
When trying, the terminal answers back by telling me: no data to report. Anyone in the same situation?
rokshana03 (6830 points)
1 2 25
answered Nov 20, 2021 by rokshana03 (6,830 points)
so, I tried doing "pytest test_01" at first and it tells me that I pass 9 tests, so no timeout exceptions and the VM also says 9 tests passed. However when I do pytest with the coverage active, it says that I pass only 8 and the one that fails is because of "testlib.TimeoutError: Timeout! (1.116 > 1)". I'm not really sure what to think since the VM passes 9 tests but when I apply the coverage, it says 8 tests passed (in my PC)...is my code wrong maybe? I don't know what to think
andrea.sterbini (167800 points)
488 897 1728
commented Nov 20, 2021 by andrea.sterbini (167,800 points)
The test coverage possibly adds some time to the execution.

Perhaps it's better I leave it as is and you know how to check for it locally.
rokshana03 (6830 points)
1 2 25
commented Nov 20, 2021 by rokshana03 (6,830 points)
Okay, thankss!