What is the best way to append custom message to the output when pytest.raises fails?

What I’m aiming at is – if I ran the following test:

def test_func():
    with pytest.raises(APIError):

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"):
but for some reason it didn’t make it to further versions (current is 6.0.2)

Stack Overflow Asked by Roman Ponomaryov on December 26, 2020

1 Answers

One Answer

You can replicate this behavior using the 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():

def test_func():
    with pytest.raises(ValueError):
        func()"Oh no!")
$ pytest

collected 1 item F                                                                                                                                                                                                   [100%]

==================================================================================================== FAILURES =====================================================================================================
____________________________________________________________________________________________________ test_func ____________________________________________________________________________________________________

    def test_func():
        with pytest.raises(ValueError):
> "Oh no!")
E           Failed: Oh no! Failed
============================================================================================= short test summary info =============================================================================================
FAILED - Failed: Oh no!
================================================================================================ 1 failed in 0.16s ================================================================================================

Correct answer by mattficke on December 26, 2020

Add your own answers!

Related Questions

Java writing unlimited text lines to a UI

0  Asked on February 11, 2021 by barry-griffey


R simple dplyr solution to filter

3  Asked on February 11, 2021 by triss


Go back browser button action

2  Asked on February 11, 2021 by ljopata


How to pass array to funtion to use as range based for loop

3  Asked on February 10, 2021 by aniket-ujgare


react ref and query selector all

2  Asked on February 10, 2021 by peter-flanagan


Direct code execution through the JS browser console

0  Asked on February 10, 2021 by matheus-nascimento


How to correctly write word-break in css?

3  Asked on February 10, 2021 by aiko-schurmann


Finding a user data based on email

1  Asked on February 10, 2021 by aamer-salame


Can I use text when using functions? Or only numbers?

4  Asked on February 9, 2021 by osk6r


Get the string before replace

0  Asked on February 9, 2021 by tanker


Ask a Question

Get help from others!

© 2022 All rights reserved.