8000 Refactor assertRaises · pythonnet/pythonnet@1320a73 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1320a73

Browse files
committed
Refactor assertRaises
test_exceptions: Add exc_info test & fix Exception nameclash
1 parent 5af6fcb commit 1320a73

16 files changed

+443
-1168
lines changed

src/tests/test_array.py

Lines changed: 81 additions & 243 deletions
Large diffs are not rendered by default.

src/tests/test_class.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,12 @@ def test_class_default_repr(self):
5959

6060
def test_non_public_class(self):
6161
"""Test that non-public classes are inaccessible."""
62-
63-
def test():
62+
with self.assertRaises(ImportError):
6463
from Python.Test import InternalClass
6564

66-
self.assertRaises(ImportError, test)
67-
68-
def test():
65+
with self.assertRaises(AttributeError):
6966
x = Test.InternalClass
7067

71-
self.assertRaises(AttributeError, test)
72-
7368
def test_basic_subclass(self):
7469
"""Test basic subclass of a managed class."""
7570
from System.Collections import Hashtable
@@ -256,13 +251,17 @@ def test_comparisons(self):
256251
self.assertEqual(d2 >= d1, True)
257252
self.assertEqual(d2 > d1, True)
258253

259-
self.assertRaises(TypeError, lambda: d1 < None)
260-
self.assertRaises(TypeError, lambda: d1 < System.Guid())
254+
with self.assertRaises(TypeError):
255+
d1 < None
256+
257+
with self.assertRaises(TypeError):
258+
d1 < System.Guid()
261259

262260
# ClassTest does not implement IComparable
263261
c1 = ClassTest()
264262
c2 = ClassTest()
265-
self.assertRaises(TypeError, lambda: c1 < c2)
263+
with self.assertRaises(TypeError):
264+
c1 < c2
266265

267266
def test_self_callback(self):
268267
"""Test calling back and forth between this and a c# baseclass."""

src/tests/test_compat.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,13 @@ def test_implicit_load_already_valid_namespace(self):
182182
self.assertTrue(isCLRClass(CLR.System.UriBuilder))
183183

184184
def test_import_non_existant_module(self):
185-
"""Test import failure for a non-existant module."""
186-
187-
def test():
185+
"""Test import failure for a non-existent module."""
186+
with self.assertRaises(ImportError):
188187
import System.SpamSpamSpam
189188

190-
def testclr():
189+
with self.assertRaises(ImportError):
191190
import CLR.System.SpamSpamSpam
192191

193-
self.assertRaises(ImportError, test)
194-
self.assertRaises(ImportError, testclr)
195-
196192
def test_lookup_no_namespace_type(self):
197193
"""Test lookup of types without a qualified namespace."""
198194
import CLR.Python.Test
@@ -201,18 +197,13 @@ def test_lookup_no_namespace_type(self):
201197

202198
def test_module_lookup_recursion(self):
203199
"""Test for recursive lookup handling."""
204-
205-
def test1():
200+
with self.assertRaises(ImportError):
206201
from CLR import CLR
207202

208-
self.assertRaises(ImportError, test1)
209-
210-
def test2():
203+
with self.assertRaises(AttributeError):
211204
import CLR
212205
x = CLR.CLR
213206

214-
self.assertRaises(AttributeError, test2)
215-
216207
def test_module_get_attr(self):
217208
"""Test module getattr behavior."""
218209
import CLR.System as System
@@ -223,20 +214,16 @@ def test_module_get_attr(self):
223214
module = System.Xml
224215
self.assertTrue(isCLRModule(module))
225216

226-
def test():
217+
with self.assertRaises(AttributeError):
227218
spam = System.Spam
228219

229-
self.assertRaises(AttributeError, test)
230-
231-
def test():
220+
with self.assertRaises(TypeError):
232221
spam = getattr(System, 1)
233222

234-
self.assertRaises(TypeError, test)
235-
236-
def test000_multiple_imports(self):
223+
def test_multiple_imports(self):
237224
# import CLR did raise a Seg Fault once
238225
# test if the Exceptions.warn() method still causes it
239-
for n in range(100):
226+
for _ in range(100):
240227
import CLR
241228

242229

0 commit comments

Comments
 (0)
0