Home » TDD » NUnit Test Code Structure

NUnit Test Code Structure

UnitTestStructure

There are two basic ways to structure the code you will be working on to Unit test the code in your main application using NUnit.  Arrange/Act/Assert (AAA) is what I refer to as the “Classic” method.  It has been around for as long as I’ve known about unit testing. The second method is the Given/When/Then (GWT) structure.

While both methods get the job done and both methods essentially help you structure your test, I think that the Given/When/Then structure helps us think about what we are trying to test from a more functional perspective.  Arrange/Act/Assert is more of a code based approach.

Since, in the end, what we care about is the functionality that we have coded.  I prefer Given/When/Then.  I also think GWT is better suited to Test Driven Development since in Test Driven Development, we are already in the mindset of “What is it that I’m trying to accomplish?”  And not, “What did I do and how do I test it?”

Arrange/Act/Assert

The classic method of arranging our NUnit test code conforms to the “Arrange/Act/Assert” pattern.  Using this pattern, we setup our code so that it is in the state we need it to be to perform the test.  Then we perform some distinct action on the code.  This action should exercise one and only one feature.  And then, finally we assert that the action we performed resulted in some expectation being fulfilled.

What?  Huh?

So, maybe some code will help.

Let’s assume that we have a class, Calculator, that has a method, Add:

public class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

To test this class, we create another class in our test project that we will name CalculatorTest.  Well, what do we want to test in our Calculator class?  We want to test that when we add two numbers together, the proper sum comes back.  So, we’ll need a test method.  It is customary to name our methods what we are trying to test for, so we will call our test method AddShouldReturnSumOfTwoNumbers().

Following the Arrange/Act/Assert pattern, the first part of our method will create the Calculator object, then we will call the Add method, passing it two numbers, and then we will Assert that the proper result was returned.

[TestFixture]
public class CalculatorTest
{
    [Test]
    public void AddShouldReturnSumOfTwoNumbers()
    {
        // Arrange
        var sut = new Calculator();

        // Act
        var returnValue = sut.Add(3, 7);

        // Assert
        Assert.That(returnValue, Is.EqualTo(10));
    }
}

Given/When/Then

An alternate way of thinking about test is by using the Given/When/Then construct.  It essentially arranges the code in the same way, but I think it helps you think about what it is you are trying to test better.  Arrange/Act/Assert (aka AAA) tends to have you think about your code from a programmer’s perspective.  “This is what my code does.”  The Give/When/Then method thinks about the problem more from a requirements perspective.  At the end of the day what is important is that you have test around your code, so I’m not going to spend a lot of time arguing that one way is particularly better than the other.  But GWT is what I use. Since this is my post, that’s what we are going to use for the remainder of this book.

So going back to our Calculator example, what would GWT look like?

[TestFixture]
public class CalculatorTest
{
    [Test]
    public void AddShouldReturnSumOfTwoNumbers()
    {
        // Given that we have a calculator
        var sut = new Calculator();

        // When I add 3 and 7 together
        var returnValue = sut.Add(3, 7);

        // Then the result should be 10
        Assert.That(returnValue, Is.EqualTo(10));
    }
}

See, it’s all the same stuff, so whatever helps you think about what you are testing is what you should use.

 

Other post in TDD

About Dave Bush

Dave Bush is a "Full Stack" ASP.NET developer currently specializing in Angular but also capable of using ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.

Leave a Reply

Be the First to Comment!

avatar
  Subscribe  
Notify of