2 # subunit: extensions to python unittest to get test results from subprocesses.
3 # Copyright (C) 2005 Robert Collins <robertc@robertcollins.net>
5 # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
6 # license at the users choice. A copy of both licenses are available in the
7 # project source as Apache-2.0 and BSD. You may not use this file except in
8 # compliance with one of these two licences.
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # license you chose for the specific language governing permissions and
14 # limitations under that license.
19 from testtools.compat import _b, StringIO
22 from subunit import content, content_type, details
26 loader = subunit.tests.TestUtil.TestLoader()
27 result = loader.loadTestsFromName(__name__)
31 class TestSimpleDetails(unittest.TestCase):
33 def test_lineReceived(self):
34 parser = details.SimpleDetailsParser(None)
35 parser.lineReceived(_b("foo\n"))
36 parser.lineReceived(_b("bar\n"))
37 self.assertEqual(_b("foo\nbar\n"), parser._message)
39 def test_lineReceived_escaped_bracket(self):
40 parser = details.SimpleDetailsParser(None)
41 parser.lineReceived(_b("foo\n"))
42 parser.lineReceived(_b(" ]are\n"))
43 parser.lineReceived(_b("bar\n"))
44 self.assertEqual(_b("foo\n]are\nbar\n"), parser._message)
46 def test_get_message(self):
47 parser = details.SimpleDetailsParser(None)
48 self.assertEqual(_b(""), parser.get_message())
50 def test_get_details(self):
51 parser = details.SimpleDetailsParser(None)
54 expected['traceback'] = content.Content(
55 content_type.ContentType("text", "x-traceback",
58 found = parser.get_details()
59 self.assertEqual(expected.keys(), found.keys())
60 self.assertEqual(expected['traceback'].content_type,
61 found['traceback'].content_type)
62 self.assertEqual(_b('').join(expected['traceback'].iter_bytes()),
63 _b('').join(found['traceback'].iter_bytes()))
65 def test_get_details_skip(self):
66 parser = details.SimpleDetailsParser(None)
69 expected['reason'] = content.Content(
70 content_type.ContentType("text", "plain"),
72 found = parser.get_details("skip")
73 self.assertEqual(expected, found)
75 def test_get_details_success(self):
76 parser = details.SimpleDetailsParser(None)
79 expected['message'] = content.Content(
80 content_type.ContentType("text", "plain"),
82 found = parser.get_details("success")
83 self.assertEqual(expected, found)
86 class TestMultipartDetails(unittest.TestCase):
88 def test_get_message_is_None(self):
89 parser = details.MultipartDetailsParser(None)
90 self.assertEqual(None, parser.get_message())
92 def test_get_details(self):
93 parser = details.MultipartDetailsParser(None)
94 self.assertEqual({}, parser.get_details())
97 parser = details.MultipartDetailsParser(None)
98 parser.lineReceived(_b("Content-Type: text/plain\n"))
99 parser.lineReceived(_b("something\n"))
100 parser.lineReceived(_b("F\r\n"))
101 parser.lineReceived(_b("serialised\n"))
102 parser.lineReceived(_b("form0\r\n"))
104 expected['something'] = content.Content(
105 content_type.ContentType("text", "plain"),
106 lambda:[_b("serialised\nform")])
107 found = parser.get_details()
108 self.assertEqual(expected.keys(), found.keys())
109 self.assertEqual(expected['something'].content_type,
110 found['something'].content_type)
111 self.assertEqual(_b('').join(expected['something'].iter_bytes()),
112 _b('').join(found['something'].iter_bytes()))