Code coverage

andrea.sterbini (169260 points)
490 900 1734
asked Nov 20, 2021 in Programming in Python by andrea.sterbini (169,260 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 (9820 points)
2 5 44
answered Nov 20, 2021 by gianluca5539 (9,820 points)
Thank you very much!
gabrimat (5570 points)
2 2 24
answered Nov 20, 2021 by gabrimat (5,570 points)
When trying, the terminal answers back by telling me: no data to report. Anyone in the same situation?
rokshana03 (6890 points)
1 2 25
answered Nov 20, 2021 by rokshana03 (6,890 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 (169260 points)
490 900 1734
commented Nov 20, 2021 by andrea.sterbini (169,260 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 (6890 points)
1 2 25
commented Nov 20, 2021 by rokshana03 (6,890 points)
Okay, thankss!