Structure Your Tests With Arrange, Act, Assert

Introduction To Pytest

Learn how to the Arrange, Act and Assert pattern when structuring your tests.

  • Arrange, setup any variables or conditions your test needs.
  • Act, execute the code you want to test.
  • Assert, Check that the code behaviours in a way that you would expect.


[0:00] When it comes to structuring your tests one pattern to keep in mind is Arrange, Act and Assert.

[0:07] You typically start out by "arranging" or setting up the test. In this case let's set up a person called Bob.

[0:14] Then we're going to "act" and call the code we want to test. So let's greet Bob.

[0:21] And finally we're going to "assert" that the code we called acted in a way we would expect. In this case that Bob is being politely greeted.

[0:32] Let's run our test...and it passes. Most tests follow this pattern. We "arrange" or we setup any variables, conditions we might need. We then "act" and execute the code we are interested in testing. And finally we "assert" or check that the code behavioured in way we would expect.