Sobes.tech
Junior

Tell me about the Triple A (Arrange, Act, Assert) principle.

sobes.tech AI

Answer from AI

The Triple A principle (Arrange, Act, Assert) is a pattern for organizing test code, widely used in unit testing and integration testing. It divides each test into three logical parts:

  1. Arrange: This part sets up the test environment, creates necessary objects, mocks, stubs, and establishes their initial state. It prepares everything needed to perform the action we want to test.
  2. Act: This is the single operation we are testing in this test. It involves calling the method, function, or component under test.
  3. Assert: In this part, the result of the action is checked. The actual result (return value, change in object state, call to another method, etc.) is compared with the expected result. If they match, the test is considered successful.

Example in Python:

# arrange
user = User("Alice")
item = Item("Book", 10)
cart = ShoppingCart(user)

# act
cart.add_item(item)

# assert
assert len(cart.items) == 1
assert cart.items[0].name == "Book"
assert cart.calculate_total() == 10

This principle makes tests more readable, understandable, and easier to maintain, as it clearly shows what is being tested and under what conditions.