arrange, act, assert
A three-step shape for a test: set up the starting state, perform the one behaviour under test, then check the observable result.
In more detail
Keeping the three steps visible makes a test read like a small specification and makes a failure point at one rule. Avoid hiding the rule inside helpers that make the failure hard to read.
Examples
In pytest: build the input, call the function once, then assert on the result.In this course: Arrange builds only the data and objects the behaviour needs. Act is the single call under test, ideally with its result given a name. Assert compares that result with a value worked out by hand, which keeps failure reports short and makes the subject of the test easy to find.lines = [(250, 2), (125, 4)]
charged = with_vat(subtotal(lines))
assert charged == 1200Related terms
- unit test — An automated check of one small piece of code, such as a single function, that runs quickly and on its own.
- assertion — A check that a condition is true, which stops a test or program with a failure if it is false.
- assert statement — A statement, assert condition, that does nothing if the condition is true and raises AssertionError if it is false.
- pytest — A popular Python testing framework that finds test functions automatically and reports failures with detailed comparisons.
- given, when, then — A way of describing a test or requirement as a starting situation, an event, and the expected outcome.
- expected value — The answer a test claims the code should produce, written into the test before comparing it with the real result.
- code under test — The particular function, class or module a test is exercising and making claims about.
Taught in
- Unit Testing with xUnit and NSubstitute — Modern .NET Core, C# & Enterprise Microservices
- Testing Basics With JUnit 5 — Modern Java: Virtual Threads & High-Throughput Services
- Testing With pytest — Hands-on Python: Complete Video Course & Book
- Arrange, Act, Assert — Automated Testing, TDD & Quality Engineering