unit-testingtestingarrange-act-assert

What's the difference between Arrange and Act in the Arrange, Act, Assert pattern?


I don't really understand the difference between the arrange and act part of the pattern in unit tests. Does Arrange ONLY mean the creation of the objects? Why should we separate the Arrange from the Act part in the first place, and what's the criteria to decide if something belongs to the Act and not to the Arrange part? To me it seems that everything belongs to the Arrange part, since we "arrange the test" for the assertion, don't we?


Solution

  • A unit test tests a single "Act" in a program, typically a single method call on an object instance. Arrange, Act, Assert organizes a unit test into three parts: before, during and after the Act.

    So we don't "arrange the test for the assertion", we Arrange the world for the Act. In the Arrange part, we do things whose effects we already know. Every method called in the Arrange part should be unit-tested elsewhere. In the Act, we do something whose effect we don't know yet; this is what the test is actually about. (In test-driven development we might not have written the method yet, or added to its implementation to pass this particular test.)