Getting started with xUnit

In this section, we are going to create a new project in our backend Visual Studio solution and start to implement simple unit tests to get comfortable with xUnit, which is the tool we are going to use to run our backend tests. So, let's open our backend ASP.NET Core project and carry out the following steps:

  1. Open up the Solution Explorer, right-click on Solutionchoose Addand then choose New Project....
  2. Select xUnit Test Project (.NET Core) from the dialog box that opens and click on the Next button:

  1. Enter BackendTests as the project name and set Location to the folder that the solution is in. Click Create to create the project.
  2. We are going to create a simple class so that we can write some unit tests for it. This will get you comfortable with xUnit. Create a static class in our unit test project called Calc with the following content:
using System;

namespace BackendTests
{
public static class Calc
{
public static decimal Add(decimal a, decimal b)
{
return a + b;
}
}
}

The class contains a method called Addwhich simply adds two numbers together that are passed in its parameters. Add is a pure function, which means the return value is always consistent for a given set of parameters and it doesn't give any side-effects. Pure functions are super-easy to test, as we'll see next.

  1. We are going to create some unit tests for the Add method in the Calc class. Let's create a new class in the unit test project called CalcTests with the following content:
using Xunit;

namespace BackendTests
{
public class CalcTests
{
[Fact]
public void Add_When2Integers_ShouldReturnCorrectInteger()
{
// TODO - call the Calc.Add method with 2 integers
// TODO - check the result is as expected
}
}
}

We have named our test method Add_When2Integers_ShouldReturnCorrectInteger.

It is useful to have a good naming convention for tests so that, when we look at a failed test report, we can start to get an understanding of the problem immediately. In this case, the name starts with the method we are testing, followed by a brief description of the conditions for the test. The last part of the name is what we expect to happen.

Note that the test method is decorated with the Fact attribute.

The Fact attribute denotes that the method is a unit test for xUnit. Another attribute that denotes a unit test is called Theory. This can be used to feed the method a range of parameter values.
  1. Let's implement the unit test:
[Fact]
public void Add_When2Integers_ShouldReturnCorrectInteger()
{
var result = Calc.Add(1, 1);
Assert.Equal(2, result);
}

We call the method we are testing and put the return value in a result variable. Then, we use the Assert class from xUnit and its Equal method to check that the result is equal to 2.

  1. Let's run our test by right-clicking inside the test method and choosing Debug Tests(s) from the menu:

  1. After a few seconds, the test will run and the result will appear in the Test Explorer:

As we expected, the test passes. Congratulations—you have just created our first unit test!

We used the Equal method in the Assert class in this test. The following are globally some other useful methods in this class:

  • True: Checks that a value is true
  • NotNullChecks that a value isn't null
  • Contains: Checks that the value is in a string
  • InRange: Checks that the value is within a range
  • Throws: Checks that an exception is raised

Now, we are starting to understand how to write unit tests. We haven't written any tests on our Q and A app yet, but we will do so next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.71.71