2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-05-25 14:18:12 +02:00
Using ``++pytest.raises++`` or ``++unittest.TestCase.assertRaises++`` will assert that an exception is raised in the following block. Ending such block in an assertion means that the test can succeed with that last assertion never being executed.
2020-12-21 15:38:52 +01:00
2023-05-25 14:18:12 +02:00
=== Noncompliant code example
[source,python]
----
import pytest
def foo(): return 1 / 0
def bar(): return 42
def test_something():
with pytest.raises(ZeroDivisionError):
foo()
assert bar() == 42 # Noncompliant
----
=== Compliant solution
[source,python]
----
import pytest
def foo(): return 1 / 0
def bar(): return 42
def test_something():
with pytest.raises(ZeroDivisionError):
foo()
assert bar() == 42
----
2020-12-21 15:38:52 +01:00
2023-05-03 11:06:20 +02:00
== Resources
2020-12-21 15:38:52 +01:00
* https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises[Unittest: assertRaises]
* https://docs.pytest.org/en/stable/assert.html#assertions-about-expected-exceptions[Pytest: assertions about expected exceptions]
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
endif::env-github,rspecator-view[]