NUnit Assert 方法错误,因为它也在 NUnit.Framework 和 Microsoft.VisualStudio.TestTools.UnitTesting 命名空间中找到 [英] NUnit Assert method error as it is found in NUnit.Framework and Microsoft.VisualStudio.TestTools.UnitTesting namespace as well

查看:63
本文介绍了NUnit Assert 方法错误,因为它也在 NUnit.Framework 和 Microsoft.VisualStudio.TestTools.UnitTesting 命名空间中找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我同时使用两个语句

If I am using both statements at the same time

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

Assert 方法出现错误.有人可以告诉我使用它的最佳方式是什么.

I get an error for Assert method. Could someone tell me what is the best way of using it.

我是否应该将每个 Assert 方法替换为以下内容?

Should I replace each Assert method with the following?

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

推荐答案

正如 Jon Skeet 在他的评论中提到的,您应该只使用一个测试框架.他们不会很好地一起玩.如果你想使用 NUnit,你应该,

As Jon Skeet mentions in his comment, you should only be using one test framework. They will not play nicely together. If you want to use NUnit, you should,

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

您的单元测试最终只会在其中一种测试适配器 MSTest 或 NUnit 下运行.每个测试框架的断言在断言失败时抛出不同的异常,每个测试运行器都希望抛出自己的异常.如果您尝试在测试中使用 MSTest 断言并且测试失败,NUnit 不会将抛出的异常识别为断言异常,因此它会将其报告为错误而不是测试失败.

Your unit tests will only end up running under one of the test adapters, MSTest or 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.

您可能最终使用了这两个测试框架,因为您在 Visual Studio 中创建了一个单元测试项目,然后向其中添加了 NUnit.NUnit 测试项目应该只是一个常规的类库.另一种选择是安装 NUnit Templates Extension for Visual StudioNUnit 3 单元测试项目模板添加到 Visual Studio.

You likely ended up with both test frameworks because you created a Unit Test Project in Visual Studio, then added NUnit to it. NUnit test projects should just be a regular Class Library. 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.

这篇关于NUnit Assert 方法错误,因为它也在 NUnit.Framework 和 Microsoft.VisualStudio.TestTools.UnitTesting 命名空间中找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆