Fundamentals of Unit Testing: Understand AAA in Unit Testing

This is the fundamentals of unit testing article series. In our previous article we saw how to do unit testing in the Visual Studio environment. You can read them here.

In this short article we will understand the “AAA” concept of unit testing. This is a very important and fundamental idea of unit testing. In the overall view of any unit test application, we will see that a unit test is a three-step processes. The first one is setup test or arrange test, Act test or execute unit test and Assert means verify test result with expected results. The concept of three "A"s are given below.

Arrange: This is the first step of a unit test application. Here we will arrange the test, in other words we will do the necessary setup of the test. For example, to perform the test we need to create an object of the targeted class, if necessary, then we need to create mock objects and other variable initialization, something like this.

Act: This is the middle step of a unit step application. In this step we will execute the test. In other words we will do the actual unit testing and the result will be obtained from the test application. Basically we will call the targeted function in this step using the object that we created in the previous step.

Assert: This is the last step of a unit test application. In this step we will check and verify the returned result with expected results.

Ok, fine we have understood the concept, so let's implement the three steps practically. So, create a class library application with the following code.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace TestProjectLibrary

{

    public class testClass

    {

        public Boolean testFunction()

        {

            return true;

        }

    }

}

This is the code that we will test. The code is very simple, the class contains only one function and it will always return true, since we are not interested in writing the business logic at all.

Now, add one unit test application to the solution and the following is the code to do the unit testing of the code above. If you are very new to unit testing then I will suggest you to go through our first article to understand unit test setup by a Visual Studio unit test application. Have a look at the following code.

using System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using TestProjectLibrary;

//using Moq;

namespace UnitTest

{

    [TestClass]   

    public class UnitTest

    {

        [TestMethod]

        public void TestMethod()

        {

           //Arrange test

            testClass objtest = new testClass();

            Boolean result;

 

           //Act test

            result = objtest.testFunction();

 

           //Assert test

            Assert.AreEqual(true, result);

 

        }

    }

}

Here we are clearly seeing three parts of unit test, at first we are creating an object of the testClass and then we are declaring one Boolean variable that will store the returned result and in the next level we are checking whether or not the return value is equal to the expected value.

Let's run the test and check the result.

result

Conclusion

Oh, we are seeing that it has successfully executed and the test passed. I hope you have understood the concept of AAA in unit test applications.

Up Next
    Ebook Download
    View all
    Learn
    View all