Checking Errors With assert.throws()

Snowline Hire’s tests now check the equipment returned for a valid booking. Kirsty’s suite also covers what happens when a booking asks for an ability the fitting rules don’t recognize. If "racer" slips through, the shop could recommend equipment using a rule it doesn’t have.

You need a test that passes when the fitting code refuses that booking. You’ll port Kirsty’s error check and make sure the JavaScript assertion gets a chance to catch the error. Start with the refusal the Python suite already expects.

Carry Over the Error Check

In Python, Kirsty uses pytest.raises() to check both the exception class and its message. Save this test in test_fitting.py beside the provided fitting.py:

Language: Python
import pytest
from fitting import FittingError, ski_length

def test_unknown_ability():
    with pytest.raises(FittingError, match="unknown ability: racer"):
        ski_length(170, "racer")

The context manager is already waiting for an exception when Python enters the with body. The test passes if the call raises FittingError with a matching message. Returning normally would fail the test, because refusing an unknown ability is part of Snowline Hire’s fitting rules.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.