如何将Xunit用于MAUI项目用于平台特定代码?
我正在为Android和Windows提供MAUI项目和平台特定的代码。当我将MAUI项目引用到Visual Studio 2022预览中的Xunit Project时,它显示了一个错误,说“项目与Net6.0不兼容”,因为MAUI项目参考目标框架” Net6.0-and6.0-and6.0-Windows ”。
有什么方法引入,以便我可以参考MAUI项目和单位测试平台特定代码?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Visual Studio中的股票“ Net6.0” Xunit测试无法参考Net6.0 Android和Net6.0-IOS库等。我已经看到您的Maui应用程序可以修改为多目标Net6.0。参见 https://www.youtube.com/watch?v教程。
对于使用平台特定代码运行的测试,我在,在实际的移动设备上运行。 Xunit团队过去曾为Xamarin.Forms跑步 - 我不知道他们是否会开始为毛伊岛维护它们。
A stock "net6.0" xUnit test in Visual Studio cannot reference net6.0-android and net6.0-iOS libraries, etc. I have seen that your MAUI app can be modified to multi-target net6.0. See https://www.youtube.com/watch?v=C9vIDLQwc7M for a tutorial.
For running tests with platform-specific code, I've had reasonable success with https://www.nuget.org/packages/Shiny.Xunit.Runners.Maui, to run on actual mobile devices. The xUnit team has made runners for Xamarin.Forms in the past - I don't know if at some point they will start to maintain them for MAUI or not.
我使用Xunit和.NET 8.0遇到了同样的问题,
尽管可以在MAUI项目中定位Net6/7/8.0,然后进行您的测试参考,并不总是那么简单。
我的MAUI项目使用了Uxddivers的Grial,并试图针对Net8.0造成了大量错误,因为Grial Nuget
经过深思熟虑,无法针对Net8.0,解决此问题的唯一方法是将所有内容从MAUI项目中移动它需要对一个新的Net8.0项目进行测试,然后可以从MAUI项目和测试项目中引用该项目。
I came across this same issue using xUnit and .NET 8.0
While it is possible to target net6/7/8.0 in the MAUI project and then have your tests reference that, it may not always be that simple.
My MAUI project used Grial from UXDivers and trying to target net8.0 created a large amount of errors as the Grial NuGet does not target net8.0
After much thought, the only way to solve this issue would be to move everything from the MAUI project which requires testing to a new net8.0 project which can then be referenced from the MAUI project and the test project.