Search code examples
c#.net-corenunitnunit-console

Which Packages do I need to refer to to create my own NUnit console runner?


I am trying to create my own NUnit console runner.

Adding a reference to NUnit to my project does not let me access NUnit.Common, NUnit.Engine or NUnit.Options as used in the console runner in https://github.com/nunit/nunit-console/blob/master/src/NUnitConsole/nunit3-console/Program.cs

My Project file:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NUnit" Version="3.11.0" />
  </ItemGroup>

</Project>

My source:

using System; 
using NUnit.Common;
using NUnit.Options;
using NUnit.Engine;


namespace MyNUnitRunner
{
    class Program
    {
        static ConsoleOptions Options = new ConsoleOptions(new DefaultOptionsProvider(), new FileSystem());
        private static ExtendedTextWriter _outWriter;
        static void Main(string[] args)
        {
            try
            {
                Options.Parse(Options.PreParse(args));
            }
            catch (OptionException ex)
            {
                WriteHeader();
                OutWriter.WriteLine(ColorStyle.Error, string.Format(ex.Message, ex.OptionName));
                return ConsoleRunner.INVALID_ARG;
            }        
        }
    }
}

Output of dotnet build:

Program.cs(2,13): error CS0234: The type or namespace name 'Common' does not exist in the namespace 'NUnit' (are you missing an assembly reference?) [/Users/jon/DEV/CSSandbox/MyNUnitRunner/MyNUnitRunner.csproj]
Program.cs(3,13): error CS0234: The type or namespace name 'Options' does not exist in the namespace 'NUnit' (are you missing an assembly reference?) [/Users/jon/DEV/CSSandbox/MyNUnitRunner/MyNUnitRunner.csproj]
Program.cs(4,13): error CS0234: The type or namespace name 'Engine' does not exist in the namespace 'NUnit' (are you missing an assembly reference?) [/Users/jon/DEV/CSSandbox/MyNUnitRunner/MyNUnitRunner.csproj]
Program.cs(11,16): error CS0246: The type or namespace name 'ConsoleOptions' could not be found (are you missing a using directive or an assembly reference?) [/Users/jon/DEV/CSSandbox/MyNUnitRunner/MyNUnitRunner.csproj]
Program.cs(12,24): error CS0246: The type or namespace name 'ExtendedTextWriter' could not be found (are you missing a using directive or an assembly reference?) [/Users/jon/DEV/CSSandbox/MyNUnitRunner/MyNUnitRunner.csproj]
    0 Warning(s)
    5 Error(s)

Which packages do I need to refer to and how can I find this out (New to C#)?


Solution

  • You're after the NUnit Engine - this is the part of the project which is intended to be used by external 'runner' programs, to run tests.

    You should pull in the nunit.engine NuGet package. The only assembly your runner should reference should be nunit.engine.api.dll - that's the supported interface which will mean that your runner will continue to work with future versions of the engine.

    There's some documentation around the process here: https://github.com/nunit/docs/wiki/Test-Engine-API

    Your runner should not be referencing the NUnit NuGet package. That package contains the test framework, which should be referenced by every test assembly, but not by runners.

    Hope that helps - good luck!