C# XUnit测试项目,只有一种主要方法:“;程序定义了多个入口点。”;
我在VisualStudio2017 RC中将vNext格式的.NET xUnit测试项目(带有project.json)转换为新的.csproj格式,并开始出现以下错误。对于这个错误,大多数在线答案都是“你有两个主要方法;去掉一个。”这似乎是一个显而易见的解决方案,但这个项目只有一个主要方法 错误:C# XUnit测试项目,只有一种主要方法:“;程序定义了多个入口点。”;,c#,.net-core,xunit,visual-studio-2017,C#,.net Core,Xunit,Visual Studio 2017,我在VisualStudio2017 RC中将vNext格式的.NET xUnit测试项目(带有project.json)转换为新的.csproj格式,并开始出现以下错误。对于这个错误,大多数在线答案都是“你有两个主要方法;去掉一个。”这似乎是一个显而易见的解决方案,但这个项目只有一个主要方法 错误: using XunitProgram = Xunit.Runner.DotNet.Program; namespace My.Namespace.Tests { public stati
using XunitProgram = Xunit.Runner.DotNet.Program;
namespace My.Namespace.Tests
{
public static class Program
{
public static void Main(string[] args)
{
XunitProgram.Main(args);
}
}
}
{
"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 Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
<DebugType>full</DebugType>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>My.Project.Tests</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>My.Project.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace>My.Project.Tests</RootNamespace>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.2.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Folder Include="SampleInput\" />
</ItemGroup>
</Project>
CS0017程序定义了多个入口点。使用/main编译以指定包含入口点的类型。项目名称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);
}
}
}
{
"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 Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
<DebugType>full</DebugType>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>My.Project.Tests</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>My.Project.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace>My.Project.Tests</RootNamespace>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.2.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Folder Include="SampleInput\" />
</ItemGroup>
</Project>
Old project.json:
using XunitProgram = Xunit.Runner.DotNet.Program;
namespace My.Namespace.Tests
{
public static class Program
{
public static void Main(string[] args)
{
XunitProgram.Main(args);
}
}
}
{
"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 Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
<DebugType>full</DebugType>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>My.Project.Tests</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>My.Project.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace>My.Project.Tests</RootNamespace>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.2.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Folder Include="SampleInput\" />
</ItemGroup>
</Project>
新项目。csproj:
using XunitProgram = Xunit.Runner.DotNet.Program;
namespace My.Namespace.Tests
{
public static class Program
{
public static void Main(string[] args)
{
XunitProgram.Main(args);
}
}
}
{
"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 Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
<DebugType>full</DebugType>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>My.Project.Tests</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>My.Project.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace>My.Project.Tests</RootNamespace>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.2.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Folder Include="SampleInput\" />
</ItemGroup>
</Project>
net462
win7-x86
满的
真的
我的项目测试
Exe
我的项目测试
真的
假的
假的
假的
我的项目测试
这需要一些修补才能弄清楚。在VS2017中迁移到新的项目格式时,Microsoft悄悄地添加了对Microsoft.NET.Test.Sdk的依赖项,我相信它有自己的主要方法
如果您在VS 2017 RC中创建一个新的xUnit测试项目,并将其与已迁移的项目一起创建,您将注意到它不再使用调用xUnit runner的主方法创建Program.cs
若要解决此问题,请删除您唯一可见的主方法。如果您有上述包引用(Microsoft.NET.Test.Sdk
,xunit.runner.visualstudio
,xunit
),您的测试仍将正常执行。我的xunit项目(typenetcoreapp
)都没有主方法。主要方法由xunit提供。首先,project.json不需要您的Program.cs
。现在,在csproj中,这似乎升级为错误
查看原始xunit并搜索main我也有类似的问题。检查是否已将AddCommandLine(args)配置选项添加到ConfigurationBuilder中。。e、 g
var config = new ConfigurationBuilder()
.AddCommandLine(args)
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.Build();
删除条目。这导致了我的问题
希望它对您有所帮助因为当您有一个带有测试的控制台应用程序时,我更喜欢这样: tl;博士在测试项目的.csproj文件中的元素中添加
false
发件人:
谢谢你的帮助。我有一个使用MSTest的项目,我刚刚在VS2017中打开并迁移到csproj,显示了相同的问题。谢谢!我已经在我的web应用程序中对内部内容进行了xunit测试,但到目前为止还没有得到合理的解释。但这条消息不应该出现。不是每个控制台项目都应该包含xunit测试和一个main方法吗?不是,因为项目反映了一个软件模块(在.NET中的程序集)。单元测试是一种开发工件,类似于构建文件。不应将它们部署到生产环境中,因为它们通常不能实现生产环境中的目的,但会带来风险。+1~我喜欢这样。它或多或少地表达了我10个月前的猜测,它注入了自己的Main method/Program.cs,尽管我很高兴看到有人更详细地了解了如何/为什么,并提出了一个优雅的解决方案,而不需要破坏自己的入口点。如果我使用此方法创建另一个测试项目,我可能会遵循您的方法。谢谢。作为将来的参考-在我的例子中,它似乎不起作用,但我在我的主项目中添加了false,而不是测试项目,它起了作用。