Stack Overflow Asked by Roman Ponomaryov on December 26, 2020
What I’m aiming at is – if I ran the following test:
def test_func():
with pytest.raises(APIError):
func()
and the func() did not raise APIError – I want to get custom message to the output, e.g. "No APIError caught"
Pytest had a feature specifically for this:
with raises(SomeError, message="Custom message here"):
pass
https://docs.pytest.org/en/4.2.1/assert.html#assertions-about-expected-exceptions
but for some reason it didn’t make it to further versions (current is 6.0.2)
You can replicate this behavior using the pytest.fail
function after the function you expect to raise an exception, since that will only run if an exception is not raised. The deprecation notice explains the reasoning for its removal, and offers this alternate approach:
import pytest
def func():
return
def test_func():
with pytest.raises(ValueError):
func()
pytest.fail("Oh no!")
$ pytest test.py
collected 1 item
test.py F [100%]
==================================================================================================== FAILURES =====================================================================================================
____________________________________________________________________________________________________ test_func ____________________________________________________________________________________________________
def test_func():
with pytest.raises(ValueError):
func()
> pytest.fail("Oh no!")
E Failed: Oh no!
test.py:9: Failed
============================================================================================= short test summary info =============================================================================================
FAILED test.py::test_func - Failed: Oh no!
================================================================================================ 1 failed in 0.16s ================================================================================================
Correct answer by mattficke on December 26, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP