简体   繁体   English

使用 Xunit 运行单元测试

[英]Running unit test using Xunit

I have added a unit testing class and added the below references to my main class library in the UWP project.我在 UWP 项目中添加了一个单元测试 class 并将以下引用添加到我的主要 class 库中。

参考

When I add a new class library and add my test classes there the test methods on the class library execute but the test methods on the main project throw the below exception.当我添加一个新的 class 库并在那里添加我的测试类时,class 库上的测试方法会执行,但主项目上的测试方法会引发以下异常。

System.InvalidOperationException: The following test container was not found: 'F:\test\WinApp\bin\x64\Debug\WinApp.exe'. This can be resolved by one or more of the following steps:
1. The test container does not exist on disk and the corresponding project might need to be built successfully.
2. For .NET Core based test projects, please ensure that a nuget package reference to "Microsoft.NET.Test.SDK" exists and that it uses the latest stable version.
3. For .NET based test projects, the project might be marked as a non-test project through an msbuild property, "IsTestProject". Please consider clearing it or set "<IsTestProject>true</IsTestProject>" in a "<PropertyGroup>" in the test project.
   at Microsoft.VisualStudio.TestWindow.Client.TestContainer.TestContainerProvider.<GetTestContainerAsync>d__42.MoveNext()

I tried the above approaches but no use.我尝试了上述方法,但没有用。

I want to write my test classes inside my main class library which is executable any suggestions.I am trying this as I cannot add references to the main class library from the test project and run it.Below error throws if I try to do so.我想在我的主 class 库中编写我的测试类,该库可执行任何建议。我正在尝试这样做,因为我无法从测试项目中添加对主 class 库的引用并运行它。如果我尝试这样做,则会引发以下错误。

在此处输入图像描述 My test project is a Unit Test App(Universal Windows)我的测试项目是一个单元测试应用程序(通用 Windows)

在此处输入图像描述

I am not running in a build server我没有在构建服务器中运行

A conflict is occurring since there are 2 Default.rd.xml files are available for the project.由于项目有 2 个Default.rd.xml文件可用,因此发生冲突。

A runtime directives (.rd.xml) file is an XML configuration file that specifies whether designated program elements are available for reflection.运行时指令 (.rd.xml) 文件是 XML 配置文件,它指定指定的程序元素是否可用于反射。 (Reference: Runtime Directive Documentation by Microsoft (参考: Microsoft 的运行时指令文档

So, if your Unit Test Project isn't using Reflection, then it should be safe to remove Default.rd.xml file from the Test Project (the one in F:\test\UnitTestProject2\Properties).因此,如果您的单元测试项目没有使用反射,那么从测试项目(F:\test\UnitTestProject2\Properties 中的那个)中删除 Default.rd.xml 文件应该是安全的。

Do a complete rebuild and you should be good to go.做一个完整的重建,你应该对 go 很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM