comparison tests/run-tests.py @ 32700:3afe258fb0fe

run-tests: factor out xunit write code into another method We're going to use this code to output an XUnit-formatted listing of tests.
author Siddharth Agarwal <sid0@fb.com>
date Tue, 06 Jun 2017 13:10:55 -0700
parents 931bb962e0eb
children 60c921ff4104
comparison
equal deleted inserted replaced
32699:f75d0aa5dc83 32700:3afe258fb0fe
1930 self.stream.writeln('Failed %s: %s' % (test.name, msg)) 1930 self.stream.writeln('Failed %s: %s' % (test.name, msg))
1931 for test, msg in result.errors: 1931 for test, msg in result.errors:
1932 self.stream.writeln('Errored %s: %s' % (test.name, msg)) 1932 self.stream.writeln('Errored %s: %s' % (test.name, msg))
1933 1933
1934 if self._runner.options.xunit: 1934 if self._runner.options.xunit:
1935 with open(self._runner.options.xunit, 'wb') as xuf: 1935 with open(self._runner.options.xunit, "wb") as xuf:
1936 timesd = dict((t[0], t[3]) for t in result.times) 1936 self._writexunit(result, xuf)
1937 doc = minidom.Document()
1938 s = doc.createElement('testsuite')
1939 s.setAttribute('name', 'run-tests')
1940 s.setAttribute('tests', str(result.testsRun))
1941 s.setAttribute('errors', "0") # TODO
1942 s.setAttribute('failures', str(failed))
1943 s.setAttribute('skipped', str(skipped + ignored))
1944 doc.appendChild(s)
1945 for tc in result.successes:
1946 t = doc.createElement('testcase')
1947 t.setAttribute('name', tc.name)
1948 t.setAttribute('time', '%.3f' % timesd[tc.name])
1949 s.appendChild(t)
1950 for tc, err in sorted(result.faildata.items()):
1951 t = doc.createElement('testcase')
1952 t.setAttribute('name', tc)
1953 t.setAttribute('time', '%.3f' % timesd[tc])
1954 # createCDATASection expects a unicode or it will
1955 # convert using default conversion rules, which will
1956 # fail if string isn't ASCII.
1957 err = cdatasafe(err).decode('utf-8', 'replace')
1958 cd = doc.createCDATASection(err)
1959 t.appendChild(cd)
1960 s.appendChild(t)
1961 xuf.write(doc.toprettyxml(indent=' ', encoding='utf-8'))
1962 1937
1963 if self._runner.options.json: 1938 if self._runner.options.json:
1964 jsonpath = os.path.join(self._runner._testdir, b'report.json') 1939 jsonpath = os.path.join(self._runner._testdir, b'report.json')
1965 with open(jsonpath, 'w') as fp: 1940 with open(jsonpath, 'w') as fp:
1966 timesd = {} 1941 timesd = {}
2053 for tdata in times: 2028 for tdata in times:
2054 test = tdata[0] 2029 test = tdata[0]
2055 cuser, csys, real, start, end = tdata[1:6] 2030 cuser, csys, real, start, end = tdata[1:6]
2056 self.stream.writeln(cols % (start, end, cuser, csys, real, test)) 2031 self.stream.writeln(cols % (start, end, cuser, csys, real, test))
2057 2032
2033 @staticmethod
2034 def _writexunit(result, outf):
2035 timesd = dict((t[0], t[3]) for t in result.times)
2036 doc = minidom.Document()
2037 s = doc.createElement('testsuite')
2038 s.setAttribute('name', 'run-tests')
2039 s.setAttribute('tests', str(result.testsRun))
2040 s.setAttribute('errors', "0") # TODO
2041 s.setAttribute('failures', str(len(result.failures)))
2042 s.setAttribute('skipped', str(len(result.skipped) +
2043 len(result.ignored)))
2044 doc.appendChild(s)
2045 for tc in result.successes:
2046 t = doc.createElement('testcase')
2047 t.setAttribute('name', tc.name)
2048 t.setAttribute('time', '%.3f' % timesd[tc.name])
2049 s.appendChild(t)
2050 for tc, err in sorted(result.faildata.items()):
2051 t = doc.createElement('testcase')
2052 t.setAttribute('name', tc)
2053 t.setAttribute('time', '%.3f' % timesd[tc])
2054 # createCDATASection expects a unicode or it will
2055 # convert using default conversion rules, which will
2056 # fail if string isn't ASCII.
2057 err = cdatasafe(err).decode('utf-8', 'replace')
2058 cd = doc.createCDATASection(err)
2059 t.appendChild(cd)
2060 s.appendChild(t)
2061 outf.write(doc.toprettyxml(indent=' ', encoding='utf-8'))
2062
2058 class TestRunner(object): 2063 class TestRunner(object):
2059 """Holds context for executing tests. 2064 """Holds context for executing tests.
2060 2065
2061 Tests rely on a lot of state. This object holds it for them. 2066 Tests rely on a lot of state. This object holds it for them.
2062 """ 2067 """