Skip to content

Commit

Permalink
Improved test behaviour: now a threshold can be provided.
Browse files Browse the repository at this point in the history
  • Loading branch information
smarie committed Nov 21, 2018
1 parent 1f507dc commit 2728c25
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ script:
# now done in a dedicated script to capture exit code 1 and transform it to 0
- chmod a+x ./ci_tools/run_tests.sh
- sh ./ci_tools/run_tests.sh
- python ci_tools/generate-junit-badge.py 100 # generates the badge for the test results and fail build if less than x%

after_success:
# ***reporting***
# - junit2html junit.xml testrun.html output is really not nice
- ant -f ci_tools/generate-junit-html.xml # generates the html for the test results. Actually we dont use it anymore
- python ci_tools/generate-junit-badge.py # generates the badge for the test results
- codecov
- pylint pytest_steps # note that at the moment the report is simply lost, we dont transform the result into anything
# ***documentation***
Expand Down
48 changes: 41 additions & 7 deletions ci_tools/generate-junit-badge.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
import sys

import requests
import shutil
from os import makedirs, path
import xunitparser


def download_badge(junit_xml: str='reports/junit/junit.xml', dest_folder: str='reports/junit'):

makedirs(dest_folder, exist_ok=True)

# read the junit test file
def get_success_percentage(junit_xml='reports/junit/junit.xml' # type: str
):
# type: (...) -> int
"""
read the junit test file and extract the success percentage
:param junit_xml: the junit xml file path
:return: the success percentage (an int)
"""
ts, tr = xunitparser.parse(open(junit_xml))
runned = tr.testsRun
failed = len(tr.failures)

success_percentage = round((runned - failed) * 100 / runned)
return success_percentage


def download_badge(success_percentage, # type: int
dest_folder='reports/junit' # type: str
):
"""
Downloads the badge corresponding to the provided success percentage, from https://img.shields.io.
:param success_percentage:
:param dest_folder:
:return:
"""
makedirs(dest_folder, exist_ok=True)

if success_percentage < 50:
color = 'red'
elif success_percentage < 75:
Expand All @@ -35,5 +55,19 @@ def download_badge(junit_xml: str='reports/junit/junit.xml', dest_folder: str='r


if __name__ == "__main__":
# execute only if run as a script
download_badge()
# Execute only if run as a script.
# Check the arguments
assert len(sys.argv[1:]) == 1, "a single mandatory argument is required: <threshold>"
threshold = float(sys.argv[1])

# First retrieve the success percentage from the junit xml
success_percentage = get_success_percentage()

# Validate against the threshold
print("Success percentage is %s%%. Checking that it is >= %s" % (success_percentage, threshold))
if success_percentage < threshold:
raise Exception("Success percentage %s%% is strictly lower than required threshold %s%%"
"" % (success_percentage, threshold))

# Download the badge
download_badge(success_percentage)

0 comments on commit 2728c25

Please sign in to comment.