Debian Codegraph @

python/tests/test_run.py

raw
1 #
2 # subunit: extensions to python unittest to get test results from subprocesses.
3 # Copyright (C) 2011 Robert Collins <[email protected]>
4 #
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.
9 #
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.
15 #
16
17 import io
18 import unittest
19
20 from testtools import PlaceHolder, TestCase
21
22 from testtools.matchers import StartsWith
23 from testtools.testresult.doubles import StreamResult
24
25 import subunit
26 from subunit import run
27 from subunit.run import SubunitTestRunner
28
29
30 class TestSubunitTestRunner(TestCase):
31 def test_includes_timing_output(self):
32 bytestream = io.BytesIO()
33 runner = SubunitTestRunner(stream=bytestream)
34 test = PlaceHolder("name")
35 runner.run(test)
36 bytestream.seek(0)
37 eventstream = StreamResult()
38 subunit.ByteStreamToStreamResult(bytestream).run(eventstream)
39 timestamps = [event[-1] for event in eventstream._events if event is not None]
40 self.assertNotEqual([], timestamps)
41
42 def test_enumerates_tests_before_run(self):
43 bytestream = io.BytesIO()
44 runner = SubunitTestRunner(stream=bytestream)
45 test1 = PlaceHolder("name1")
46 test2 = PlaceHolder("name2")
47 case = unittest.TestSuite([test1, test2])
48 runner.run(case)
49 bytestream.seek(0)
50 eventstream = StreamResult()
51 subunit.ByteStreamToStreamResult(bytestream).run(eventstream)
52 self.assertEqual(
53 [
54 ("status", "name1", "exists"),
55 ("status", "name2", "exists"),
56 ],
57 [event[:3] for event in eventstream._events[:2]],
58 )
59
60 def test_list_errors_if_errors_from_list_test(self):
61 bytestream = io.BytesIO()
62 runner = SubunitTestRunner(stream=bytestream)
63
64 def list_test(test):
65 return [], ["failed import"]
66
67 self.patch(run, "list_test", list_test)
68 exc = self.assertRaises(SystemExit, runner.list, None)
69 self.assertEqual((2,), exc.args)
70
71 def test_list_includes_loader_errors(self):
72 bytestream = io.BytesIO()
73 runner = SubunitTestRunner(stream=bytestream)
74
75 def list_test(test):
76 return [], []
77
78 class Loader:
79 errors = ["failed import"]
80
81 loader = Loader()
82 self.patch(run, "list_test", list_test)
83 exc = self.assertRaises(SystemExit, runner.list, None, loader=loader)
84 self.assertEqual((2,), exc.args)
85
86 class FailingTest(TestCase):
87 def test_fail(self):
88 1 / 0
89
90 def test_exits_zero_when_tests_fail(self):
91 bytestream = io.BytesIO()
92 stream = io.TextIOWrapper(bytestream, encoding="utf8")
93 try:
94 self.assertEqual(
95 None,
96 run.main(argv=["progName", "tests.test_run.TestSubunitTestRunner.FailingTest"], stdout=stream),
97 )
98 except SystemExit:
99 self.fail("SystemExit raised")
100 self.assertThat(bytestream.getvalue(), StartsWith(b"\xb3"))
101
102 class ExitingTest(TestCase):
103 def test_exit(self):
104 raise SystemExit(0)
105
106 def test_exits_nonzero_when_execution_errors(self):
107 bytestream = io.BytesIO()
108 stream = io.TextIOWrapper(bytestream, encoding="utf8")
109 exc = self.assertRaises(
110 SystemExit,
111 run.main,
112 argv=["progName", "tests.test_run.TestSubunitTestRunner.ExitingTest"],
113 stdout=stream,
114 )
115 self.assertEqual(0, exc.args[0])