【问题标题】:Why can't I use 'dotnet run' with xUnit in C#?为什么我不能在 C# 中将“dotnet run”与 xUnit 一起使用?
【发布时间】:2017-11-05 00:10:19
【问题描述】:

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

首先,我用

初始化了一个新的“Hello, World!类”
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

【问题讨论】:

    标签: c# xunit


    【解决方案1】:

    您不应在同一目录中同时运行“dotnet new console”和“dotnet new xunit”。它们是不同的项目,它们有不同的切入点。

    【讨论】:

    • 哦,好吧,你是说我应该做两个完全不同的项目:一个是测试版,一个是可运行版本?
    • 没错,你应该做两个项目,分别运行项目。带有dotnet run 的控制台应用程序和带有dotnet test 的测试应用程序。
    【解决方案2】:

    找到了一个能够同时运行dotnet testdotnet run here 的解决方案。

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

    <GenerateProgramFile>false</GenerateProgramFile>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-17
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 2013-07-02
      • 2015-11-08
      • 2013-08-24
      • 1970-01-01
      相关资源
      最近更新 更多