Adding unit tests

Unit tests allow you to individually verify the behavior of your various technical components and ensure that they are working as expected. They also help you quickly identify regressions and analyze the overall impact of new developments. Visual Studio 2019 includes powerful features for unit testing.

The Test Explorer helps you run unit tests as well as view and analyze test results. For that, you can either use the built-in Microsoft testing framework or additional frameworks such as NUnit or xUnit.

Furthermore, you can automatically execute unit tests after each build so that developers can react quickly if something isn't working as expected.

Refactoring code can be done without fearing regressions since unit tests ensure that everything is still working like it was previously. No more excuses for not having the best code quality possible!

You could even go further and apply test-driven development (TDD), which is where you write unit tests before writing implementations. Additionally, unit tests become some sort of design document and functional specifications. A further step would be to apply behavior-driven development (BDD) and create tests from specifications.

This book is about ASP.NET Core 3, so we won't go into too much detail about unit tests. It is, however, advised to dig deeper and familiarize yourself with all the different unit test concepts so that you can build better applications.

Let's learn how easy it is to use xUnit, which is the preferred unit testing framework for ASP.NET Core 3:

  1. Add a new project of the xUnit Test Project (.NET Core) type called TicTacToe.UnitTests to the TicTacToe solution:

  1. Update the xunit and Microsoft.NET.Test.SDK NuGet packages to their latest versions using the NuGet Package Manager: 

  1. Add references to the TicTacToe and TicTacToe.Logging projects: 

  1. Delete the autogenerated class, add a new class called FileLoggerTests.cs for testing a regular class, and implement a new method called ShouldCreateALogFileAndAddEntry
public class FileLoggerTests
{
[Fact]
public void ShouldCreateALogFileAndAddEntry()
{
var fileLogger = new FileLogger(
"Test", (category, level) => true,
Path.Combine(Directory.GetCurrentDirectory(),
"testlog.log"));
var isEnabled = fileLogger.IsEnabled
(LogLevel.Information);
Assert.True(isEnabled);
}
}
  1. Add another new class called UserServiceTests.cs for testing services and implement a new method called ShouldAddUser
public class UserServiceTests
{
[Theory]
[InlineData("[email protected]", "test", "test", "test123!")]
[InlineData("[email protected]", "test1", "test1", "test123!")]
[InlineData("[email protected]", "test2", "test2", "test123!")]
public async Task ShouldAddUser(string email, string firstName,
string lastName, string password)
{ var userModel = new UserModel
{ Email = email,
FirstName = firstName,
LastName = lastName,
Password = password };
var userService = new UserService();
var userAdded = await userService.RegisterUser
(userModel);
Assert.True(userAdded); }
}
  1. Open Test Explorer via Test | Windows | Test Explorer and choose to Run All to ensure that all the tests execute successfully: 

Unit tests are great and really important, but also somewhat limited. They only test each technical component separately, which is the main goal of this type of test.

The idea behind unit tests is to quickly get a glimpse of the current status of all your technical components, one by one, without slowing down the continuous integration process. They don't test applications under real production conditions since external dependencies are mocked. Instead, they are intended to run quickly and ensure that each method being tested creates no unintended side effects in other methods or classes.
If you stop here, you won't be able to find as many bugs as you usually would during the development phase. You have to go even further and test all the components together in a real environment; this is where integration tests come into play.

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

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