【问题标题】:xUnit testing error (CS5001) Program does not contain a static Main method suitable for an entry pointxUnit 测试错误 (CS5001) 程序不包含适用于入口点的静态 Main 方法
【发布时间】:2021-12-27 13:07:56
【问题描述】:

早安/晚安! 我正在尝试使用 Visual Studio Code 中的以下代码在 xUnit 中执行测试程序:

using Xunit;
using FluentAssertions;
using System.Collections.Generic;

namespace Ferma;

public class UnitTest1
{
    [Fact]
    public static void EmployeeTest()
    {
        //Arrange
        List<FermaMea.Employee> List = new List<FermaMea.Employee>();
        //Act + Assert
        List.Should().Contain(p => p.GID == "Z0001");
    }
}   

我要测试此代码的文件是:

using Newtonsoft.Json;
using Serilog;

namespace Ferma
{
    public partial class FermaMea
    {
        public static void Main()
        {
            List<Employee> myEmployee = new List<Employee>();
            myEmployee.Add(new Employee() { GID = "Z0001", FirstName = "Billy Bob", LastName = "Bean", Department = "Cows11" });
            myEmployee.Add(new Employee() { GID = "Z0002", FirstName = "Joey", LastName = "Ray", Department = "Field" });

            string ExportToJson = JsonConvert.SerializeObject(myEmployee, Formatting.Indented);
            File.WriteAllText(@"..\Emp.App\config\Employee.json", ExportToJson);
        }

        public class Employee
        {
            public string? GID { get; set; }
            public string? FirstName { get; set; }
            public string? LastName { get; set; }
            public string? Department { get; set; }

            public Employee()
            {
                Log.Information("New employee has been added");
            }
        }
    }
}

程序文件夹结构如下:

MainFolder
    -> Emp.Test-> UnitTest1.cs
    -> Emp.Lib -> Employee.cs
    -> Emp.App -> Main.cs (Here is my main program, which, by my understanding, is not affected in this question. 
                  But if I'm mistaken, please tell me and I will provide all relevant informations.

Emp.Test.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <GenerateProgramFile>false</GenerateProgramFile>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
    <GenerateProgramFile>false</GenerateProgramFile>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0-preview-20211130-02"/>
    <PackageReference Include="MSTest.TestAdapter" Version="2.2.7"/>
    <PackageReference Include="MSTest.TestFramework" Version="2.2.7"/>
    <PackageReference Include="coverlet.collector" Version="3.1.0"/>
    <PackageReference Include="xunit" Version="2.4.2-pre.12"/>
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"/>
    <PackageReference Include="FluentAssertions" Version="6.2.0"/>
    
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\Emp.App\Emp.App.csproj"/>
    <ProjectReference Include="..\Emp.Lib\Emp.Lib.csproj"/>
  </ItemGroup>
</Project>

Emp.Lib.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <GenerateProgramFile>false</GenerateProgramFile>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
    <PackageReference Include="Serilog" Version="2.10.0"/>
    <PackageReference Include="Serilog.Sinks.Console" Version="4.0.1"/>
    <PackageReference Include="Serilog.Sinks.File" Version="5.0.1-dev-00947"/>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0-preview-20211130-02"/>
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"/>
    <PackageReference Include="xunit" Version="2.4.2-pre.12"/>
    <PackageReference Include="FluentAssertions" Version="6.2.0"/>
  </ItemGroup>
</Project>

我得到的错误是这个:

----- Running test method "Ferma.UnitTest1.EmployeeTest" -----

Microsoft (R) Build Engine version 17.0.0+c9eb9dd64 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Emp.Lib -> C:\WORK\demofarm\Emp.Lib\bin\Debug\net6.0\Emp.Lib.dll
  Emp.App -> c:\WORK\demofarm\Emp.App\bin\Debug\net6.0\Emp.App.dll
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [c:\WORK\demofarm\Emp.Test\Emp.Test.csproj]

Build FAILED.

CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [c:\WORK\demofarm\Emp.Test\Emp.Test.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:02.90

如果我忘记了什么,请告诉我,我会提供任何相关信息。

非常感谢您,提前, 艾诺特

【问题讨论】:

    标签: c# .net visual-studio-code


    【解决方案1】:

    xUnit 安装到项目后,编译器将生成程序文件,并自动配置Main 方法以运行给定的测试。
    .csproj 文件中的此配置将防止这种情况发生。

    <GenerateProgramFile>false</GenerateProgramFile>
    

    那么你必须自己创建Main方法。


    您在 Emp.LibEmp.Test 项目中有此配置。 在Emp.Lib 中有一个Main 方法,但在Emp.Test 中没有。

    你需要做的

    Emp.Test 中生成Main 方法或删除Emp.Test.csproj 文件中的GenerateProgramFile 标记。

    Emp.Test.csproj

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <GenerateProgramFile>false</GenerateProgramFile> <!-- remove this -->
        <Nullable>enable</Nullable>
        <IsPackable>false</IsPackable>
        <GenerateProgramFile>false</GenerateProgramFile> <!-- remove this -->
        <LangVersion>latest</LangVersion>
      </PropertyGroup>
      ...
    </Project>
    

    【讨论】:

    • 非常感谢。删除文件就行了。
    猜你喜欢
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多