为什么我不能在 C# 中将 'dotnet run' 与 xUnit 一起使用?

Why can't I use 'dotnet run' with xUnit in C#?

我正在创建一个需要单元测试的应用程序。我正在使用 .NET xUnit 框架。

首先,我用

初始化了一个新的"Hello, World! class"
dotnet new console

然后我添加了单元测试

dotnet new xunit

当我 运行 dotnet rundotnet test 时,我得到这个错误:

 error CS0017: Program has more than one entry point defined. Compile with /main to specify the type thatcontains the entry point.

我了解到在 xUnit 中定义了一个 Main,它会干扰 Program 中的入口点,但我如何才能将它们分开?也就是说,我如何才能 运行 同时 dotnet rundotnet test?

你不应该 运行 同时 "dotnet new console" 和 "dotnet new xunit" 在同一个目录中。他们是不同的项目,他们有不同的切入点。

找到了一个能够 运行 dotnet testdotnet run here.

的解决方案

问题是测试项目构建了自己的 Program.cs 文件。您想将以下内容添加到您的 .csproj:

<GenerateProgramFile>false</GenerateProgramFile>