1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
4 | # Copyright (C) 2006 Matthew Good <matt@matt-good.net> |
---|
5 | # Copyright (C) 2007-2010 Edgewall Software |
---|
6 | # All rights reserved. |
---|
7 | # |
---|
8 | # This software is licensed as described in the file COPYING, which |
---|
9 | # you should have received as part of this distribution. The terms |
---|
10 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
11 | |
---|
12 | import os |
---|
13 | import os.path |
---|
14 | import shutil |
---|
15 | import tempfile |
---|
16 | import unittest |
---|
17 | |
---|
18 | from bitten.build import javatools |
---|
19 | from bitten.recipe import Context, Recipe |
---|
20 | |
---|
21 | class CoberturaTestCase(unittest.TestCase): |
---|
22 | xml_template="""<?xml version="1.0"?> |
---|
23 | <!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-02.dtd"> |
---|
24 | |
---|
25 | <coverage timestamp="1148533713840"> |
---|
26 | <sources> |
---|
27 | <source>src</source> |
---|
28 | </sources> |
---|
29 | <packages> |
---|
30 | <package name="test"> |
---|
31 | <classes>%s |
---|
32 | </classes> |
---|
33 | </package> |
---|
34 | </packages> |
---|
35 | </coverage>""" |
---|
36 | |
---|
37 | def setUp(self): |
---|
38 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
39 | self.ctxt = Context(self.basedir) |
---|
40 | |
---|
41 | def tearDown(self): |
---|
42 | shutil.rmtree(self.basedir) |
---|
43 | |
---|
44 | def _create_file(self, *path, **kw): |
---|
45 | filename = os.path.join(self.basedir, *path) |
---|
46 | dirname = os.path.dirname(filename) |
---|
47 | if not os.path.exists(dirname): |
---|
48 | os.makedirs(dirname) |
---|
49 | fd = file(filename, 'w') |
---|
50 | content = kw.get('content') |
---|
51 | if content is not None: |
---|
52 | fd.write(content) |
---|
53 | fd.close() |
---|
54 | return filename[len(self.basedir) + 1:] |
---|
55 | |
---|
56 | def test_basic(self): |
---|
57 | filename = self._create_file('coverage.xml', content=self.xml_template % """ |
---|
58 | <class name="test.TestClass" filename="test/TestClass.java"> |
---|
59 | <lines> |
---|
60 | <line number="1" hits="0" branch="false"/> |
---|
61 | <line number="2" hits="1" branch="false"/> |
---|
62 | <line number="3" hits="0" branch="false"/> |
---|
63 | <line number="4" hits="2" branch="false"/> |
---|
64 | </lines> |
---|
65 | </class>""") |
---|
66 | javatools.cobertura(self.ctxt, file_=filename) |
---|
67 | type, category, generator, xml = self.ctxt.output.pop() |
---|
68 | self.assertEqual('report', type) |
---|
69 | self.assertEqual('coverage', category) |
---|
70 | self.assertEqual(1, len(xml.children)) |
---|
71 | |
---|
72 | elem = xml.children[0] |
---|
73 | self.assertEqual('coverage', elem.name) |
---|
74 | self.assertEqual('src/test/TestClass.java', elem.attr['file']) |
---|
75 | self.assertEqual('test.TestClass', elem.attr['name']) |
---|
76 | self.assertEqual(4, elem.attr['lines']) |
---|
77 | self.assertEqual(50, elem.attr['percentage']) |
---|
78 | |
---|
79 | def test_skipped_lines(self): |
---|
80 | filename = self._create_file('coverage.xml', content=self.xml_template % """ |
---|
81 | <class name="test.TestClass" filename="test/TestClass.java"> |
---|
82 | <lines> |
---|
83 | <line number="1" hits="0" branch="false"/> |
---|
84 | <line number="3" hits="1" branch="false"/> |
---|
85 | </lines> |
---|
86 | </class>""") |
---|
87 | javatools.cobertura(self.ctxt, file_=filename) |
---|
88 | type, category, generator, xml = self.ctxt.output.pop() |
---|
89 | self.assertEqual('report', type) |
---|
90 | self.assertEqual('coverage', category) |
---|
91 | self.assertEqual(1, len(xml.children)) |
---|
92 | |
---|
93 | elem = xml.children[0] |
---|
94 | self.assertEqual('coverage', elem.name) |
---|
95 | self.assertEqual('src/test/TestClass.java', elem.attr['file']) |
---|
96 | self.assertEqual('test.TestClass', elem.attr['name']) |
---|
97 | self.assertEqual(2, elem.attr['lines']) |
---|
98 | self.assertEqual(50, elem.attr['percentage']) |
---|
99 | |
---|
100 | line_hits = elem.children[0] |
---|
101 | self.assertEqual('line_hits', line_hits.name) |
---|
102 | self.assertEqual('0 - 1', line_hits.children[0]) |
---|
103 | |
---|
104 | def test_interface(self): |
---|
105 | filename = self._create_file('coverage.xml', content=self.xml_template % """ |
---|
106 | <class name="test.TestInterface" filename="test/TestInterface.java"> |
---|
107 | <lines> |
---|
108 | </lines> |
---|
109 | </class>""") |
---|
110 | javatools.cobertura(self.ctxt, file_=filename) |
---|
111 | type, category, generator, xml = self.ctxt.output.pop() |
---|
112 | self.assertEqual('report', type) |
---|
113 | self.assertEqual('coverage', category) |
---|
114 | self.assertEqual(1, len(xml.children)) |
---|
115 | |
---|
116 | elem = xml.children[0] |
---|
117 | self.assertEqual('coverage', elem.name) |
---|
118 | self.assertEqual('src/test/TestInterface.java', elem.attr['file']) |
---|
119 | self.assertEqual('test.TestInterface', elem.attr['name']) |
---|
120 | self.assertEqual(0, elem.attr['lines']) |
---|
121 | self.assertEqual(0, elem.attr['percentage']) |
---|
122 | |
---|
123 | class JUnitTestCase(unittest.TestCase): |
---|
124 | xml_template = """<testsuite name="%(name)s" errors="%(errors)d" |
---|
125 | failures="%(failures)d" skips="%(skips)d" tests="%(tests)d" time="%(time)f"> |
---|
126 | %(body)s |
---|
127 | </testsuite> |
---|
128 | """ |
---|
129 | # <testsuite> top level |
---|
130 | xml_template1 = """<?xml version="1.0" encoding="UTF-8"?> |
---|
131 | <testsuite name="Vector" tests="2" failures="0" disabled="0" errors="0" time="0.001"> |
---|
132 | <properties> |
---|
133 | <property name="javacDebug" value="false" /> |
---|
134 | </properties> |
---|
135 | <testcase name="Static" status="run" time="0" classname="Vector" /> |
---|
136 | <testcase name="Dynamic" status="run" time="0" classname="Vector" /> |
---|
137 | <testcase name="Proxy" status="run" time="0" classname="Vector" /> |
---|
138 | <system-out><![CDATA[]]></system-out> |
---|
139 | <system-err><![CDATA[]]></system-err> |
---|
140 | </testsuite>""" |
---|
141 | # <testsuites> top level |
---|
142 | xml_template2 = """<?xml version="1.0" encoding="UTF-8"?> |
---|
143 | <testsuites tests="3" failures="0" disabled="0" errors="0" time="0.002" name="AllTests"> |
---|
144 | <testsuite name="Vector" tests="2" failures="0" disabled="0" errors="0" time="0.001"> |
---|
145 | <testcase name="Static" status="run" time="0" classname="Vector" /> |
---|
146 | <testcase name="Dynamic" status="run" time="0" classname="Vector" /> |
---|
147 | </testsuite> |
---|
148 | <testsuite name="Vector" tests="1" failures="0" disabled="0" errors="0" time="0.001"> |
---|
149 | <properties> |
---|
150 | <property name="javacDebug" value="false" /> |
---|
151 | </properties> |
---|
152 | <testcase name="Proxy" status="run" time="0" classname="Vector" /> |
---|
153 | <system-out><![CDATA[]]></system-out> |
---|
154 | <system-err><![CDATA[]]></system-err> |
---|
155 | </testsuite> |
---|
156 | </testsuites>""" |
---|
157 | |
---|
158 | def setUp(self): |
---|
159 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
160 | self.ctxt = Context(self.basedir) |
---|
161 | |
---|
162 | def tearDown(self): |
---|
163 | shutil.rmtree(self.basedir) |
---|
164 | |
---|
165 | def _create_file(self, *path, **kw): |
---|
166 | filename = os.path.join(self.basedir, *path) |
---|
167 | dirname = os.path.dirname(filename) |
---|
168 | if not os.path.exists(dirname): |
---|
169 | os.makedirs(dirname) |
---|
170 | fd = file(filename, 'w') |
---|
171 | content = kw.get('content') |
---|
172 | if content is not None: |
---|
173 | fd.write(content) |
---|
174 | fd.close() |
---|
175 | return filename[len(self.basedir) + 1:] |
---|
176 | |
---|
177 | def _xml_file(self, body, name="", errors=0, failures=0, skips=0, tests=0, time=0.01): |
---|
178 | if tests == 0: |
---|
179 | tests = errors + failures + skips |
---|
180 | (fd, path) = tempfile.mkstemp(prefix="junit", suffix=".xml", dir=self.basedir, text=True) |
---|
181 | stream = os.fdopen(fd, "w") |
---|
182 | content = self.xml_template % dict(body=body, name=name, errors=errors, |
---|
183 | failures=failures, skips=skips, tests=tests, time=time) |
---|
184 | stream.write(content) |
---|
185 | stream.close() |
---|
186 | return path |
---|
187 | |
---|
188 | def test_simple(self): |
---|
189 | body = '<testcase classname="_test.test_event" name="test_simple" time="0.0002"></testcase>' |
---|
190 | filename = self._xml_file(body, tests=1) |
---|
191 | javatools.junit(self.ctxt, file_=filename) |
---|
192 | type, category, generator, xml = self.ctxt.output.pop() |
---|
193 | self.assertEqual('report', type) |
---|
194 | self.assertEqual('test', category) |
---|
195 | self.assertEqual(1, len(xml.children)) |
---|
196 | |
---|
197 | elem = xml.children[0] |
---|
198 | self.assertEqual('test', elem.name) |
---|
199 | self.assertEqual('test_simple', elem.attr['name']) |
---|
200 | self.assertEqual('success', elem.attr['status']) |
---|
201 | self.assertEqual(0, len(elem.children)) |
---|
202 | |
---|
203 | def test_setup_fail(self): |
---|
204 | """Check that py.test setup failures are handled""" |
---|
205 | body = '<testcase classname="_test.test_event" name="test_simple" time="0">' \ |
---|
206 | + '<error message="test setup failure">request = <FuncargRequest for <Function...</error>' \ |
---|
207 | + '</testcase>' |
---|
208 | filename = self._xml_file(body, errors=1) |
---|
209 | javatools.junit(self.ctxt, file_=filename) |
---|
210 | type, category, generator, xml = self.ctxt.output.pop() |
---|
211 | self.assertEqual('report', type) |
---|
212 | self.assertEqual('test', category) |
---|
213 | self.assertEqual(1, len(xml.children)) |
---|
214 | |
---|
215 | elem = xml.children[0] |
---|
216 | self.assertEqual('test', elem.name) |
---|
217 | self.assertEqual('test_simple', elem.attr['name']) |
---|
218 | self.assertEqual('error', elem.attr['status']) |
---|
219 | self.assertEqual(1, len(elem.children)) |
---|
220 | |
---|
221 | trace = elem.children[0] |
---|
222 | self.assertEqual('traceback', trace.name) |
---|
223 | self.assertEqual(1, len(trace.children)) |
---|
224 | self.assertEqual('request = <FuncargRequest for <Function...', trace.children[0]) |
---|
225 | |
---|
226 | type, category, generator, xml = self.ctxt.output.pop() |
---|
227 | self.assertEqual(Recipe.ERROR, type) |
---|
228 | |
---|
229 | self.assertEqual(0, len(self.ctxt.output)) |
---|
230 | |
---|
231 | def test_skipped_tests(self): |
---|
232 | """Check that skipped tests (here: xfail in py.test) are not considered an error""" |
---|
233 | body = '<testcase classname="_test.test_event" name="test_simple" time="0.06">' \ |
---|
234 | + '<skipped/></testcase>' |
---|
235 | filename = self._xml_file(body, skips=1) |
---|
236 | javatools.junit(self.ctxt, file_=filename) |
---|
237 | type, category, generator, xml = self.ctxt.output.pop() |
---|
238 | self.assertEqual('report', type) |
---|
239 | self.assertEqual('test', category) |
---|
240 | self.assertEqual(1, len(xml.children)) |
---|
241 | |
---|
242 | elem = xml.children[0] |
---|
243 | self.assertEqual('test', elem.name) |
---|
244 | self.assertEqual('test_simple', elem.attr['name']) |
---|
245 | self.assertEqual('ignore', elem.attr['status']) |
---|
246 | self.assertEqual(1, len(elem.children)) |
---|
247 | |
---|
248 | trace = elem.children[0] |
---|
249 | self.assertEqual('traceback', trace.name) |
---|
250 | self.assertEqual(0, len(trace.children)) |
---|
251 | |
---|
252 | self.assertEqual(0, len(self.ctxt.output)) |
---|
253 | |
---|
254 | def test_parse_and_compare_formats(self): |
---|
255 | # parse xml_template1 |
---|
256 | f1 = self._create_file('junit1.xml', content=self.xml_template1) |
---|
257 | javatools.junit(self.ctxt, file_=f1) |
---|
258 | type1, category1, generator1, xml1 = self.ctxt.output.pop() |
---|
259 | self.assertEqual('report', type1) |
---|
260 | self.assertEqual('test', category1) |
---|
261 | # parse xml_template2 |
---|
262 | f2 = self._create_file('junit2.xml', content=self.xml_template2) |
---|
263 | javatools.junit(self.ctxt, file_=f2) |
---|
264 | type2, category2, generator2, xml2 = self.ctxt.output.pop() |
---|
265 | self.assertEqual('report', type2) |
---|
266 | self.assertEqual('test', category2) |
---|
267 | # compare |
---|
268 | for i in range(2): |
---|
269 | self.assertEquals(xml1.children[i].name, xml2.children[i].name) |
---|
270 | self.assertEquals(xml1.children[i].attr, xml2.children[i].attr) |
---|
271 | |
---|
272 | def suite(): |
---|
273 | suite = unittest.TestSuite() |
---|
274 | suite.addTest(unittest.makeSuite(CoberturaTestCase, 'test')) |
---|
275 | suite.addTest(unittest.makeSuite(JUnitTestCase, 'test')) |
---|
276 | return suite |
---|
277 | |
---|
278 | if __name__ == '__main__': |
---|
279 | unittest.main(defaultTest='suite') |
---|