Home > other >  xUnit testing error (CS5001) Program does not contain a static Main method suitable for an entry poi
xUnit testing error (CS5001) Program does not contain a static Main method suitable for an entry poi

Time:12-29

Good day/evening! I am trying to do a test program in xUnit with the following code in Visual Studio Code:

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");
    }
}   

The file I am trying to test this code with is:

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");
            }
        }
    }
}

The program folder structure is as following:

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>

The error I'm getting is this one:

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

If I forgot anything, please let me know and I will provide anything relevant.

Thank you so much, in advance, Ionut

CodePudding user response:

When you have installed xUnit to your project, the compiler will Generate Program File with a Main method automatically configured to run the given tests.
This configuration in .csproj file will prevent that.

<GenerateProgramFile>false</GenerateProgramFile>

Then you have to create the Main method by yourself.


You have this configuration in Emp.Lib and Emp.Test projects. In Emp.Lib you have a Main method but not in Emp.Test.

You need to do

Generate your Main method in Emp.Test or remove GenerateProgramFile tag in Emp.Test.csproj file.

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