Tests should validate themselves regardless of whether the test execution passes or fails


Python clean test tip:

A test should validate itself whether the test execution is passed or failed.

The self-validating test can avoid the need to do an evaluation manually by us.

Example:

from dataclasses import dataclass


@dataclass
class User:
    first_name: str
    last_name: str

    def fullname(self):
        return f"{self.first_name} {self.last_name}"


# BAD
def test_full_name_consists_of_first_name_and_last_name_manual():
    first_name = "John"
    last_name = "Doe"
    user = User(first_name=first_name, last_name=last_name)

    print(user.fullname())
    assert input("Is result correct? (Y/n)") == "Y"


# GOOD
def test_full_name_consists_of_first_name_and_last_name():
    first_name = "John"
    last_name = "Doe"
    full_name = "John Doe"
    user = User(first_name=first_name, last_name=last_name)

    assert user.fullname() == full_name