typestar

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

  1. Plain assert is enough: pytest rewrites it to show both sides.
  2. One behavior per test, named for what it checks.
  3. No class, no setUp, no boilerplate.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Writing tests, step 1 of 9 in Testing with pytest.

Next →