Skip to content

Commit ea020c9

Browse files
author
Sylvain MARIE
committed
Using latest badge generator to get the test counts
1 parent d8d193f commit ea020c9

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

ci_tools/generate-junit-badge.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,70 @@
11
import sys
2+
try:
3+
# python 3
4+
from urllib.parse import quote_plus
5+
except ImportError:
6+
# python 2
7+
from urllib import quote_plus
28

39
import requests
410
import shutil
511
from os import makedirs, path
612
import xunitparser
713

814

9-
def get_success_percentage(junit_xml='reports/junit/junit.xml' # type: str
10-
):
11-
# type: (...) -> int
15+
class TestStats(object):
16+
def __init__(self, success_percentage, success, runned, skipped):
17+
self.success_percentage = success_percentage
18+
self.success = success
19+
self.runned = runned
20+
self.skipped = skipped
21+
22+
23+
def get_test_stats(junit_xml='reports/junit/junit.xml' # type: str
24+
):
25+
# type: (...) -> TestStats
1226
"""
1327
read the junit test file and extract the success percentage
1428
:param junit_xml: the junit xml file path
1529
:return: the success percentage (an int)
1630
"""
1731
ts, tr = xunitparser.parse(open(junit_xml))
18-
runned = tr.testsRun
32+
skipped = len(tr.skipped)
33+
runned = tr.testsRun - skipped
1934
failed = len(tr.failures)
35+
success = runned - failed
2036

21-
success_percentage = round((runned - failed) * 100 / runned)
22-
return success_percentage
37+
success_percentage = round(success * 100 / runned)
2338

39+
return TestStats(success_percentage, success, runned, skipped)
2440

25-
def download_badge(success_percentage, # type: int
41+
42+
def download_badge(test_stats, # type: TestStats
2643
dest_folder='reports/junit' # type: str
2744
):
2845
"""
2946
Downloads the badge corresponding to the provided success percentage, from https://img.shields.io.
3047
31-
:param success_percentage:
48+
:param test_stats:
3249
:param dest_folder:
3350
:return:
3451
"""
3552
if not path.exists(dest_folder):
3653
makedirs(dest_folder) # , exist_ok=True) not python 2 compliant
3754

38-
if success_percentage < 50:
55+
if test_stats.success_percentage < 50:
3956
color = 'red'
40-
elif success_percentage < 75:
57+
elif test_stats.success_percentage < 75:
4158
color = 'orange'
42-
elif success_percentage < 90:
59+
elif test_stats.success_percentage < 90:
4360
color = 'green'
4461
else:
4562
color = 'brightgreen'
46-
url = 'https://img.shields.io/badge/tests-' + str(success_percentage) + '%25-' + color + '.svg'
63+
64+
left_txt = "tests"
65+
# right_txt = "%s%%" % test_stats.success_percentage
66+
right_txt = "%s/%s" % (test_stats.success, test_stats.runned)
67+
url = 'https://img.shields.io/badge/%s-%s-%s.svg' % (left_txt, quote_plus(right_txt), color)
4768

4869
dest_file = path.join(dest_folder, 'junit-badge.svg')
4970

@@ -62,13 +83,13 @@ def download_badge(success_percentage, # type: int
6283
threshold = float(sys.argv[1])
6384

6485
# First retrieve the success percentage from the junit xml
65-
success_percentage = get_success_percentage()
86+
test_stats = get_test_stats()
6687

6788
# Validate against the threshold
68-
print("Success percentage is %s%%. Checking that it is >= %s" % (success_percentage, threshold))
69-
if success_percentage < threshold:
89+
print("Success percentage is %s%%. Checking that it is >= %s" % (test_stats.success_percentage, threshold))
90+
if test_stats.success_percentage < threshold:
7091
raise Exception("Success percentage %s%% is strictly lower than required threshold %s%%"
71-
"" % (success_percentage, threshold))
92+
"" % (test_stats.success_percentage, threshold))
7293

7394
# Download the badge
74-
download_badge(success_percentage)
95+
download_badge(test_stats)

0 commit comments

Comments
 (0)