Skip to content

Commit ad513ef

Browse files
author
Alex Palesandro
committed
Fixing converter
1 parent 07ae892 commit ad513ef

File tree

6 files changed

+102
-55
lines changed

6 files changed

+102
-55
lines changed

tests/converter/converter.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import re
2+
from junit_xml import TestSuite, TestCase
3+
import itertools
4+
5+
6+
7+
def file_log_parser(file:str) -> dict:
8+
with open(file) as file_log:
9+
config = file_log.read()
10+
test_logs_results = {}
11+
index = ""
12+
for line in config.split("\n"):
13+
if "++++Starting test" in line:
14+
index = line.split(" ")[-1][:-4]
15+
test_logs_results[index] = ""
16+
elif "++++TEST" in line:
17+
index = ""
18+
elif index:
19+
test_logs_results[index] += line + "\n"
20+
return test_logs_results
21+
22+
def create_junit_test_file(tests_file:str):
23+
tests = []
24+
for file in tests_file.split("\n"):
25+
file_log = "test_log_" + "_".join(file.split("_")[2:])
26+
test = file_log_parser(file_log)
27+
with open(file) as file_buffer: # Use file to refer to the file object
28+
config = file_buffer.read()
29+
current_section = {}
30+
lines = config.split()
31+
date = lines[1]
32+
is_relaunch = lines[2]
33+
debug=lines[3]
34+
for _,test_name,status,_,_,duration in zip(*[iter(lines[6:])]*6):
35+
if duration == "false":
36+
continue
37+
else:
38+
duration=duration[:-1]
39+
if test_name in test:
40+
tc = TestCase(name=test_name, elapsed_sec=int(duration),status=status.split("+")[0], stdout=test[test_name])
41+
if status != "Passed++++":
42+
tc.add_failure_info(output=test[test_name])
43+
else:
44+
tc = TestCase(name=test_name, elapsed_sec=int(duration), status=status.split("+")[0])
45+
tests.append(tc)
46+
return tests
47+
48+
def write_junit_file(output_file:str, tests:[], test_name:str) -> None:
49+
# We create a TestSUite and we write them in a XML File
50+
t = TestSuite(name=test_name,test_cases=tests)
51+
with open('output.xml', 'w') as f:
52+
TestSuite.to_file(f, [t], prettyprint=False)
53+

tests/converter/requirements.txt

100644100755
File mode changed.

tests/converter/tests.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from converter import create_junit_test_file, write_junit_file
2+
import assertpy
3+
4+
# content of test_sample.py
5+
6+
def test_count_single_file_1():
7+
t = create_junit_test_file("test_results_1.txt")
8+
assertpy.assert_that(len(t)).is_equal_to(10)
9+
10+
def test_count_single_file_2():
11+
t = create_junit_test_file("test_results_1.txt\ntest_results_2.txt")
12+
assertpy.assert_that(len(t)).is_equal_to(29)
13+
14+
def test_assert_status_1():
15+
t = create_junit_test_file("test_results_1.txt")
16+
for test in t:
17+
assertpy.assert_that(test.status).is_equal_to("Failed")
18+
19+
def test_assert_status_2():
20+
t = create_junit_test_file("test_results_2.txt")
21+
for test in t:
22+
assertpy.assert_that(test.status).is_equal_to("Passed")
23+
24+
def test_failure_1():
25+
t = create_junit_test_file("test_results_1.txt")
26+
assertpy.assert_that(len(t)).is_equal_to(10)
27+
for test in t:
28+
assertpy.assert_that(test.is_failure()).is_not_none()
29+
30+
def test_not_failure_1():
31+
t = create_junit_test_file("test_results_2.txt")
32+
assertpy.assert_that(len(t)).is_equal_to(19)
33+
for test in t:
34+
assertpy.assert_that(test.is_failure()).is_none()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
assertpy==0.15
2+
pytest==5.3.1

tests/converter/to_junit.py

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,13 @@
1-
import re
2-
from junit_xml import TestSuite, TestCase
3-
import itertools
41
import os
52
import argparse
6-
import os
3+
from converter import create_junit_test_file, write_junit_file
74

85
parser = argparse.ArgumentParser(description='Test File')
96
parser.add_argument('test_suite_name', type=str, help='Class of tests to parse')
107
args = parser.parse_args()
8+
OUTPUT_FILE = 'output.xml'
119
TESTS_FILE = os.environ['TEST_RESULTS']
1210

13-
tests = []
14-
15-
def file_log_parser(file:str) -> dict:
16-
with open(file) as file_log:
17-
config = file_log.read()
18-
test_logs_results = {}
19-
index = ""
20-
for line in config.split("\n"):
21-
if "++++Starting test" in line:
22-
index = line.split(" ")[-1][:-4]
23-
test_logs_results[index] = ""
24-
elif "++++TEST" in line:
25-
index = ""
26-
elif index:
27-
test_logs_results[index] += line + "\n"
28-
return test_logs_results
29-
30-
# For each test we generate a testCase parsing the output file
31-
for file in TESTS_FILE.split("\n"):
32-
file_log = "test_log_" + "_".join(file.split("_")[2:])
33-
test = file_log_parser(file_log)
34-
with open(file) as file_buffer: # Use file to refer to the file object
35-
config = file_buffer.read()
36-
current_section = {}
37-
lines = config.split()
38-
date = lines[1]
39-
is_relaunch = lines[2]
40-
debug=lines[3]
41-
for _,test_name,status,_,_,duration in zip(*[iter(lines[6:])]*6):
42-
if duration == "false":
43-
continue
44-
else:
45-
duration=duration[:-1]
46-
if test_name in test:
47-
tc = TestCase(name=test_name, elapsed_sec=int(duration),status=status, stdout=test[test_name])
48-
else:
49-
tc = TestCase(name=test_name, elapsed_sec=int(duration),status=status)
50-
tests.append(tc)
51-
52-
# We create a TestSUite and we write them in a XML File
53-
t = TestSuite(name=args.test_suite_name,test_cases=tests)
54-
with open('output.xml', 'w') as f:
55-
TestSuite.to_file(f, [t], prettyprint=False)
11+
if __name__ == "__main__":
12+
tests = create_junit_test_file(TESTS_FILE)
13+
write_junit_file(OUTPUT_FILE,tests,args.test_suite_name)

tests/helpers_tests.bash

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ function cleanup {
1717
cat $test_results
1818

1919
echo ""
20-
echo "FAILED TESTS:"
20+
echo "Failed TESTS:"
2121
echo ""
22-
cat $test_results | grep FAILED -A 1
22+
cat $test_results | grep Failed -A 1
2323

2424
if $failed ; then
2525
exit 1
@@ -41,15 +41,15 @@ function log_test {
4141
fi
4242
test_total=$(($test_total+1))
4343
if [ $status -ne 0 ]; then
44-
echo "++++TEST $1 FAILED++++"
45-
echo "++++TEST $1 FAILED++++" >> $test_results
46-
echo "++++TEST $1 FAILED++++" >> $test_tmp
44+
echo "++++TEST $1 Failed++++"
45+
echo "++++TEST $1 Failed++++" >> $test_results
46+
echo "++++TEST $1 Failed++++" >> $test_tmp
4747
failed=true
4848
else
4949
test_passed=$(($test_passed+1))
50-
echo "++++TEST $1 PASSED++++"
51-
echo "++++TEST $1 PASSED++++" >> $test_results
52-
echo "++++TEST $1 PASSED++++" >> $test_tmp
50+
echo "++++TEST $1 Passed++++"
51+
echo "++++TEST $1 Passed++++" >> $test_results
52+
echo "++++TEST $1 Passed++++" >> $test_tmp
5353
fi
5454
cat $test_tmp >> $test_log
5555
return $status

0 commit comments

Comments
 (0)