Set up test projects

Automated tests typically mirror the structure of the tested code. We recommend creating at least one test project for every custom project in your Xperience solution. You can have any number of test projects in the solution.

Create a test project

Add a class library to your Xperience solution for the test project. When setting up the project:

  • Name the project with a .Tests suffix (for example, MyCustomCode.Tests).

  • Install the Kentico.Xperience.Core.Tests NuGet package.

    Bash
    
    
    
      dotnet add package Kentico.Xperience.Core.Tests
    
    
      
  • Add references from the test project to all projects whose code you want to test.

The Kentico.Xperience.Core.Tests package depends on an exact version of Kentico.Xperience.Core. Always install the same version of the test package as the Xperience version used by the rest of your solution, and update both together.

Additional packages

All functionality in the CMS.Tests library is compatible with the NUnit testing framework.

The package does not include a test runner or a mocking library.

Add the following test infrastructure packages to your test project. These packages enable test discovery and execution via dotnet test, which runs your tests from the command line without any IDE, as well as in IDE test runners such as Visual Studio Test Explorer and Rider:

  • NUnit3TestAdapter – test runner for dotnet test, Visual Studio, and Rider
  • Microsoft.NET.Test.Sdk – test platform infrastructure

For test doubles (fakes) of other service dependencies, you can add a mocking library of your choice (for example Moq, NSubstitute, or FakeItEasy), or use hand-written fakes and stubs.

With your test project ready, you can start writing test classes. To choose an approach, see unit tests with faked data, integration tests, or isolated integration tests. Use the Visual Studio Test Explorer or dotnet test to execute your tests.