Reputation: 494
I want to assert in pytest that if my call fails that sys.exit is called.
import requests
import sys
def httpRequest(uri):
try:
response = requests.get(uri,verify=False)
except requests.ConnectionError:
sys.exit(1)
return response
uri = 'http://reallybaduri.com/test'
response = httpRequest(uri)
print(response.text)
Testing Code:
import api_call
import requests
import pytest
def test_httpRequest():
uri = 'http://reallybaduri.com/test'
with pytest.raises(SystemExit) as pytest_wrapped_e:
api_call.httpRequest(uri)
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 42
This gives me a "did not raise" error. How Do I assert that an error is raised?
Upvotes: 2
Views: 2369
Reputation: 2159
The test will automatically fail if no exception / exception other than SystemExit
is raised.
The code only needs to be
import api_call
import requests
import pytest
def test_httpRequest():
uri = 'http://reallybaduri.com/test'
with pytest.raises(SystemExit):
api_call.httpRequest(uri)
Upvotes: 0
Reputation: 1278
You should change your code assert pytest_wrapped_e.value.code == 1
. You are expecting 1 not 42
Upvotes: 1