##// END OF EJS Templates
run-tests: added '--json' functionality to store test result in json file...
anuraggoel -
r22391:c42e6926 default
parent child Browse files
Show More
@@ -60,6 +60,14 b' import Queue as queue'
60 from xml.dom import minidom
60 from xml.dom import minidom
61 import unittest
61 import unittest
62
62
63 try:
64 if sys.version_info < (2, 7):
65 import simplejson as json
66 else:
67 import json
68 except ImportError:
69 json = None
70
63 processlock = threading.Lock()
71 processlock = threading.Lock()
64
72
65 # subprocess._cleanup can race with any Popen.wait or Popen.poll on py24
73 # subprocess._cleanup can race with any Popen.wait or Popen.poll on py24
@@ -186,6 +194,8 b' def getparser():'
186 " (default: $%s or %d)" % defaults['timeout'])
194 " (default: $%s or %d)" % defaults['timeout'])
187 parser.add_option("--time", action="store_true",
195 parser.add_option("--time", action="store_true",
188 help="time how long each test takes")
196 help="time how long each test takes")
197 parser.add_option("--json", action="store_true",
198 help="store test result data in 'report.json' file")
189 parser.add_option("--tmpdir", type="string",
199 parser.add_option("--tmpdir", type="string",
190 help="run tests in the given temporary directory"
200 help="run tests in the given temporary directory"
191 " (implies --keep-tmpdir)")
201 " (implies --keep-tmpdir)")
@@ -1419,6 +1429,37 b' class TextTestRunner(unittest.TextTestRu'
1419 finally:
1429 finally:
1420 xuf.close()
1430 xuf.close()
1421
1431
1432 if self._runner.options.json:
1433 if json is None:
1434 raise ImportError("json module not installed")
1435 jsonpath = os.path.join(self._runner._testdir, 'report.json')
1436 fp = open(jsonpath, 'w')
1437 try:
1438 timesd = {}
1439 for test, cuser, csys, real in result.times:
1440 timesd[test] = real
1441
1442 outcome = {}
1443 for tc in result.successes:
1444 testresult = {'result': 'success',
1445 'time': ('%0.3f' % timesd[tc.name])}
1446 outcome[tc.name] = testresult
1447
1448 for tc, err in sorted(result.faildata.iteritems()):
1449 testresult = {'result': 'failure',
1450 'time': ('%0.3f' % timesd[tc])}
1451 outcome[tc] = testresult
1452
1453 for tc, reason in result.skipped:
1454 testresult = {'result': 'skip',
1455 'time': ('%0.3f' % timesd[tc.name])}
1456 outcome[tc.name] = testresult
1457
1458 jsonout = json.dumps(outcome, sort_keys=True, indent=4)
1459 fp.writelines(("testreport =", jsonout))
1460 finally:
1461 fp.close()
1462
1422 self._runner._checkhglib('Tested')
1463 self._runner._checkhglib('Tested')
1423
1464
1424 self.stream.writeln('# Ran %d tests, %d skipped, %d warned, %d failed.'
1465 self.stream.writeln('# Ran %d tests, %d skipped, %d warned, %d failed.'
@@ -369,3 +369,40 b" Missing skips or blacklisted skips don't"
369 Skipped test-failure.t: blacklisted
369 Skipped test-failure.t: blacklisted
370 # Ran 0 tests, 2 skipped, 0 warned, 0 failed.
370 # Ran 0 tests, 2 skipped, 0 warned, 0 failed.
371
371
372 test for --json
373 ==================
374
375 $ $TESTDIR/run-tests.py --with-hg=`which hg` --json
376
377 --- $TESTTMP/test-failure.t
378 +++ $TESTTMP/test-failure.t.err
379 @@ -1,4 +1,4 @@
380 $ echo babar
381 - rataxes
382 + babar
383 This is a noop statement so that
384 this test is still more bytes than success.
385
386 ERROR: test-failure.t output changed
387 !.s
388 Skipped test-skip.t: skipped
389 Failed test-failure.t: output changed
390 # Ran 2 tests, 1 skipped, 0 warned, 1 failed.
391 python hash seed: * (glob)
392 [1]
393
394 $ cat report.json
395 testreport ={
396 "test-failure.t": [\{] (re)
397 "result": "failure",
398 "time": "\s*[\d\.]{5}" (re)
399 },
400 "test-skip.t": {
401 "result": "skip",
402 "time": "\s*[\d\.]{5}" (re)
403 },
404 "test-success.t": [\{] (re)
405 "result": "success",
406 "time": "\s*[\d\.]{5}" (re)
407 }
408 } (no-eol)
General Comments 0
You need to be logged in to leave comments. Login now