1 # Copyright 2014 Dirk Pranke. All rights reserved.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from textwrap import dedent as d
20 from typ import Host, Runner, TestCase, TestSet, TestInput
21 from typ import WinMultiprocessing
24 def _setup_process(child, context): # pylint: disable=W0613
28 def _teardown_process(child, context): # pylint: disable=W0613
32 class RunnerTests(TestCase):
33 def test_context(self):
35 r.args.tests = ['typ.tests.runner_test.ContextTests']
36 r.context = {'foo': 'bar'}
37 r.setup_fn = _setup_process
38 r.teardown_fn = _teardown_process
39 r.win_multiprocessing = WinMultiprocessing.importable
41 self.assertEqual(ret, 0)
43 def test_bad_default(self):
45 ret = r.main([], foo='bar')
46 self.assertEqual(ret, 2)
48 def test_good_default(self):
50 ret = r.main([], tests=['typ.tests.runner_test.ContextTests'])
51 self.assertEqual(ret, 0)
54 class TestSetTests(TestCase):
55 # This class exists to test the failures that can come up if you
56 # create your own test sets and bypass find_tests(); failures that
57 # would normally be caught there can occur later during test execution.
59 def test_missing_name(self):
61 test_set.parallel_tests = [TestInput('nonexistent test')]
64 ret, _, _ = r.run(test_set)
65 self.assertEqual(ret, 1)
67 def test_failing_load_test(self):
74 h.write_text_file('load_test.py', d("""\
76 def load_tests(_, _2, _3):
80 test_set.parallel_tests = [TestInput('load_test.BaseTest.test_x')]
83 ret, _, _ = r.run(test_set)
84 self.assertEqual(ret, 1)
91 class TestWinMultiprocessing(TestCase):
95 def call(self, argv, platform=None, win_multiprocessing=None, **kwargs):
103 if platform is not None:
104 h.platform = platform
106 if win_multiprocessing is not None:
107 r.win_multiprocessing = win_multiprocessing
108 ret = r.main(argv, **kwargs)
110 out, err = h.restore_output()
117 def test_bad_value(self):
118 self.assertRaises(ValueError, self.call, [], win_multiprocessing='foo')
120 def test_ignore(self):
122 if h.platform == 'win32': # pragma: win32
123 self.assertRaises(ValueError, self.call, [],
124 win_multiprocessing=WinMultiprocessing.ignore)
126 result = self.call([],
127 win_multiprocessing=WinMultiprocessing.ignore)
128 ret, out, err = result
129 self.assertEqual(ret, 1)
130 self.assertEqual(out, 'No tests to run.\n')
131 self.assertEqual(err, '')
133 def test_real_unimportable_main(self):
138 out_str = err_str = ''
142 out = tempfile.NamedTemporaryFile(delete=False)
143 err = tempfile.NamedTemporaryFile(delete=False)
144 path_above_typ = h.realpath(h.dirname(__file__), '..', '..')
146 if 'PYTHONPATH' in env: # pragma: untested
147 env['PYTHONPATH'] = '%s%s%s' % (env['PYTHONPATH'],
150 else: # pragma: untested.
151 env['PYTHONPATH'] = path_above_typ
153 h.write_text_file('test', d("""
156 importable = typ.WinMultiprocessing.importable
157 sys.exit(typ.main(win_multiprocessing=importable))
161 ret = h.call_inline([h.python_interpreter, h.join(tmpdir, 'test')],
180 self.assertEqual(ret, 1)
181 self.assertEqual(out_str, '')
182 self.assertIn('ValueError: The __main__ module ',
185 def test_single_job(self):
186 ret, out, err = self.call(['-j', '1'], platform='win32')
187 self.assertEqual(ret, 1)
188 self.assertIn('No tests to run.', out)
189 self.assertEqual(err, '')
191 def test_spawn(self):
192 ret, out, err = self.call([])
193 self.assertEqual(ret, 1)
194 self.assertIn('No tests to run.', out)
195 self.assertEqual(err, '')
198 class ContextTests(TestCase):
199 def test_context(self):
200 # This test is mostly intended to be called by
201 # RunnerTests.test_context, above. It is not interesting on its own.
203 self.assertEquals(self.context['foo'], 'bar')