01/28/2023 22:19 | Category: testing

Tags: mockspattern

testing pattern to follow inside unit tests

This is a pattern that I use quite heavily when writing my tests. I find this helps to organize each unit test's setup, execution, and assertions.

I remember reading/hearing this from somewhere, but that reference may be lost to time.

Tests follow the following flow

  1. Arrange
  2. Mock
  3. Act
  4. Assert

Example test


def my_func(var_one: int, var_two: int) -> int:
    return var_one + var_two


def test_my_func() -> None:

    # Arrange
    var_one = 1
    var_two = 1
    expected = 2

    # Mock
    # No behavior mocking required

    # Act
    result = my_func(var_one, var_two)

    # Assert
    assert result == expected