Running One Test Over Many Cases

Snowline Hire’s tests now check fitting results and rejected inputs. Kirsty’s ski-length test has another job: checking the same rule for several heights and abilities. Copying the assertion into five functions would make the port harder to maintain when the shop changes its rules.

You can keep the cases together and still get a separate result for each one. You’ll port Kirsty’s parametrized test, group its results under a heading, and select the cases you need while working on a fitting rule. Start with the five inputs her Python test already covers.

Give Each Case a Test

In Python, @pytest.mark.parametrize supplies each row to the same test function and reports a separate result for each case. Kirsty’s test keeps the inputs and expected lengths together:

Language: Python
import pytest
from fitting import ski_length

@pytest.mark.parametrize(
    "height, ability, expected",
    [
        (168, "intermediate", 155),
        (141, "beginner", 125),
        (183, "expert", 175),
        (110, "beginner", 120),
        (210, "expert", 190),
    ],
)
def test_ski_length(height, ability, expected):
    assert ski_length(height, ability) == expected

With pytest -v, each row appears under a name such as test_ski_length[141-beginner-125]. The height, ability, and expected length tell you which input needs attention if a check fails.

JavaScript’s node:test has no parametrization decorator. Because test is an ordinary function, a loop can call it once for every row. A suite groups related tests. To register the tests in a suite, describe() runs the function you pass to it.

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.