I am writing test using pytest. I have a case where some function throws SystemExit with some error message on terminal in case of wrong input.
I want to write test for the case when SystemExit is thrown and verify that there is specific string in the output error message.
Here's the code:
def test_validate_input():
  ...
  with pytest.raises(SystemExit) as error:
    _validate_function(test_wrong_input)
  assert error.value.code == 1
I am not able to get the output error message in error that I get on command line if I run the actual function the validate some input. Please let me know what I am missing here.
Edit:
I was calling a subprocess.call_output to run a command which was throwing the error. I have to add stderr=subprocess.STDOUT in the call_output call as an argument to get the error message. Then I used @p3j4p5's answer in my test.
                        
Pytest's
raises()takes amatchargument. The docs tell us:and
So it should be suitable for your case:
This test will pass if the raised
SystemExitexception has been raised with a message matching the provided regular expression, and will fail otherwise.Alternatively, if you want to manually check the message:
In your case, assuming that
SystemExitis called with and integer (code) and a string (message) that would be: