CS5001 程序不包含适用于入口点 XUnit 的静态 'Main' 方法

CS5001 Program does not contain a static 'Main' method suitable for an entry point XUnit

我有一个带有 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 测试浏览器 window 上,当我构建我的解决方案时它会抛出这个错误:

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/

解决方案结构:

我的测试浏览器

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

并确保在工具 > 选项 > 测试 > 常规 下选中 "Discover tests in real time from C# ...."

否则,每次添加新测试时,您都需要单击 "Run All" 来发现所有测试。