I have a folder containing XML files which somewhat look like this:
<testsuite file="test_apis.py" name="activities.tests time="8.679">
<testcase classname="testcase_1" file="test_apis.py" line="171" name="test_results" time="1.965">
I need to parse the file and get the value of time attribute under test case(second line of the code).
Here are the things I need to do with the files:
- I want to loop over a list where I have like about 100 files and
calculate time for each specified classname(there are about 6 unique classnames in all the files) and access "time" attribue of the tag and then sum up all the instances where the same classname occurs. Each classname occurs in one file only once but there are multiple instances of the same classname in one file. - Distribute the classname in groups so that the time consumed by all testcases will be the same.
- Create a csv file with the testcase name and the total time consumed.
I have created a fun to iterate over the files and store them into a list:
import glob
import pandas as pd
# Empty list to store path of xml files
path_list = []
# Function to iterate folder and store path of xml files.
# Can be modified to take the path as an argument via command line if required
time_sum = []
testcase = []
def calc_time(path):
for path in glob.iglob(f'{path}/*.xml'):
path_list.append(path)
try:
for file in path_list:
xml_df = pd.read_xml(file, xpath=".//testcase")
# Get the classname values from the XML file
testcase_v = xml_df.at[0, 'classname']
testcase.append(testcase_v)
# Get the aggregate time value of all instances of the classname
time_sum_test = xml_df['time'].sum()
time_sum.append(time_sum_test)
case_time = dict(zip(testcase, time_sum))
except Exception as ex:
msg_template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = msg_template.format(type(ex).__name__, ex.args)
print(message)
calc_time('assignment-1/data')
Please help me out..