只有1个Main方法的XUnit测试项目:“程序定义了多个入口点。”

我将vNext格式的.NET xUnit测试项目(带有project.json)转换为Visual Studio 2017 RC中的新.csproj格式,并开始收到以下错误。 这个错误的大多数在线答案都说“你有两个主要方法;摆脱一个。” 这似乎是一个明显的解决方案,但这个项目只有一个Main方法。

错误:

CS0017程序定义了多个入口点。 使用/ main编译以指定包含入口点的类型。 Project.Name C:\ path \ to \ Program.cs

Program.cs中:

using XunitProgram = Xunit.Runner.DotNet.Program; namespace My.Namespace.Tests { public static class Program { public static void Main(string[] args) { XunitProgram.Main(args); } } } 

老project.json:

 { "version": "1.0.0-*", "testRunner": "xunit", "buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true, "debugType": "full" }, "dependencies": { "dotnet-test-xunit": "2.2.0", "xunit": "2.2.0", "Microsoft.DotNet.InternalAbstractions": "1.0.0" }, "frameworks": { "net462": {} } } 

新Project.csproj:

   net462 win7-x86 full true My.Project.Tests Exe My.Project.Tests true false false false My.Project.Tests                

这需要一些修修补补。 在VS 2017中迁移到新项目格式后,Microsoft悄悄地为Microsoft.NET.Test.Sdk添加了一个依赖项,我相信它有自己的Main方法。

如果您在VS 2017 RC中创建了一个新的xUnit测试项目以及您已迁移的项目,您将注意到它不再使用调用XUnit运行器的Main方法创建Program.cs。

要解决此问题,请删除唯一可见的Main方法。 如果您有上述软件包引用( Microsoft.NET.Test.Sdkxunit.runner.visualstudioxunit.runner.visualstudio ,您的测试仍将正常执行。

因为当你有一个带有测试的控制台应用程序时,我更喜欢这个:

TL;博士; 在测试项目的.csproj文件中false inside a 元素false inside a 添加false inside a

从:

https://andrewlock.net/fixing-the-error-program-has-more-than-one-entry-point-defined-for-console-apps-containing-xunit-tests/

我的netcoreapp项目(类型netcoreapp )都没有main方法。 主要方法由xunit提供。 首先,project.json不需要Program.cs 。 现在,使用csproj,这似乎被提升为错误。

查看此原始xunit 文档并搜索main

我有一个类似的问题。 检查是否已将AddCommandLine(args)配置选项添加到ConfigurationBuilder中。例如

 var config = new ConfigurationBuilder() .AddCommandLine(args) .AddEnvironmentVariables(prefix: "ASPNETCORE_") .Build(); 

删除条目。 这导致我的问题……

希望能帮助到你