I have a pretty standard function for my game call_quit() that accepts user input to confirm the quit, then either continues via a generic prompt() or calls exit() to kill the program.
Testing the strings associated with each if statement is easy, but how do you test that exit() has been called?
I’ve been reading this article which states a base exception of SystemExit will be thrown, and I am seeing this in the Pytest output error, but I cannot hook into it for the assertion.
Has anyone found a reliable way to do this?
def call_quit():
"""Enables the player to confirm they want to quit the game."""
confirm = input("Are you sure? (y/n) ")
if confirm == 'n':
print("Game on...")
prompt()
elif confirm == 'y':
print("See you soon...")
exit()
else:
print("I don't understand that...")
prompt()
import pytest
import mock
def test_call_quit_ends_the_game():
"""Tests call_quit and confirming ends the session."""
with mock.patch('builtins.input', return_value='y'):
pytest_wrapped_exit = pytest.raises(SystemExit)
assert src.util.call_quit() == pytest_wrapped_exit.type