Skip to content

Assertions

stepline's assertions read like a sentence and tell you exactly what went wrong. You start every check with assert_that, pass the value you want to check, and chain the checks that must hold for it:

from stepline import assert_that


def test_create_user() -> None:
    user = create_user(name="Alice")

    assert_that(user.id, description="user id").is_not_none().is_instance_of(int)
    assert_that(user.name).is_equal_to("Alice")

The assertions are plain Python and do not need pytest. In a pytest run, a failed check is a normal test failure.

The entry point

assert_that(actual, description=None) takes the value to check and returns an assertion object for it. Which object you get depends on the type of the value, so your editor only offers checks that make sense for it:

Value Assertion class
bool BooleanAssert
int, float, Decimal, Fraction NumberAssert
str StringAssert
bytes, bytearray BytesAssert
Mapping, for example dict MappingAssert
Collection, for example list or set CollectionAssert
Path PathAssert
date and datetime DateAssert
BaseException ExceptionAssert
functions, classes and other callables CallableAssert
everything else, for example None ObjectAssert

Subclasses get the class of their base type: an OrderedDict gets MappingAssert. A bool gets BooleanAssert and not NumberAssert, and a str gets StringAssert and not CollectionAssert, even though Python treats them as a number and a collection.

Every assertion class also has the generic checks, such as is_equal_to() and is_none(), which work for any value.

Describing the value

The optional keyword argument description names the value. It appears at the start of the failure message, so you can tell at a glance which of several checks failed:

assert_that(response.status_code, description="status code").is_equal_to(200)

Writing styles

Every check returns the assertion itself. You can chain checks, write one check per line or keep the assertion in a variable. All styles are equally valid, and all of them stop at the first failed check:

assert_that(user.id, description="user id").is_not_none().is_instance_of(int)
assert_that(user.id).is_not_none()
assert_that(user.id).is_instance_of(int)
user_id = assert_that(user.id, description="user id")
user_id.is_not_none()
user_id.is_instance_of(int)

For common combinations, the type-specific assertion classes will offer dedicated methods, such as a range check for numbers instead of two comparisons.

Soft assertions

Inside a with soft_assertions(): block, failed checks will not stop the test right away. stepline will collect them and report them together at the end of the block. Soft assertions are planned and not available yet.

When a check fails

A failed check raises AssertionFailedError, a subclass of AssertionError. Its message starts with a headline that describes the failed check. If the check has an expected value, a tree below the headline compares it with the value that was found:

status code: expected 404 to be equal to 200
├─ expected  200
╰─ actual    404

Values are shown with repr, so '1', 1 and None stay distinguishable. Long values are shortened: strongly in the headline, which pytest also shows in its short test summary, and in the tree only if they are very large.

In a pytest run, stepline hides its own frames from the traceback. The failure points directly at the line of your test:

    def test_status() -> None:
>       assert_that(404, description="status code").is_equal_to(200)
E       stepline.errors.AssertionFailedError: status code: expected 404 to be equal to 200
E       ├─ expected  200
E       ╰─ actual    404

If you need to catch the error yourself, import it from stepline.errors:

import pytest

from stepline import assert_that
from stepline.errors import AssertionFailedError


def test_rejects_wrong_status() -> None:
    with pytest.raises(AssertionFailedError):
        assert_that(404).is_equal_to(200)