【问题标题】:X Unit test not compatible with .net core 2.2X 单元测试与 .net core 2.2 不兼容
【发布时间】:2019-09-20 11:13:55
【问题描述】:

我有.net core 2.2 的网络应用程序。

我想用XUnitMSUnit 测试我的Web 应用程序,但是遇到了问题。

XUnit项目中Microsoft.NETCore.App包的版本为2.1.0,api项目中Microsoft.NETCore.App的版本为2.2。

我搜索了好几个小时,做了我看到的所有事情,但没用。 我该怎么办?

【问题讨论】:

  • XUnit project中的应用包更新为2.2。它对我来说很好:<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> <PackageReference Include="xunit" Version="2.4.0" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />

标签: unit-testing asp.net-core .net-core xunit


【解决方案1】:

只需添加

<PackageReference Include="Microsoft.AspNetCore.App"/>

到你的测试项目,rebuild就可以了。

这是我的 .csproj 示例

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App"/>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
    <PackageReference Include="Moq" Version="4.13.0" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
  </ItemGroup>

...

</Project>

您可以通过this查看问题

【讨论】:

    【解决方案2】:

    正如您在this 链接上看到的,在 Github 上打开了相同的问题,XUnit 似乎没有提供任何解决方案。我在我的 .NET Core v2.2 项目中使用 Nunit,它运行良好。要查看该库,您可以查看this 页面。语法与 XUnit 非常相似,我想你不会花那么长时间来更新你的测试。所以,我建议你迁移 NUnit。

    【讨论】:

    • NUnit 有一个重要区别,XUnit 为每个测试方法实例化一个新的测试类实例,而 NUnit 只为其中的所有测试实例化一个测试类实例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 2023-03-25
    • 2015-05-24
    相关资源
    最近更新 更多