0

I am running below code to parse xml file. The score is coming close to 21% only. Not able to understand why? Can you please suggest what needs to be done here to improve the score.

import os
import pandas as pd
from xml.dom import minidom
import coverage



def parseXml(DF, filePath):

    xmldoc = minidom.parse(filePath)
    PODs= xmldoc.getElementsByTagName("POD")
    for pod in PODs:
        if pod.hasAttribute("name") and pod.hasAttribute("DC") and pod.hasAttribute("deferLPUpgrade"):
            DF = DF.append({'PODNAME': pod.getAttribute("name"), 'DC': pod.getAttribute("DC"), 'Upgrade': pod.getAttribute("deferLPUpgrade")}, ignore_index=True)
    return(DF)

if __name__ == "__main__":
    cov = coverage.Coverage()
    cov.start()
    FILENAME = "schedule.xml"
    COL_NAMES = ['PODNAME', 'DC', 'Upgrade']
    DF = pd.DataFrame(columns = COL_NAMES)
    DF = parseXml(DF, FILENAME)
    print DF
    head, tail = os.path.splitext(FILENAME)
    OUT_FILE = os.path.join(head + '.csv')
    DF.to_csv(OUT_FILE, sep=',', index=False)

    cov.stop()
    cov.save()

    cov.html_report(directory='covhtml')

Below is the temp_py.html

screenshot of temp_py.html

3
  • Suggest reading (and this is arguably a duplicate of) stackoverflow.com/questions/52989127/… Commented Oct 25, 2018 at 17:46
  • the above link is about coverage code and here I want to know more about score of coverage which is not covered in above link. Commented Oct 26, 2018 at 5:42
  • Going to have to disagree... if you read the top answer: Coverage measurement only occurs in functions called after start() is invoked. Statements in the same scope as start() won’t be measured. Assuming you're running coverage by invoking your main stuff, it's only going to measure what it shows in green, hence the 21% Commented Oct 26, 2018 at 14:10

1 Answer 1

1

You don't need to use the coverage API. Just run your program with the coverage command-line. Then all of your code will be covered.

You have a low score here because coverage can't measure code that runs before coverage starts. Because you are starting coverage from within your program, all of your program that runs before you call coverage.start() won't be covered. Also, none of the code in the frame that calls start() will be covered, only code in functions called from there. This explains all the red in your screen shot.

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

1 Comment

Many thanks.Now I am able to make full sense out of it. When I tried coverage.py from command line. I am getting 100% code covered.I am going to paste one more question on this, if you can answer that will be great.

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.