jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/331141 )
Change subject: Replace assertRaises with asserRaisesRegex in tools_tests.py
......................................................................
Replace assertRaises with asserRaisesRegex in tools_tests.py
assertRaises is not as good of a test as asserRaisesRegex. The latter has an extra
parameter to match the exception message, allowing more more precision when checking an
error.
Bug: T154281
Change-Id: I99f62d6eade8d0878858bab749022eb4e12cb8b3
---
M tests/tools_tests.py
1 file changed, 45 insertions(+), 16 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/tools_tests.py b/tests/tools_tests.py
index a3623ab..6e97772 100644
--- a/tests/tools_tests.py
+++ b/tests/tools_tests.py
@@ -74,7 +74,8 @@
"""Check that the wrapper permits exceptions."""
wrapper = tools.ContextManagerWrapper(self.DummyClass())
self.assertFalse(wrapper.closed)
- with self.assertRaises(ZeroDivisionError):
+ with self.assertRaisesRegex(ZeroDivisionError,
+ '(integer division or modulo by zero|division by
zero)'):
with wrapper:
1 / 0
self.assertTrue(wrapper.closed)
@@ -133,9 +134,14 @@
def test_open_archive_without_bz2(self):
"""Test open_archive when bz2 and bz2file are not
available."""
old_bz2 = tools.bz2
+ BZ2_IMPORT_ERROR = ('This is a fake exception message that is '
+ 'used when bz2 and bz2file is not importable')
try:
- tools.bz2 = ImportError()
- self.assertRaises(ImportError, self._get_content, self.base_file +
'.bz2')
+ tools.bz2 = ImportError(BZ2_IMPORT_ERROR)
+ self.assertRaisesRegex(ImportError,
+ BZ2_IMPORT_ERROR,
+ self._get_content,
+ self.base_file + '.bz2')
finally:
tools.bz2 = old_bz2
@@ -145,13 +151,17 @@
def test_open_archive_7z(self):
"""Test open_archive with 7za if installed."""
+ FAILED_TO_OPEN_7ZA = 'Unexpected STDERR output from 7za '
try:
subprocess.Popen(['7za'], stdout=subprocess.PIPE).stdout.close()
except OSError:
raise unittest.SkipTest('7za not installed')
self.assertEqual(self._get_content(self.base_file + '.7z'),
self.original_content)
- self.assertRaises(OSError, self._get_content, self.base_file +
'_invalid.7z',
- use_extension=True)
+ self.assertRaisesRegex(OSError,
+ FAILED_TO_OPEN_7ZA,
+ self._get_content,
+ self.base_file + '_invalid.7z',
+ use_extension=True)
class OpenCompressedTestCase(OpenArchiveTestCase, DeprecationTestCase):
@@ -202,14 +212,26 @@
def test_invalid_modes(self):
"""Test various invalid mode configurations."""
- self.assertRaises(ValueError, tools.open_archive,
- '/dev/null', 'ra') # two modes besides
- self.assertRaises(ValueError, tools.open_archive,
- '/dev/null', 'rt') # text mode
- self.assertRaises(ValueError, tools.open_archive,
- '/dev/null', 'br') # binary at front
- self.assertRaises(ValueError, tools.open_archive,
- '/dev/null', 'wb', False) # writing without
extension
+ INVALID_MODE_RA = 'Invalid mode: "ra"'
+ INVALID_MODE_RT = 'Invalid mode: "rt"'
+ INVALID_MODE_BR = 'Invalid mode: "br"'
+ MN_DETECTION_ONLY = 'Magic number detection only when reading'
+ self.assertRaisesRegex(ValueError,
+ INVALID_MODE_RA,
+ tools.open_archive,
+ '/dev/null', 'ra') # two modes besides
+ self.assertRaisesRegex(ValueError,
+ INVALID_MODE_RT,
+ tools.open_archive,
+ '/dev/null', 'rt') # text mode
+ self.assertRaisesRegex(ValueError,
+ INVALID_MODE_BR,
+ tools.open_archive,
+ '/dev/null', 'br') # binary at front
+ self.assertRaisesRegex(ValueError,
+ MN_DETECTION_ONLY,
+ tools.open_archive,
+ '/dev/null', 'wb', False) # writing
without extension
def test_binary_mode(self):
"""Test that it uses binary mode."""
@@ -230,8 +252,12 @@
def test_write_archive_7z(self):
"""Test writing an archive as a 7z archive."""
- self.assertRaises(NotImplementedError, tools.open_archive,
- '/dev/null.7z', mode='wb')
+ FAILED_TO_WRITE_7Z = 'It is not possible to write a 7z file.'
+ self.assertRaisesRegex(NotImplementedError,
+ FAILED_TO_WRITE_7Z,
+ tools.open_archive,
+ '/dev/null.7z',
+ mode='wb')
class MergeUniqueDicts(TestCase):
@@ -347,7 +373,10 @@
def test_accept_only_keyword_marker(self):
"""Test that the only kwargs accepted is
'marker'."""
- self.assertRaises(TypeError, tools.islice_with_ellipsis(self.it, 1,
t=''))
+ GENERATOR_NOT_CALLABLE = "'generator' object is not callable"
+ self.assertRaisesRegex(TypeError,
+ GENERATOR_NOT_CALLABLE,
+ tools.islice_with_ellipsis(self.it, 1, t=''))
def passthrough(x):
--
To view, visit
https://gerrit.wikimedia.org/r/331141
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I99f62d6eade8d0878858bab749022eb4e12cb8b3
Gerrit-PatchSet: 8
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: SacredWKnight <yudhis.sumardiono(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: SacredWKnight <yudhis.sumardiono(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>