0

I want to find a way to test that my code return the correct error message for a given error with pytest

My code works on a dictionary and in some cases, some keys could be undefined. In the example below, the tag variable is provided by the user, and in some cases, that key can be undefined. I then added a try/except statement to catch this exception and return an error message to the user, explaining the issue.

try:
    species = self.species[tag]
except KeyError:
    print(f"Error: the species {tag} is not defined")

My issue arise when trying to write a unit test for this code. How could I check that my code return the appropriate error message ? I could write a test who provide willingly a wrong tag and then check for stdout, but then I would have to rewrite this test if I decided to change the error message. And it seems not very appropriate in the "Don't Repeat Yourself" approach.

This : How to properly assert that an exception gets raised in pytest? does not answer my question, as I don't raise an exception.

Maybe I'm not even handling correctly the error in the first place and there is a better way to do it with Python.

Rowin
  • 435
  • 2
  • 9
  • properly answered here [https://stackoverflow.com/questions/23337471/how-to-properly-assert-that-an-exception-gets-raised-in-pytest](https://stackoverflow.com/questions/23337471/how-to-properly-assert-that-an-exception-gets-raised-in-pytest) – probhonjon Oct 04 '19 at 08:43
  • 1
    Possible duplicate of [How to properly assert that an exception gets raised in pytest?](https://stackoverflow.com/questions/23337471/how-to-properly-assert-that-an-exception-gets-raised-in-pytest) – probhonjon Oct 04 '19 at 08:44
  • This will not work as the code catch the exception and returns an error message instead. I want to check if the code return the proper error message. – Rowin Oct 04 '19 at 08:50
  • https://stackoverflow.com/a/56300627/2650249 – hoefling Oct 04 '19 at 09:26

1 Answers1

1

I believe you should change you code structure to achieve what you wish.

class Species:

    def myMethod(tag):
        try:
            species = self.species[tag]
        except KeyError:
            raise YourCustomError(f"Error: the species {tag} is not defined")

Then you can write your pytest function to check that YourCustomError is raised when using myMethod with a bad tag. Note that instead of YourCustomError, you can do : raise KeyError("custom message")

cylon86
  • 550
  • 4
  • 20
  • Yes, I was thinking of using my own exceptions. Maybe it's the better way to handle this issue. – Rowin Oct 04 '19 at 09:33
  • I tried the ```raise KeyError("custom message")``` you suggested, but I got a quite ugly stack trace, with a first KeyError, then a message saying ```During handling of the above exception, another exception occured``` and then another trace for a second KeyError with my custom message – Rowin Oct 04 '19 at 09:38
  • Yes the stack trace is not really pretty, but after all it is quite precise. I don't know if this can be changed.. – cylon86 Oct 04 '19 at 11:54