仅具有1个主要方法的XUnit测试项目:“程序定义了多个入口点。” [英] XUnit test project with only 1 Main method: "Program has more than one entry point defined."

查看:122
本文介绍了仅具有1个主要方法的XUnit测试项目:“程序定义了多个入口点。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I converted a vNext format .NET xUnit test project (with project.json) to the new .csproj format in Visual Studio 2017 RC and started getting the below error. Most of the online answers to this error say "You have two Main methods; get rid of one." This seems like an obvious solution, but this project has only one Main method.

错误:


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

CS0017 Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. 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);
        }
    }
}

旧项目。 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": {}
  }
}

New Project.csproj:

<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>


推荐答案

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

This took a bit of tinkering to figure out. Upon migration to the new project format in VS 2017, Microsoft quietly added a dependency for Microsoft.NET.Test.Sdk, which I believe has its own Main method.

如果您在VS 2017 RC中创建了一个新的xUnit Test Project,同时又迁移了一个新的xUnit Test Project,您将注意到它不再创建具有Main方法的Program.cs来调用XUnit运行程序。

If you create a new xUnit Test Project in VS 2017 RC alongside the one that you had migrated, you will notice that it no longer creates a Program.cs with a Main method invoking the XUnit runner.

要解决此问题,请删除唯一可见的Main方法。只要拥有上述软件包,测试仍将照常执行引用( Microsoft.NET.Test.Sdk xunit.runner.visualstudio xunit )。

To fix this, delete your sole visible Main method. Your tests will still execute as normal, provided you have the above package references (Microsoft.NET.Test.Sdk, xunit.runner.visualstudio, xunit).

这篇关于仅具有1个主要方法的XUnit测试项目:“程序定义了多个入口点。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆