4

I want to capture the coverage from inside the code. I tried below one but getting error. Referred to below link for coverage API. https://coverage.readthedocs.io/en/v4.5.x/api.html#api

import os
import pandas as pd
import sys
import requests
import xml.etree.ElementTree as ET
from xml.dom import minidom
import coverage

cov = coverage.Coverage()
cov.start()

#actual code

cov.stop()
cov.save()

cov.html_report(directory='covhtml')

getting below errors

CoverageException                         Traceback (most recent call last)
<ipython-input-15-2047badbbd57> in <module>()
     48 cov.save()
     49 
---> 50 cov.html_report(directory='covhtml')

C:\Users\\Anaconda2\lib\site-packages\coverage\control.pyc in html_report(self, morfs, directory, ignore_errors, omit, include, extra_css, title, skip_covered)
   1093             )
   1094         reporter = HtmlReporter(self, self.config)
-> 1095         return reporter.report(morfs)
   1096 
   1097     def xml_report(

C:\Users\\Anaconda2\lib\site-packages\coverage\html.pyc in report(self, morfs)
    137 
    138         # Process all the files.
--> 139         self.report_files(self.html_file, morfs, self.config.html_dir)
    140 
    141         if not self.all_files_nums:

C:\Users\\Anaconda2\lib\site-packages\coverage\report.pyc in report_files(self, report_fn, morfs, directory)
     81 
     82         if not file_reporters:
---> 83             raise CoverageException("No data to report.")
     84 
     85         self.directory = directory

CoverageException: No data to report.
3
  • Can you say why you want to use the coverage API? Why not just run coverage on the command line? Commented Oct 28, 2018 at 16:27
  • I am having a shell script which is calling different python scripts. So, I feel for each python script I can insert the coverage code and redirect the output to a separate directory for each python script. This looks cleaner than command line option. Commented Oct 29, 2018 at 7:06
  • I don't know why that seems cleaner to you. Coverage measurement should be an optional process that you can apply to existing code. You can change one line in a shell script to make it happen. Commented Oct 29, 2018 at 11:16

1 Answer 1

6

If you wrap whatever you have for #actual code in a function, then it will work. Here's a (minimalish) example:

import coverage

def test_it(x):
    return x + 1

cov = coverage.Coverage()
cov.start()

test_it(123)

cov.stop()
cov.save()

cov.html_report(directory='covhtml')

However, if you would replace test_it(123) by just doing some inline statement (like x = 123; x += 1; print(x)), then the coverage module will fail.

It's well hidden, but the docs do explain this behavior:

start()

Start measuring code coverage.

Coverage measurement only occurs in functions called after start() is invoked. Statements in the same scope as start() won’t be measured.

Once you invoke start(), you must also call stop() eventually, or your process might not shut down cleanly.

Emphasis my own, here's the link: https://coverage.readthedocs.io/en/v4.5.x/api_coverage.html#coverage.Coverage.start

Sign up to request clarification or add additional context in comments.

7 Comments

Matt - I tried your code but its failing too with same error.
@RishiBansal You're sure? You copy/pasted it exactly as-is into a new file, and ran that file through your python interpreter? It works for me (just double check with a quick copy/paste), I'm on Python 3.6, MacOS.
Yeah, I tried again and its failing. I am using Python 2.7 Anaconda/Jupiter, Windows 10.
@RishiBansal What version of coverage do you have? I just tried with Python 2.7 from a conda env, it still worked. The version I have is 4.5.1. To get the version, do import coverage; coverage.__version__.
@RishiBansal To be safe, I think it also works best if your code to be covered is in a different module as well. That also might fix the issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.