【发布时间】:2021-12-28 11:05:00
【问题描述】:
我通过 Visual Studio 的添加新项目功能创建了一个全新的“类库(桌面中的 Win Ui)”库。
出于这个问题的目的,我已将库的名称替换为 <MYLIBRARY>。
创建“xUnit 测试项目”并选择“.NET Core 3.1(长期支持)”作为目标框架,然后将项目引用添加到类库后,出现以下错误:
Error NU1201 Project <MYLIBRARY> is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Project <MYLIBRARY> supports: net5.0-windows10.0.19041 (.NETCoreApp,Version=v5.0) <MYLIBRARY>.UnitTests
Error Project '..\<MYLIBRARY>\<MYLIBRARY>.csproj' targets 'net5.0-windows10.0.19041.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v3.1'. <MYLIBRARY>.UnitTests C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 1718
删除该测试项目后,创建一个新的“xUnit 测试项目”并选择“.NET 5 (Current)”作为目标框架,然后将项目引用添加到类库,我收到以下错误:
Error NU1201 Project <MYLIBRARY> is not compatible with net5.0 (.NETCoreApp,Version=v5.0). Project <MYLIBRARY> supports: net5.0-windows10.0.19041 (.NETCoreApp,Version=v5.0) <MYLIBRARY>.UnitTests
Error Project '..\<MYLIBRARY>\<MYLIBRARY>.csproj' targets 'net5.0-windows10.0.19041.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v5.0'. <MYLIBRARY>.UnitTests C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 1718
在单元测试项目的 .csproj 中,我尝试将 <TargetFramework>net5.0</TargetFramework> 更改为 <TargetFramework>net5.0-windows</TargetFramework>,但这并没有解决任何问题。
我浏览了各种网站,但我发现一些答案似乎表明我应该能够做我想做的事,因为“像 xUnit.net 这样的单元测试框架通常与您的生产代码使用的库无关.”,没有解释如何让它工作,如果它不是,还有其他人说我不能做我想做的事,因为 xUnit 和 Win Ui 3 还不兼容,例如“WinUI 3 目前不支持任何 .NET 单元测试框架。”。
我对 Stack Overflow 进行了一些搜索 - 对于 Win Ui / xUnit / 等 - 但我发现的结果对我没有任何用处。
我是否可以针对我的类库中的类运行 xUnit 单元测试?
(我可能会搞混一些事情,因为这是我的第一个 Win Ui 项目。)
【问题讨论】: