BellaCode

by Geoff Cox

Software Craftsman. Guitarist. Folk Hero.

Structure your tests with the Arrange, Act, Assert pattern

The Arrange, Act, Assert (AAA) pattern separates your test into distinct steps using code comments.

Pattern

Comment each section of a test using implementation comments (//).

    // Arrange
    // Act
    // Assert

Example

Note: This test is using the When/Then pattern and the Expected, Target, Actual patterns as well.

    [TestMethod]
    public void WhenNameSetToDifferentValueThenPropertyChangedRaised()
    {
      // Arrange            
      var target = new Customer();
      target.Name = "New";
    
      bool actual = false;
      target.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
      {
        if (e.PropertyName == PropertyNames.Task.Name)
        {
          actual = true;
        }
      };
    
      // Act
      target.Name = "Name";
    
      // Assert            
      Assert.IsTrue(actual);
    }

Tips

Benefits