Modern .NET development offers a wide range of testing frameworks and, separately, test runners. Test frameworks may range from NUnit to XUnit to MSTest or others, while runners may be first-party to each of those or third-party and framework-agnostic. These runners raise the level of abstraction and can offer helpful features, however sometimes it may be that you just want to run tests with as little ceremony as possible. This is where I’ve found NUnitlite to shine.
NUnitlite works with NUnit to allow your test assembly to become an executable, removing the need for an external runner altogether. This can be helpful in particular if there are limitations or disconnects to installing or updating runners in CI environments.
For example:
Suppose a CI test suite is initiated from a dedicated server but the tests must access private or secure resources like a database. You could allow access to the database from the CI server a security-conscious person might consider that to violate the Principle of Least Privilege. That could be fixed by simply configuring the CI server to run the tests from a dedicated machine, but that could also carry the undesirable consequence of installing dedicated test runners on that machine. Allowing tests to run themselves removes both the need to open access to a trusted resource as well as removes needing to install an additional piece of software.
This can be done in just a few steps:
- Install NUnitlite to your test project
<PackageReference Include="NUnitLite" Version="4.5.1" />
- OPTIONAL: Update your test project to output as an “Exe”
<OutputType>Exe</OutputType>
- Add a
Program.csfile (if not using single-file applications) and include:
using NUnitLite;return new AutoRun().Execute(args);
These three steps will turn the test assembly into an executable which can be easily and independently run. More information and examples can be found in the documentation.
