Replace `unittest.TestCase.assertRaises` with `unittest.TestCase.assertRaisesRegex`
See original GitHub issueSome tests use unittest.TestCase.assertRaises
to test an exception is raised for illegal operations, but they need to be replaces with unittest.TestCase.assertRaisesRegex
.
Why do we need this change?
Let’s say we have a function that raises an exception:
def throw_exception(...):
if condition_1:
raise TypeError("condition_1")
if condition_2:
raise TypeError("condition_2")
...
If we test this function using assertRaises
:
class MyTest(unittest.TestCase):
def test_throw_exception(self):
# Does `throw_exception` really raise the second TypeError?
# It might throw the first TypeError, then the test will pass.
with self.assertRaises(TypeError):
throw_exception(...) # should raise TypeError("condition_2")
If we test this function using assertRaisesRegex
:
class MyTest(unittest.TestCase):
def test_throw_exception(self):
# This test fails when `throw_exception` raises the first TypeError.
with self. assertRaisesRegex(TypeError, "condition_b"):
throw_exception(...) # should raise TypeError("condition_2")
Example
The code above needs to be fixed to the following:
# "<string that matches the error message>" must be replaced
with self.assertRaisesRegex(MlflowException, "<string that matches the error message>") as e:
References
- https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises
- https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaisesRegex
Instructions
Ping me with the file you want to work on 😃
File | Assignee | PR | Done |
---|---|---|---|
tests/entities/test_run_status.py |
@Sumanth077 | ||
tests/store/model_registry/test_sqlalchemy_store.py |
@ognis1205 | #5875 | ✅ |
tests/store/db/test_utils.py |
@erich-db | ||
tests/store/tracking/__init__.py |
@Sumanth077 | ||
tests/store/tracking/test_file_store.py |
@andy1122 | ||
tests/store/tracking/test_sqlalchemy_store.py |
@ognis1205 | #5875 | ✅ |
Issue Analytics
- State:
- Created a year ago
- Comments:18 (14 by maintainers)
Top Results From Across the Web
unittest — Unit testing framework — Python 3.11.1 ...
It checks for a specific response to a particular set of inputs. unittest provides a base class, TestCase , which may be used...
Read more >How do you test that a Python function throws an exception?
Use TestCase.assertRaises (or TestCase.failUnlessRaises ) from the unittest module, for example: import mymod class MyTestCase(unittest.
Read more >T154281 [recurring] Replace assertRaises with ...
I think the page edit fails because the edits involved in the test do not show up on the testwiki end.
Read more >Pywikibot: Replace assertRaises with assertRaisesRegex
Python unittest framework provides two methods for checking that an operation raises an expected exception: assertRaises , which is not a good assertion, ......
Read more >Replace assertRaisesRegexp with assertRaisesRegex
This replaces the deprecated (in python 3.2) unittest.TestCase method assertRaisesRegexp() with assertRaisesRegex(). Change-Id: ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @Sumanth077, No. I should’ve informed you but #5914 replaced all the
assertRaises
calls and closed this issue.Hi @harupy is this Issue still opened?