Writing tests
A test is a function that sets something up, calls your code, and asserts on the result. pytest finds functions named test_* and runs them. When an assert fails, it shows the values involved.
def add(a, b):
return a + b
def test_add_negative():
assert add(2, -3) == -1What to test
- One behaviour per test, named after that behaviour.
- Boundaries: empty input, one item, the largest allowed value.
- Each bug you fix, so it stays fixed.
Testing for errors
import pytest
def test_bad_input():
with pytest.raises(ValueError):
int("nope")A good test fails when the code is wrong. A test that passes no matter what gives false comfort, so break the code on purpose once and watch the test catch it.
Your task
slugify is written for you. Write test_lowercases, test_spaces_become_dashes and test_empty_string, each using a plain assert on a slugify call. Each test must fail if that behaviour breaks.
The explanation opens here once every check passes.