|
2 | 2 | import unittest
|
3 | 3 | import os
|
4 | 4 | import tempfile
|
| 5 | +import textwrap |
5 | 6 | from xml.dom import minidom
|
6 | 7 | from junit_xml import (TestCase, TestSuite)
|
7 | 8 |
|
@@ -41,21 +42,59 @@ def serialize_and_read(test_suites, to_file=False, prettyprint=None):
|
41 | 42 |
|
42 | 43 |
|
43 | 44 | class TestSuiteTests(unittest.TestCase):
|
| 45 | + def test_single_suite_single_test_case(self): |
| 46 | + try: |
| 47 | + (ts, tcs) = serialize_and_read(TestSuite('test', TestCase('Test1')), to_file=True)[0] |
| 48 | + self.fail("This should've raised an exeception") # pragma: nocover |
| 49 | + except Exception as exc: |
| 50 | + self.assertEqual(str(exc), 'test_cases must be a list of test cases') |
| 51 | + |
| 52 | + def test_single_suite_no_test_cases(self): |
| 53 | + properties = {'foo': 'bar'} |
| 54 | + package = 'mypackage' |
| 55 | + timestamp = 1398382805 |
| 56 | + |
| 57 | + (ts, tcs) = serialize_and_read( |
| 58 | + TestSuite( |
| 59 | + 'test', |
| 60 | + [], |
| 61 | + hostname='localhost', |
| 62 | + id=1, |
| 63 | + properties=properties, |
| 64 | + package=package, |
| 65 | + timestamp=timestamp |
| 66 | + ), |
| 67 | + to_file=True |
| 68 | + )[0] |
| 69 | + self.assertEqual(ts.tagName, 'testsuite') |
| 70 | + self.assertEqual(ts.attributes['package'].value, package) |
| 71 | + self.assertEqual(ts.attributes['timestamp'].value, str(timestamp)) |
| 72 | + self.assertEqual( |
| 73 | + ts.childNodes[1].childNodes[1].attributes['name'].value, |
| 74 | + 'foo') |
| 75 | + self.assertEqual( |
| 76 | + ts.childNodes[1].childNodes[1].attributes['value'].value, |
| 77 | + 'bar') |
| 78 | + |
44 | 79 | def test_single_suite_to_file(self):
|
45 |
| - (ts, tcs) = serialize_and_read(TestSuite('test', [TestCase('Test1')]), True)[0] |
| 80 | + (ts, tcs) = serialize_and_read(TestSuite('test', [TestCase('Test1')]), to_file=True)[0] |
46 | 81 | verify_test_case(self, tcs[0], {'name': 'Test1'})
|
47 | 82 |
|
48 | 83 | def test_single_suite_to_file_prettyprint(self):
|
49 |
| - (ts, tcs) = serialize_and_read(TestSuite('test', [TestCase('Test1')]), True, prettyprint=True)[0] |
| 84 | + (ts, tcs) = serialize_and_read(TestSuite('test', [TestCase('Test1')]), to_file=True, prettyprint=True)[0] |
| 85 | + verify_test_case(self, tcs[0], {'name': 'Test1'}) |
| 86 | + |
| 87 | + def test_single_suite_prettyprint(self): |
| 88 | + (ts, tcs) = serialize_and_read(TestSuite('test', [TestCase('Test1')]), to_file=False, prettyprint=True)[0] |
50 | 89 | verify_test_case(self, tcs[0], {'name': 'Test1'})
|
51 | 90 |
|
52 | 91 | def test_single_suite_to_file_no_prettyprint(self):
|
53 |
| - (ts, tcs) = serialize_and_read(TestSuite('test', [TestCase('Test1')]), True, prettyprint=False)[0] |
| 92 | + (ts, tcs) = serialize_and_read(TestSuite('test', [TestCase('Test1')]), to_file=True, prettyprint=False)[0] |
54 | 93 | verify_test_case(self, tcs[0], {'name': 'Test1'})
|
55 | 94 |
|
56 | 95 | def test_multiple_suites_to_file(self):
|
57 | 96 | tss = [TestSuite('suite1', [TestCase('Test1')]), TestSuite('suite2', [TestCase('Test2')])]
|
58 |
| - suites = serialize_and_read(tss, True) |
| 97 | + suites = serialize_and_read(tss, to_file=True) |
59 | 98 |
|
60 | 99 | self.assertEqual('suite1', suites[0][0].attributes['name'].value)
|
61 | 100 | verify_test_case(self, suites[0][1][0], {'name': 'Test1'})
|
@@ -88,6 +127,31 @@ def test_attribute_time(self):
|
88 | 127 |
|
89 | 128 | # TODO: add more tests for the other attributes and properties
|
90 | 129 |
|
| 130 | + def test_to_xml_string(self): |
| 131 | + test_suites = [TestSuite('suite1', [TestCase('Test1')]), |
| 132 | + TestSuite('suite2', [TestCase('Test2')])] |
| 133 | + xml_string = TestSuite.to_xml_string(test_suites) |
| 134 | + expected_xml_string = textwrap.dedent(""" |
| 135 | + <?xml version="1.0" ?> |
| 136 | + <testsuites> |
| 137 | + \t<testsuite errors="0" failures="0" name="suite1" skipped="0" tests="1" time="0"> |
| 138 | + \t\t<testcase name="Test1"/> |
| 139 | + \t</testsuite> |
| 140 | + \t<testsuite errors="0" failures="0" name="suite2" skipped="0" tests="1" time="0"> |
| 141 | + \t\t<testcase name="Test2"/> |
| 142 | + \t</testsuite> |
| 143 | + </testsuites> |
| 144 | + """.strip("\n")) |
| 145 | + self.assertEqual(xml_string, expected_xml_string) |
| 146 | + |
| 147 | + def test_to_xml_string_test_suites_not_a_list(self): |
| 148 | + test_suites = TestSuite('suite1', [TestCase('Test1')]) |
| 149 | + |
| 150 | + try: |
| 151 | + TestSuite.to_xml_string(test_suites) |
| 152 | + except Exception as exc: |
| 153 | + self.assertEqual(str(exc), 'test_suites must be a list of test suites') |
| 154 | + |
91 | 155 |
|
92 | 156 | class TestCaseTests(unittest.TestCase):
|
93 | 157 | def test_init(self):
|
@@ -168,6 +232,17 @@ def test_init_skipped_output(self):
|
168 | 232 | (ts, tcs) = serialize_and_read(TestSuite('test', [tc]))[0]
|
169 | 233 | verify_test_case(self, tcs[0], {'name': 'Skipped-Output'}, skipped_output="I skipped!")
|
170 | 234 |
|
| 235 | + def test_init_skipped_err_output(self): |
| 236 | + tc = TestCase('Skipped-Output') |
| 237 | + tc.add_skipped_info(output="I skipped!") |
| 238 | + tc.add_error_info(output="I skipped with an error!") |
| 239 | + (ts, tcs) = serialize_and_read(TestSuite('test', [tc]))[0] |
| 240 | + verify_test_case( |
| 241 | + self, tcs[0], |
| 242 | + {'name': 'Skipped-Output'}, |
| 243 | + skipped_output="I skipped!", |
| 244 | + error_output="I skipped with an error!") |
| 245 | + |
171 | 246 | def test_init_skipped(self):
|
172 | 247 | tc = TestCase('Skipped-Message-and-Output')
|
173 | 248 | tc.add_skipped_info("skipped message", "I skipped!")
|
|
0 commit comments