question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Replace `unittest.TestCase.assertRaises` with `unittest.TestCase.assertRaisesRegex`

See original GitHub issue

Some 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

https://github.com/mlflow/mlflow/blob/fe6618823a2e6038149ee0da675503d2764552ca/tests/store/tracking/test_sqlalchemy_store.py#L107

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

Instructions

https://github.com/mlflow/mlflow/blob/101ad6e8eb383c769178df0df83d1d2a1cea6b4a/pylint_plugins/assert_raises_without_msg.py#L20-L33

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:closed
  • Created a year ago
  • Comments:18 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
harupycommented, May 23, 2022

Hi @Sumanth077, No. I should’ve informed you but #5914 replaced all the assertRaises calls and closed this issue.

0reactions
Sumanth077commented, May 23, 2022

Hi @harupy is this Issue still opened?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found