美好的一天/晚上!我正在嘗試使用 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
如果我忘記了什么,請告訴我,我會提供任何相關的資訊。
非常感謝,提前,Ionut
uj5u.com熱心網友回復:
安裝xUnit到專案后,編譯器將使用自動配置為運行給定測驗的方法生成程式檔案Main。檔案中的
此配置.csproj將阻止這種情況。
<GenerateProgramFile>false</GenerateProgramFile>
然后你必須自己創建Main方法。
您在Emp.Lib和Emp.Test專案中有此配置。在Emp.Lib你有一個Main方法但沒有在Emp.Test.
你需要做
在檔案中生成您的Main方法Emp.Test或洗掉GenerateProgramFile標簽Emp.Test.csproj。
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/395595.html
上一篇:將所選文本復制到新行VS代碼?
