3

I would like to run some xUnit tests on a server, without having to install the .NET Core SDK there.

The server is a Windows server with the .NET Core runtime v2.2 installed.

How would I go about this?

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
Svein Fidjestøl
  • 3,106
  • 2
  • 24
  • 40
  • Very vague. What kind of tests? Unit tests? Unit tests are supposed to be run during building the application (e.g. in a pipeline) or while developing locally. Even integration tests. – Jabberwocky Feb 26 '20 at 16:29

1 Answers1

5

Add the xunit.console nuget package (version 2.4.1 at the time of writing), and add a one-liner to your Program.Main:

public class Program
{
    public static int Main()
    {
        return Xunit.ConsoleClient.Program.Main(
            new[] { typeof(Program).Assembly.Location });
    }
}

You will also need to add <GenerateProgramFile>false</GenerateProgramFile> to your csproj file.

Simply use dotnet publish, and you can use dotnet run "myapp.dll" to execute the tests.

You can extend this to pass through other args if you want flexibility to override reporters or filters if necessary.

It seems these packages have not been updated along with other xunit packages (i think this is a v2 release - there is now a v3) but this still seems to work with netcoreapp3.1 and net5 xunit test apps.

csmacnz
  • 331
  • 3
  • 8