1 # Copyright (c) 2010-2012 testtools developers. See LICENSE for details.
3 from testtools import TestCase
4 from testtools.helpers import (
8 from testtools.matchers import (
13 from testtools.tests.helpers import (
21 def check_error_callback(test, function, arg, expected_error_count,
23 """General test template for error_callback argument.
25 :param test: Test case instance.
26 :param function: Either try_import or try_imports.
27 :param arg: Name or names to import.
28 :param expected_error_count: Expected number of calls to the callback.
29 :param expect_result: Boolean for whether a module should
30 ultimately be returned or not.
34 test.assertIsInstance(e, ImportError)
37 result = function(arg, error_callback=cb)
39 test.assertFalse(expect_result)
42 test.assertThat(result, Not(Is(None)))
44 test.assertThat(result, Is(None))
45 test.assertEquals(len(cb_calls), expected_error_count)
48 class TestSafeHasattr(TestCase):
50 def test_attribute_not_there(self):
53 self.assertEqual(False, safe_hasattr(Foo(), 'anything'))
55 def test_attribute_there(self):
60 self.assertEqual(True, safe_hasattr(foo, 'attribute'))
62 def test_property_there(self):
68 self.assertEqual(True, safe_hasattr(foo, 'attribute'))
70 def test_property_raises(self):
76 self.assertRaises(ZeroDivisionError, safe_hasattr, foo, 'attribute')
79 class TestTryImport(TestCase):
81 def test_doesnt_exist(self):
82 # try_import('thing', foo) returns foo if 'thing' doesn't exist.
84 result = try_import('doesntexist', marker)
85 self.assertThat(result, Is(marker))
87 def test_None_is_default_alternative(self):
88 # try_import('thing') returns None if 'thing' doesn't exist.
89 result = try_import('doesntexist')
90 self.assertThat(result, Is(None))
92 def test_existing_module(self):
93 # try_import('thing', foo) imports 'thing' and returns it if it's a
95 result = try_import('os', object())
97 self.assertThat(result, Is(os))
99 def test_existing_submodule(self):
100 # try_import('thing.another', foo) imports 'thing' and returns it if
101 # it's a module that exists.
102 result = try_import('os.path', object())
104 self.assertThat(result, Is(os.path))
106 def test_nonexistent_submodule(self):
107 # try_import('thing.another', foo) imports 'thing' and returns foo if
108 # 'another' doesn't exist.
110 result = try_import('os.doesntexist', marker)
111 self.assertThat(result, Is(marker))
113 def test_object_from_module(self):
114 # try_import('thing.object') imports 'thing' and returns
115 # 'thing.object' if 'thing' is a module and 'object' is not.
116 result = try_import('os.path.join')
118 self.assertThat(result, Is(os.path.join))
120 def test_error_callback(self):
121 # the error callback is called on failures.
122 check_error_callback(self, try_import, 'doesntexist', 1, False)
124 def test_error_callback_missing_module_member(self):
125 # the error callback is called on failures to find an object
126 # inside an existing module.
127 check_error_callback(self, try_import, 'os.nonexistent', 1, False)
129 def test_error_callback_not_on_success(self):
130 # the error callback is not called on success.
131 check_error_callback(self, try_import, 'os.path', 0, True)
134 class TestTryImports(TestCase):
136 def test_doesnt_exist(self):
137 # try_imports('thing', foo) returns foo if 'thing' doesn't exist.
139 result = try_imports(['doesntexist'], marker)
140 self.assertThat(result, Is(marker))
142 def test_fallback(self):
143 result = try_imports(['doesntexist', 'os'])
145 self.assertThat(result, Is(os))
147 def test_None_is_default_alternative(self):
148 # try_imports('thing') returns None if 'thing' doesn't exist.
149 e = self.assertRaises(
150 ImportError, try_imports, ['doesntexist', 'noreally'])
153 Equals("Could not import any of: doesntexist, noreally"))
155 def test_existing_module(self):
156 # try_imports('thing', foo) imports 'thing' and returns it if it's a
157 # module that exists.
158 result = try_imports(['os'], object())
160 self.assertThat(result, Is(os))
162 def test_existing_submodule(self):
163 # try_imports('thing.another', foo) imports 'thing' and returns it if
164 # it's a module that exists.
165 result = try_imports(['os.path'], object())
167 self.assertThat(result, Is(os.path))
169 def test_nonexistent_submodule(self):
170 # try_imports('thing.another', foo) imports 'thing' and returns foo if
171 # 'another' doesn't exist.
173 result = try_imports(['os.doesntexist'], marker)
174 self.assertThat(result, Is(marker))
176 def test_fallback_submodule(self):
177 result = try_imports(['os.doesntexist', 'os.path'])
179 self.assertThat(result, Is(os.path))
181 def test_error_callback(self):
182 # One error for every class that doesn't exist.
183 check_error_callback(self, try_imports,
184 ['os.doesntexist', 'os.notthiseither'],
186 check_error_callback(self, try_imports,
187 ['os.doesntexist', 'os.notthiseither', 'os'],
189 check_error_callback(self, try_imports,
194 class TestStackHiding(TestCase):
196 run_tests_with = FullStackRunTest
199 super(TestStackHiding, self).setUp()
200 self.addCleanup(hide_testtools_stack, is_stack_hidden())
202 def test_is_stack_hidden_consistent_true(self):
203 hide_testtools_stack(True)
204 self.assertEqual(True, is_stack_hidden())
206 def test_is_stack_hidden_consistent_false(self):
207 hide_testtools_stack(False)
208 self.assertEqual(False, is_stack_hidden())
212 from unittest import TestLoader
213 return TestLoader().loadTestsFromName(__name__)