pytest assertions in Python
A test is a function whose name starts with test and whose body asserts.
def slugify(title):
return "-".join(title.lower().split())
def test_joins_words_with_hyphens():
assert slugify("Language Basics") == "language-basics"
def test_collapses_runs_of_space():
assert slugify(" Two Words ") == "two-words"
def test_leaves_a_single_word_alone():
assert slugify("Basics") == "basics"
How it works
- Plain
assertis enough: pytest rewrites it to show both sides. - One behavior per test, named for what it checks.
- No class, no setUp, no boilerplate.
Keywords and builtins used here
assertdefreturnslugifytest_collapses_runs_of_spacetest_joins_words_with_hyphenstest_leaves_a_single_word_alone
The run, in numbers
- Lines
- 14
- Characters to type
- 316
- Tokens
- 71
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 43 seconds.
Step 1 of 3 in Writing tests, step 1 of 9 in Testing with pytest.