Skip to content

Commit 7480982

Browse files
authored
Merge pull request #9 from common-workflow-language/junit-xml-test-durations
Add test duration to jUnit XML output
2 parents 82f80db + 0caf9af commit 7480982

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

cwltest/__init__.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import pipes
1414
import logging
1515
import schema_salad.ref_resolver
16+
import time
1617
from concurrent.futures import ThreadPoolExecutor
1718
from typing import Any, Dict, List, Text
1819

@@ -40,16 +41,20 @@ class TestResult(object):
4041

4142
"""Encapsulate relevant test result data."""
4243

43-
def __init__(self, return_code, standard_output, error_output):
44-
# type: (int, str, str) -> None
44+
def __init__(self, return_code, standard_output, error_output, duration):
45+
# type: (int, str, str, float) -> None
4546
self.return_code = return_code
4647
self.standard_output = standard_output
4748
self.error_output = error_output
49+
self.duration = duration
4850

4951
def create_test_case(self, test):
5052
# type: (Dict[Text, Any]) -> junit_xml.TestCase
5153
doc = test.get(u'doc', 'N/A').strip()
52-
return junit_xml.TestCase(doc, stdout=self.standard_output, stderr=self.error_output)
54+
return junit_xml.TestCase(
55+
doc, elapsed_sec=self.duration,
56+
stdout=self.standard_output, stderr=self.error_output
57+
)
5358

5459

5560
def compare_file(expected, actual):
@@ -147,6 +152,7 @@ def compare(expected, actual): # type: (Any, Any) -> None
147152
def run_test(args, i, tests): # type: (argparse.Namespace, int, List[Dict[str, str]]) -> TestResult
148153
out = {} # type: Dict[str,Any]
149154
outdir = outstr = outerr = test_command = None
155+
duration = 0.0
150156
t = tests[i]
151157
try:
152158
test_command = [args.tool]
@@ -166,9 +172,11 @@ def run_test(args, i, tests): # type: (argparse.Namespace, int, List[Dict[str,
166172
sys.stderr.write("\rTest [%i/%i] " % (i + 1, len(tests)))
167173
sys.stderr.flush()
168174

175+
start_time = time.time()
169176
process = subprocess.Popen(test_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
170177
outstr, outerr = process.communicate()
171178
return_code = process.poll()
179+
duration = time.time() - start_time
172180
if return_code:
173181
raise subprocess.CalledProcessError(return_code, " ".join(test_command))
174182

@@ -179,13 +187,13 @@ def run_test(args, i, tests): # type: (argparse.Namespace, int, List[Dict[str,
179187
_logger.error(outerr)
180188
except subprocess.CalledProcessError as err:
181189
if err.returncode == UNSUPPORTED_FEATURE:
182-
return TestResult(UNSUPPORTED_FEATURE, outstr, outerr)
190+
return TestResult(UNSUPPORTED_FEATURE, outstr, outerr, duration)
183191
else:
184192
_logger.error(u"""Test failed: %s""", " ".join([pipes.quote(tc) for tc in test_command]))
185193
_logger.error(t.get("doc"))
186194
_logger.error("Returned non-zero")
187195
_logger.error(outerr)
188-
return TestResult(1, outstr, outerr)
196+
return TestResult(1, outstr, outerr, duration)
189197
except (yamlscanner.ScannerError, TypeError) as e:
190198
_logger.error(u"""Test failed: %s""", " ".join([pipes.quote(tc) for tc in test_command]))
191199
_logger.error(outstr)
@@ -208,7 +216,7 @@ def run_test(args, i, tests): # type: (argparse.Namespace, int, List[Dict[str,
208216
if outdir:
209217
shutil.rmtree(outdir, True)
210218

211-
return TestResult((1 if failed else 0), outstr, outerr)
219+
return TestResult((1 if failed else 0), outstr, outerr, duration)
212220

213221

214222
def main(): # type: () -> int

0 commit comments

Comments
 (0)