Table of Contents

Contribute to 2sxc / EAV Tests

Important: If you only want to USE 2sxc / EAV, then you do NOT need this. This is meant for people who want to contribute to the source code of 2sxc and EAV.

2sxc has about 3'500 unit tests and growing. This is a guide to help you understand how to run them, and how to contribute to them.

Pre-Requisites

  • all the pre-requisites for building 2sxc

Background

On 2sxc and the EAV project we strive to have many unit tests, but as always it's hard to keep up.

One of the challenges is also that some of the tests were written when we were less experienced, so we would do it better today. So if you do review some tests, note that they may not use the latest best practices.

Desired Setup and Conventions

As of 2025-03 we want to use xUnit for all our unit tests. But only a small fraction of the tests have been migrated. Other tests still use the old MSTest framework.

This is especially apparent in the setup of the tests, which require dependency injection to be properly setup for complex tests.

This is how we want it to be:

  1. Every project has one or more separate Tests project. Advanced scenarios need own projects, simple scenarios can share a project.
    Example: ToSic.Lib.Core has
    1. ToSic.Lib.Core.Tests - general tests
    2. ToSic.Lib.DI.Tests - specific tests which require a DI setup
  2. If necessary, some projects also have a ...TestHelper project containing shared test code for this project and other projects which build on it.
    Example: ToSic.Eav.DataSources has a ToSic.Eav.DataSource.TestHelpers project which is not a unit-test project.
    1. Test helpers can contain TestAccessors which are static methods matching the original method but ending in ...Tac (for Test Accessor). These methods are used to access internal methods for testing. We need them to reduce the method-access count, as otherwise methods which are not in use any more have a large access count.

Standard xUnit CSProj

Here's a standard xUnit project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net9.0;net472</TargetFrameworks>
    <ImplicitUsings>enable</ImplicitUsings>
    <LangVersion>preview</LangVersion>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
    <!-- <RootNamespace>ToSic.Eav.Data</RootNamespace> -->
  </PropertyGroup>

  <!-- specific stuff -->
</Project>
  1. Test projects should set nullable to <Nullable>enable</Nullable>
  2. Test projects should use c# latest <LangVersion>preview</LangVersion>
  3. Test projects should set <ImplicitUsings>enable</ImplicitUsings> See implicit usings

Tracing / Logging Test Data

In some cases you want to log more information to the output. In the old days this was done using Trace.WriteLine(...), but this doesn't work in xUnit which runs processes in parallel.

So for this, inject the ITestOutputHelper output and use it to log messages.

public class CompressorTests(ITestOutputHelper output)
{
  [Fact]
  public void Compress()
  {
    output.WriteLine("Starting compression test");
    // do something
  }
}

Note: older code which was converted to xUnit may still have Trace.WriteLine statements, but they will not appear in the output and should be updated as you find them.

Differences in .net 472 and .net Core

Some tests need different values depending on the .net framework. Use #if statements for this.

public class CompressorTests(ITestOutputHelper output)
{
    // Compression sizes differ between .NET Framework and .NET Core
#if NETCOREAPP
    private const int SizeDeflate = 14980;
    private const int SizeGZip = 14998;
#else
    private const int SizeDeflate = 14898;
    private const int SizeGZip = 14916;
#endif
}

Conversion to xUnit - Progress

  1. ToSic.Lib - 100%
    1. ToSic.Lib.Core.Tests
    2. ToSic.Lib.DI.Tests
  2. ToSic.Eav.Core - 100%
    1. ToSic.Eav.Core.TestHelpers (Startup and Test-Accessors)
    2. ToSic.Eav.TokenEngine.Tests
    3. ToSic.Eav.Core.TestsBasic (basic tests)
    4. ToSic.Eav.Data.Tests (data tests)
    5. ToSic.Eav.StartupTests (full tests)
  3. ToSic.Eav.DataSources - 100%
    1. ToSic.Eav.DataSource.TestHelpers
    2. ToSic.Eav.DataSource.Tests (unit tests)
    3. ToSic.Eav.DataSource.DbTests (system tests)