简体   繁体   English

在NUnit.Framework和Microsoft.VisualStudio.TestTools.UnitTesting命名空间中也发现了NUnit断言方法错误

[英]NUnit Assert method error as it is found in NUnit.Framework and Microsoft.VisualStudio.TestTools.UnitTesting namespace as well

If I am using both statements at the same time 如果我同时使用两个语句

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;

I get an error for Assert method. 我收到一个Assert方法错误。 Could someone tell me what is the best way of using it. 有人可以告诉我使用它的最佳方法是什么。

Should I replace each Assert method with the following? 我应该用以下替换每个Assert方法吗?

NUnit.Framework.Assert.AreEqual(expectedResult, result);

As Jon Skeet mentions in his comment, you should only be using one test framework. 正如Jon Skeet在其评论中提到的那样,您应该只使用一个测试框架。 They will not play nicely together. 他们在一起玩不会很好。 If you want to use NUnit, you should, 如果您想使用NUnit,则应该

  1. Remove all of your Microsoft.VisualStudio.TestTools.UnitTesting using statements. 使用语句删除所有Microsoft.VisualStudio.TestTools.UnitTesting
  2. Remove the reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework from your project references. 从项目引用中删除对Microsoft.VisualStudio.QualityTools.UnitTestFramework的引用。

Your unit tests will only end up running under one of the test adapters, MSTest or NUnit. 您的单元测试将最终只能在测试适配器之一MSTest或NUnit下运行。 The asserts for each test framework throw different exceptions on assert failures and each test runner expects its own exceptions to be thrown. 每个测试框架的断言都会在断言失败时引发不同的异常,并且每个测试运行者都希望抛出自己的异常。 If you try to use an MSTest assert in your tests and your test fails, NUnit will not recognize the exception thrown as an assertion exception, so it will report it as an error as opposed to a test failure. 如果尝试在测试中使用MSTest断言而测试失败,则NUnit不会将抛出的异常识别为断言异常,因此它将报告为错误而不是测试失败。

You likely ended up with both test frameworks because you created a Unit Test Project in Visual Studio, then added NUnit to it. 您可能最终都使用了两个测试框架,因为您在Visual Studio中创建了一个单元测试项目 ,然后向其中添加了NUnit。 NUnit test projects should just be a regular Class Library . NUnit测试项目应该只是常规的类库 Another option is to install the NUnit Templates Extension for Visual Studio which will add an NUnit 3 Unit Test Project template to Visual Studio. 另一个选择是安装用于Visual StudioNUnit模板扩展,这会将一个NUnit 3单元测试项目模板添加到Visual Studio。

You can either decide to use NUnit or the built-in Visual Studio Unit Testing DLL. 您可以决定使用NUnit还是内置的Visual Studio单元测试DLL。

NUnit NUnit的
If you want to use NUnit, and it sounds like you already have the NUnit NuGet package installed, you simply comment out the line: 如果要使用NUnit,并且听起来好像已经安装了NUnit NuGet软件包,则只​​需在行中注释掉即可:

using Microsoft.VisualStudio.TestTools.UnitTesting;

If you want to really remove the native Visual Studio Unit Testing from your project, you could delete that line and remove your reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll . 如果要从项目中真正删除本机Visual Studio单元测试,则可以删除该行并删除对Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll的引用。 This should not be required, but it could prevent any of those "ambiguous reference" errors that could possibly occur. 这不是必需的,但是它可以防止可能发生的任何“歧义参考”错误。

Visual Studio Tools Visual Studio工具
If you wanted to use the Visual Studio native tools, however, you'd want to go to the NuGet Package Manager, find the NUnit package, and select to uninstall it and click OK on the prompt. 但是,如果要使用Visual Studio本机工具,则要转到NuGet程序包管理器,找到NUnit程序包,然后选择卸载它,然后在提示上单击“确定”。 It would proceed to remove the NUnit DLLs and dependencies. 它将继续删除NUnit DLL和依赖项。 You would remove the 您将删除

using NUnit.Framework;

line manually from the top of your Unit Test class. 从单元测试课的顶部手动排行。 You would need to ensure you have the Visual Studio Unit Testing DLL referenced. 您需要确保已引用了Visual Studio单元测试DLL。 If you don't have it already referenced, you'd need to go to References > Add References and browse to it. 如果尚未引用它,则需要转到参考>添加参考并浏览到它。 Its location can vary depending on the Visual Studio version you have. 它的位置可能因您使用的Visual Studio版本而异。 I answered a question on that, here: 我在这里回答了一个问题:

Where to find "Microsoft.VisualStudio.TestTools.UnitTesting" missing dll? 哪里可以找到缺少dll的“ Microsoft.VisualStudio.TestTools.UnitTesting”?

暂无
暂无

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

相关问题 与Microsoft.VisualStudio.TestTools.UnitTesting命名空间相关的错误 - Error related to Microsoft.VisualStudio.TestTools.UnitTesting namespace Microsoft.VisualStudio.TestTools.UnitTesting提供错误 - Microsoft.VisualStudio.TestTools.UnitTesting Giving Error 定义使用Microsoft.VisualStudio.TestTools.UnitTesting或NUnit时需要多少个断言 - Define how many asserts are expected when using Microsoft.VisualStudio.TestTools.UnitTesting or NUnit 使用Microsoft.VisualStudio.TestTools.UnitTesting编译错误 - using Microsoft.VisualStudio.TestTools.UnitTesting compile error Specflow 2.3.2:为什么Specflow使用“ Microsoft.VisualStudio.TestTools”而不是“ NUnit.Framework”生成* .cs文件 - Specflow 2.3.2: why is Specflow generating *.cs files using “Microsoft.VisualStudio.TestTools” instead of “NUnit.Framework” Microsoft.VisualStudio.TestTools.UnitTesting中的QueueBackgroundWorkItem - QueueBackgroundWorkItem from Microsoft.VisualStudio.TestTools.UnitTesting 无法在VS2013中创建测试,没有Microsoft.VisualStudio.TestTools.UnitTesting命名空间 - Can't create test in VS2013, no Microsoft.VisualStudio.TestTools.UnitTesting namespace Microsoft.VisualStudio.TestTools.UnitTesting.Assert泛型方法重载行为 - Microsoft.VisualStudio.TestTools.UnitTesting.Assert generic method overloads behavior 测试订单Microsoft.VisualStudio.TestTools.UnitTesting问题? - Test order Microsoft.VisualStudio.TestTools.UnitTesting issue? 哪里可以找到“Microsoft.VisualStudio.TestTools.UnitTesting”丢失的dll? - Where to find “Microsoft.VisualStudio.TestTools.UnitTesting” missing dll?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM