Python unittest - opposite of assertRaises?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
unittest does not provide a built-in assertion named something like assertNotRaises. In practice, you usually do not need one, because a test already fails automatically if an unexpected exception is raised.
The Simple Answer
If the code should run without errors, just call it and then assert on the result or side effects.
If parse_port("8080") raises any exception, the test fails automatically. That is already the opposite behavior of assertRaises.
Why There Is No Built-In assertNotRaises
An explicit "does not raise" assertion is less useful than assertRaises because the normal execution path already covers it. In most tests, you do not just want to prove that nothing crashed. You want to prove that the code returned the right value, updated the right state, or produced the right output.
That is why ordinary assertions are usually better than a special no-exception assertion.
If You Want a Clearer Failure Message
Sometimes you want a more explicit failure message, especially when testing several inputs. In that case, wrap the call in try and use self.fail().
This pattern is helpful when the default traceback is not descriptive enough for the test you are writing.
A Small Helper if You Really Want One
If your codebase strongly prefers a dedicated helper, you can define one in your base test class.
This is valid, but many teams still prefer the simpler style because it keeps tests closer to standard unittest.
subTest Works Well for Many Inputs
When you want to confirm that several inputs succeed without exceptions, subTest keeps the output readable.
If one case raises unexpectedly, unittest reports which input failed.
Focus on Behavior, Not Just Absence of Exceptions
A test that only proves "this did not crash" is often too weak. Prefer assertions that verify meaningful behavior:
- correct return value
- correct object state
- expected file or network side effect
- correct database change
No exception is usually just one part of a good test, not the full goal.
Common Pitfalls
- Looking for a built-in
assertNotRaiseswhen plain test code already gives the same failure behavior. - Writing tests that only check for lack of exceptions and never verify the result.
- Catching exceptions too broadly and hiding useful traceback information.
- Adding a custom helper everywhere when a direct function call would be clearer.
- Forgetting
subTestwhen checking several success cases in one test.
Summary
- There is no standard
unittestmethod that is the direct opposite ofassertRaises. - In normal tests, just call the function and let unexpected exceptions fail the test.
- Use
self.fail()insidetryandexceptonly when you want a custom failure message. - A custom
assertNotRaiseshelper is possible but usually optional. - The best tests verify correct behavior, not only the absence of exceptions.
Related reading
- Python update a key in dict if it doesn't exist
- Python UTC datetime object's ISO format doesn't include Z Zulu or Zero offset
- Python version 3.9 Calling class staticmethod within the class body?
- Python void return type annotation
- QUnit Async Tests with setup And teardown
- RabbitListener method testing in SpringBoot app
- Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings
- pytorch error multi-target not supported in CrossEntropyLoss
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.