简体   繁体   English

在未安装 SDK 的情况下运行 xUnit 测试

[英]Run xUnit tests without SDK installed

I would like to run some xUnit tests on a server, without having to install the .NET Core SDK there.我想在服务器上运行一些 xUnit 测试,而不必在那里安装 .NET Core SDK。

The server is a Windows server with the .NET Core runtime v2.2 installed.服务器是安装了 .NET Core 运行时 v2.2 的 Windows 服务器。

How would I go about this?我该怎么办?

Add the xunit.console nuget package (version 2.4.1 at the time of writing), and add a one-liner to your Program.Main:添加xunit.console nuget 包(在撰写本文时为 2.4.1 版),并向您的 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.您还需要将<GenerateProgramFile>false</GenerateProgramFile>到您的 csproj 文件中。

Simply use dotnet publish , and you can use dotnet run "myapp.dll" to execute the tests.只需使用dotnet publish ,您就可以使用dotnet run "myapp.dll"来执行测试。

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.似乎这些包没有与其他 xunit 包一起更新(我认为这是一个 v2 版本 - 现在有一个 v3)但这似乎仍然适用于 netcoreapp3.1 和 net5 xunit 测试应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM