【问题标题】:CS5001 Program does not contain a static 'Main' method suitable for an entry point XUnitCS5001 程序不包含适用于入口点 XUnit 的静态“Main”方法
【发布时间】:2020-08-04 01:37:38
【问题描述】:

我有一个带有 Main 方法的控制台应用程序

 private static void Main(string[] args)
        {
            #region Variables Initialization

            var sqlService = new SQLService();
            var ruleService = new RuleService();
            var fileService = new FileService();
            var recDetail = new ReconciliationDetails();
            List<string> ListHashesSource = null;
            List<string> ListHashesTarget = null;
            HashSet<string> HashsetSource = new HashSet<string>();
            HashSet<string> HashsetTarget = new HashSet<string>();
            HashSet<string> interceptedRows = new HashSet<string>();
            int numberOfMatchesOnSource = 0;
            int numberOfMatchesOnTarget = 0;

...
         }

我正在尝试安装 xUnit 来对我的控制台应用程序进行单元测试和集成测试。

namespace SparkPOC.Tests
{
    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
            Assert.True(true);
        }
    }
}

问题是这个测试没有出现在我的 Visual Studio 2019 的测试资源管理器窗口中,当我构建我的解决方案时,它会抛出这个错误:

SparkPOC.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="Models\AppConfiguration.cs~RF1f4d2dd7.TMP" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
    <PackageReference Include="ConsoleTables" Version="2.4.0" />
    <PackageReference Include="Dapper" Version="2.0.30" />
    <PackageReference Include="EasyConsole" Version="1.1.0" />
    <PackageReference Include="Microsoft.Spark" Version="0.8.0" />
    <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.0" />
    <PackageReference Include="TrieNet.Core" Version="1.0.4" />

  </ItemGroup>

</Project>

SparkPOC.Tests.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>

    <IsPackable>false</IsPackable>
    <GenerateProgramFile>false</GenerateProgramFile>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.0" />
    <PackageReference Include="Microsoft.TestPlatform.TestHost" Version="16.6.0" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"/>
  </ItemGroup>
 <ItemGroup>
    <ProjectReference Include="..\SparkPOC\SparkPOC.csproj" />
  </ItemGroup>

</Project>

我添加了这一行

    <GenerateProgramFile>false</GenerateProgramFile>

根据本站建议

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

解决方案结构:

我的测试浏览器

【问题讨论】:

  • 对我的控制台应用程序进行单元测试和集成测试 为什么需要在同一个控制台应用程序中执行此操作?你介意把它移到单独的项目中吗?
  • 目前我只考虑做单元测试,但我希望将来做一个集成测试,因为我从来没有做过,我正在努力学习它
  • 我不清楚您为什么指定 &lt;GenerateProgramFile&gt;false&lt;/GenerateProgramFile&gt; - 您的测试项目包含 Main 方法。不过,我会一次专注于一个问题 - 卸载您的测试项目并只需让您的控制台应用程序首先运行。一旦它工作了,然后让测试项目也工作。
  • 如果我卸载测试项目,一切正常
  • 等一下 - 我刚刚注意到您的测试项目甚至没有引用您的主项目。这很令人困惑。如果您可以在此处提供minimal reproducible example,那将非常有帮助。真的应该没问题。

标签: c# .net-core xunit


【解决方案1】:

您需要为单元测试创​​建一个单独的项目。并且不要在这个项目中声明一个带有Main 方法的类,xUnit 已经有一个Main 方法用于运行测试。

并确保在 Tools > Options > Test > General 下选中“Discover tests in real time from C# ....”

否则,每次添加新测试时,您都需要单击“全部运行”以发现所有测试。

【讨论】:

  • 是的,我试图重现您的问题,显然是 &lt;GenerateProgramFile&gt;false&lt;/GenerateProgramFile&gt; 导致了我这边的错误。一旦删除,一切都很好。
  • 是的,我删除了它并解决了错误,但测试仍然没有显示在 Visual Studio 2019 中的测试资源管理器中
  • 我为测试发现选项添加了屏幕截图。
  • 我必须按照你的建议去做,我必须去 VS 安装程序并安装 .NET 桌面开发包,它成功了
  • 对于那些使用 xunit 的人,我从 恢复到 和 CS5001 错误消失了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-21
  • 1970-01-01
  • 2018-05-15
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多