Simple test hosts in .NET using NUnitlite and file-based apps.

In my previous post I showed how NUnitlite can provide a low-ceremony, minimal footprint option to run NUnit tests. The introduction of file-based apps in .NET 10 allow for this to be taken further. A file-based app no longer requires a project or solution file meaning everything can be run from a single .cs file as if it were a script. This can drastically simplify the setup process, reducing the chances of misconfiguration and allowing more time to be spent focusing on writing the tests themselves.

Referencing External Test Projects

The simplest scenario is to have the file-based app serve as a thin test host from which your tests can be run. This can be achieved in just a few lines:

#:package NUnitlite@4.5.1
#:project ../FileBasedApps.Tests/FileBasedApps.Tests.csproj
using NUnitLite;
using FileBasedApps.Tests;
// Use NUnitlite to execute the tests
new AutoRun(typeof(SampleTests).Assembly).Execute(args);

A file with those contents will:

  1. Load version 4.5.1 of NUnitlite from NuGet
  2. Load a project file from a relative path
  3. Pass the test assembly into NUnitlite and run all discovered tests

It can be run from the command line with the following command:

dotnet run .\path-to\script-file.cs

Defining Inline Tests

More complex scenarios are also possible, such as having your script reference the system-under-test itself and then defining the tests directly within the C# script. This may be useful if you have a project or library and you’d like to quickly write a test to validate something without the ceremony of defining a full project and solution file. For this scenario, the example from my previous post can be used and modified slightly to load the desired system under test and to then define the test methods inline.

For example:

#:package NUnit@4.5.1
#:package NUnitlite@4.5.1
#:project ../FileBasedApps.Library/FileBasedApps.Library.csproj
using NUnitLite;
using NUnit.Framework;
using FileBasedApps.Library;
// Use NUnitlite to execute the tests
new AutoRun().Execute(args);
// The tests themselves
public class SampleTests
{
[Test]
public void Test1()
{
Assert.That(new SampleClass().MeaningOfLife, Is.EqualTo(42));
}
}

The key difference here is that no assembly is passed into NUnitlite’s AutoRun constructor. This will cause NUnitlite to instead reference the calling assembly to find tests. This is desirable in this case as the assembly calling NUnitlite will resolve to the assembly generated for our top-level script file where our tests reside, allowing it to “just work” for NUnitlite to find the tests to run.

Passing Command-line Arguments

It’s possible to pass options to NUnitlite to customize behaviour in either scenario by passing them on the command line after a -- delimiter. For example, the below command will change the output location of the test results file:

dotnet run .\path-to\script-file.cs -- --result=CustomResultFile.xml

A full list of NUnitlite command line options can be found here.

Summary

In summary, NUnitlite and the new file-based apps feature in .NET 10 work well together and can provide a very quick and easy way to start writing tests. There are several options to structure your approach and all options can be further configured at runtime by passing command-line arguments.

Comments

Leave a Reply

Discover more from Software by Steven

Subscribe now to keep reading and get access to the full archive.

Continue reading