3

We are working for getting code coverage by our test cases for the product. But the thing is that coverage tool does not show the files which are not at all hit.

We are using coverage command to run our processes like this:

coverage run -a --rcfile=/home/coveragerc -L

So we get a code coverage of 57%. Which has all the files which are hit. If a file is not hit then it is not included in coverage calculation. So it files which are not hit, are calculated, then actual coverage will be around 45%.

Please help how can I achieve it.

prateek goyal
  • 31
  • 1
  • 3
  • why do u want that file in the test case? If not required then remove. is that file have any test case which is not executing? – Manoj Jadhav Dec 05 '17 at 05:23
  • @ManojJadhav, I am not talking about the test files. I am talking about product code files. If they are not hit by our testcases, they are not shown in coverage report. It means those files have 0% coverage. It is like, I have given 2-2 apples to 2 persons, so I gave 4 apples. How many I have with me now? It did not take the original count of apples with me in consideration. So i can not tell actual count of remaining apples with me. – prateek goyal Dec 05 '17 at 05:39
  • check this. ```https://stackoverflow.com/questions/36517137/how-to-properly-use-coverage-py-in-python``` – Manoj Jadhav Dec 05 '17 at 05:54
  • Does this answer your question? [Why does Coverage.py ignore files with no coverage?](https://stackoverflow.com/questions/43077589/why-does-coverage-py-ignore-files-with-no-coverage) – Newskooler Mar 26 '20 at 23:39

2 Answers2

2

You want the --source option. It specifies the root of the source tree, so coverage.py can find all the source files, and include them even if they were never run: http://coverage.readthedocs.io/en/coverage-4.4.2/source.html

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • Thanks Ned. I tried this option. But I am facing one issue. in --source option we can give the directory or package name. We have a particular package "abcd". for which the path to package is "python2.6/site-packages/abcd-0.1-py2.6.egg/abcd/", but this is exactly not a directory, because "egg" file is a compressed one and this path is not taken as a directory by coverage. And when I use package name "abcd" with --source option, it is not giving me the files which are not hit. Does using "package name" means that the files, which import this package, are covered by coverage? – prateek goyal Dec 06 '17 at 07:02
  • If you use a package name, then something in the package has to be imported for coverage.py to be able to find the directory. You should get a warning, "Module abcd was never imported" or something like that. – Ned Batchelder Dec 06 '17 at 11:19
0

Worst case, this works if not neat:

coverage.start()

for module_name in all_my_modules:

  __import__(module_name)

run tests...

coverage.stop()

It does then report all the non-tested python files.

Squirrel
  • 1,189
  • 12
  • 15