13
13
import pipes
14
14
import logging
15
15
import schema_salad .ref_resolver
16
+ import time
16
17
from concurrent .futures import ThreadPoolExecutor
17
18
from typing import Any , Dict , List , Text
18
19
@@ -40,16 +41,20 @@ class TestResult(object):
40
41
41
42
"""Encapsulate relevant test result data."""
42
43
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
45
46
self .return_code = return_code
46
47
self .standard_output = standard_output
47
48
self .error_output = error_output
49
+ self .duration = duration
48
50
49
51
def create_test_case (self , test ):
50
52
# type: (Dict[Text, Any]) -> junit_xml.TestCase
51
53
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
+ )
53
58
54
59
55
60
def compare_file (expected , actual ):
@@ -147,6 +152,7 @@ def compare(expected, actual): # type: (Any, Any) -> None
147
152
def run_test (args , i , tests ): # type: (argparse.Namespace, int, List[Dict[str, str]]) -> TestResult
148
153
out = {} # type: Dict[str,Any]
149
154
outdir = outstr = outerr = test_command = None
155
+ duration = 0.0
150
156
t = tests [i ]
151
157
try :
152
158
test_command = [args .tool ]
@@ -166,9 +172,11 @@ def run_test(args, i, tests): # type: (argparse.Namespace, int, List[Dict[str,
166
172
sys .stderr .write ("\r Test [%i/%i] " % (i + 1 , len (tests )))
167
173
sys .stderr .flush ()
168
174
175
+ start_time = time .time ()
169
176
process = subprocess .Popen (test_command , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
170
177
outstr , outerr = process .communicate ()
171
178
return_code = process .poll ()
179
+ duration = time .time () - start_time
172
180
if return_code :
173
181
raise subprocess .CalledProcessError (return_code , " " .join (test_command ))
174
182
@@ -179,13 +187,13 @@ def run_test(args, i, tests): # type: (argparse.Namespace, int, List[Dict[str,
179
187
_logger .error (outerr )
180
188
except subprocess .CalledProcessError as err :
181
189
if err .returncode == UNSUPPORTED_FEATURE :
182
- return TestResult (UNSUPPORTED_FEATURE , outstr , outerr )
190
+ return TestResult (UNSUPPORTED_FEATURE , outstr , outerr , duration )
183
191
else :
184
192
_logger .error (u"""Test failed: %s""" , " " .join ([pipes .quote (tc ) for tc in test_command ]))
185
193
_logger .error (t .get ("doc" ))
186
194
_logger .error ("Returned non-zero" )
187
195
_logger .error (outerr )
188
- return TestResult (1 , outstr , outerr )
196
+ return TestResult (1 , outstr , outerr , duration )
189
197
except (yamlscanner .ScannerError , TypeError ) as e :
190
198
_logger .error (u"""Test failed: %s""" , " " .join ([pipes .quote (tc ) for tc in test_command ]))
191
199
_logger .error (outstr )
@@ -208,7 +216,7 @@ def run_test(args, i, tests): # type: (argparse.Namespace, int, List[Dict[str,
208
216
if outdir :
209
217
shutil .rmtree (outdir , True )
210
218
211
- return TestResult ((1 if failed else 0 ), outstr , outerr )
219
+ return TestResult ((1 if failed else 0 ), outstr , outerr , duration )
212
220
213
221
214
222
def main (): # type: () -> int
0 commit comments