Tests should be repeatable and deterministic


Python clean test tip:

Your tests should be repeatable in any environment.

They should be deterministic, always result in the same tests succeeding.

Example:

import random

LOTTO_COMBINATION_LENGTH = 5
MIN_LOTTO_NUMBER = 1
MAX_LOTTO_NUMBER = 42


def lotto_combination():
    combination = []
    while len(combination) < LOTTO_COMBINATION_LENGTH:
        number = random.randint(MIN_LOTTO_NUMBER, MAX_LOTTO_NUMBER)
        if number not in combination:
            combination.append(number)

    return combination


# BAD
def test_lotto_combination():
    assert lotto_combination() == [10, 33, 5, 7, 2]


# GOOD
def test_all_numbers_are_between_min_max_range():
    assert all(MIN_LOTTO_NUMBER <= number <= MAX_LOTTO_NUMBER for number in lotto_combination())


def test_length_of_lotto_combination_has_expected_number_of_elements():
    assert len(lotto_combination()) == LOTTO_COMBINATION_LENGTH