Table of Contents

xUnit Theory Tests - Using Files

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.

Using Files in Tests

In some situations we need to read files to setup a test. This is how you can do it, in most scenarios.

  1. Place the files in a folder that is accessible by the test runner.
  2. Use the appropriate methods to read these files during the test setup.
  3. Ensure that your tests clean up any resources after execution.

Example

The project ToSic.Eav.StartupTests has a folder ScenarioData that contains various test files for different scenarios.

The csproj file has a configuration to copy these files into the bin folder when compiling, like this:

<ItemGroup>
  <!-- Copy ScenarioData to bin folder for reviewing during tests -->
  <None
    Include="ScenarioData\**"
    CopyToOutputDirectory="PreserveNewest"
    LinkBase="ScenarioData\" />
</ItemGroup>

We have a helper class called TestFiles which helps us find the bin folder and attach the full folder path to the file name.

using System.Reflection;

namespace ToSic.Eav.Testing;

public static class TestFiles
{
    public static string GetTestPath(string relativePath)
    {
        var codeBaseUrl = new Uri(Assembly.GetExecutingAssembly().Location);
        var codeBasePath = Uri.UnescapeDataString(codeBaseUrl.AbsolutePath);
        var dirPath = Path.GetDirectoryName(codeBasePath);
        return Path.Combine(dirPath, relativePath);
    }
}

We can then use this to load the files as needed and do our work with them. The following example uses this folder to create a TestScenario with this folder:

using ToSic.Eav.Testing;
using ToSic.Eav.Testing.Scenarios;

namespace ToSic.Eav.ConfigurationOverride;

/// <summary>
/// Special scenario which has a configuration to override the Fancybox3 Web Resources
/// </summary>
public record ScenarioOverrideFancybox3 : ScenarioBasic
{
    public override string GlobalDataCustomFolder => TestFiles.GetTestPath("ScenarioData\\OverrideFancybox3") + $"\\{ScenarioConstants.DevMaterialsEnd}";
}