有什么我可以在 NUnit 中做但在 MSTest 中不能做的事情吗?

发布于 2024-08-06 05:06:22 字数 459 浏览 2 评论 0原文

这个问题已经在许多不同的论坛上以各种形式提出,但是,恕我直言,我一直无法找到一个真正明确回答这个问题的地方,所以我将重新构建它并再次询问它。

我基本上在一家微软商店工作。我们使用 TFS,并且所有开发人员都订阅了 MSDN,包括 VS 的 Team Suite 版本。这样我们就可以访问MSTest了。

我阅读了各种 NUnit 与 MSTest 的比较,开发人员社区似乎绝大多数选择了 NUnit。但给出的理由似乎从来都不是压倒性的或令人信服的,至少对于我们的情况来说是这样。 (NUnit 更新更频繁,NUnit 更快,NUnit 不需要 TFS 等)

如果我选择,我可以使用 NUnit,但必须捍卫在没有正式支持的情况下使用开源软件的行为。我需要一个相当令人信服的理由才能这样做。

为了证明使用 NUnit 而不是 MSTest 的合理性,我基本上必须回答的是:有什么是我在 NUnit 中可以做而在 MSTest 中无法做的事情吗?

This question has been asked in various forms in a number of different forums, but, IMHO, I haven't been able to find a place where it's really answered clearly, so I'm going to reframe it and ask it again.

I work in a basically Microsoft Shop. We use TFS, and all of our developers have MSDN subscriptions including the Team Suite edition of VS. So we have access to MSTest.

I've read the various NUnit vs. MSTest comparisons, and the developer community seems to pretty much overwhelmingly choose NUnit. But the reasons given don't ever seem to be overwhelming or compelling, at least to our situation. (NUnit is updated more often, NUnit is faster, NUnit doesn't require TFS, etc.)

I can use NUnit if I choose, but the use of open source software without a formal support behind it has to be defended. I need a fairly compelling reason to do so.

What I basically have to answer to justify using NUnit in preference to MSTest is this: is there anything that I can do in NUnit that I can't do with comparable effort in MSTest?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

烟凡古楼 2024-08-13 05:06:22
  • NUnit 包含一个 [TestCase] 属性,允许实现参数化测试。这在 MSTest 中并不是开箱即用的 - 但可以通过可扩展性来完成。
  • MsTest 的 ExpectedException 属性有一个错误,即预期消息永远不会真正断言,即使它是错误的 - 测试将通过。
  • NUnit 附带一个 Assert.Throws API,允许在特定代码行而不是整个方法上测试异常。 MSTest 也有类似的功能(由为 NUnit 实现的同一个人实现),但不随 MSTest 一起提供。
  • NUnit 包含开箱即用的断言 API 的流畅版本。 MSTest 有第三方扩展可以执行此操作,但没有随 MSTest 一起提供。
  • NUnit 允许抽象类作为测试装置(因此您可以继承测试装置)。 MsTest 允许这样做,但将抽象类限制为单个程序集。
  • NUnit 允许非公共类作为测试装置(从最新版本开始)
  • NUnit 是专门为了单元测试的想法而创建的。 MSTest 是为测试而创建的 - 也是一些单元测试。
  • NUnit 包含 PNunit(与 NUnit 运行并行测试)。 MSTest 在 Visual Studio 2010 中添加了此功能,可通过 XML 配置
  • NUnit contains a [TestCase] attribute that allows implementing parametrized tests. This does not exist out of the box in MSTest - it can be done via extensibility though.
  • MsTest's ExpectedException attribute has a bug where the expected message is never really asserted even if it's wrong - the test will pass.
  • NUnit ships with an Assert.Throws API to allow testing an exception on a specific line of code instead of the whole method. A similar feature exists for MSTest (implemented by the same person who did it for NUnit) but does not ship with MSTest.
  • NUnit contains a fluent version of Assert API out of the box. MSTest has third party extensions that do this, but none are shipped with MSTest.
  • NUnit allows abstract classes to be test fixtures (so you can inherit test fixtures). MsTest allows this but limits abstract classes to a single assembly.
  • NUnit allows non public classes to be test fixtures (as of the latest version)
  • NUnit was created SOLELY for the idea of unit testing. MSTest was created for Testing - and also a bit of unit testing.
  • NUnit contains PNunit (running parallel tests with NUnit). MSTest added this ability in Visual Studio 2010 which is configurable via XML
北风几吹夏 2024-08-13 05:06:22

Roy,你的一些信息已经过时了,尤其是与 2010 年相关的信息;

<块引用>

Nunit 包含一个 [TestCase] 属性,允许实现参数化测试。这在 MSTest 中不存在

这可以使用 2010 年的单元测试扩展性来实现。

<块引用>

MSTest 的 ExpectedException 属性有一个错误,即预期消息永远不会真正断言,即使它是错误的 - 测试将通过。

正确那仍然存在

<块引用>

NUnit 有一个 Assert.Throws API,允许在特定代码行而不是整个方法上测试异常(不过您可以自己轻松实现这个)

Jim 在执行 MSTest 的同时为 MSTest 实现了 Assert.Throws 的一个版本NUnit 的原始实现,NUnit 已包含在后续版本中,MSTest 尚未包含,但仍然可以使用。

<块引用>

NUnit 包含 Assert API 的流畅版本(如前所述 - Assert.That..)

其中有几个由第三方为 MSTest 实现

<块引用>

NUnit 更快

请参阅 Jamie 的评论,他已设法使 MSTest 运行得更快:-)

<块引用>

NUnit 可以在 32 位和 64 位中运行测试(MSTest 仅在 32 位 IIRC 中运行它们)

不是在 2010 年,内置了 64 位支持。

<块引用>

NUnit 允许抽象类作为测试装置(因此您可以继承测试装置)。 MsTest 没有。

这可以工作,但不能跨程序集,这确实限制了它的实用性。

<块引用>

NUnit 允许非公共类作为测试装置(从最新版本开始)

仍然存在

<块引用>

NUnit 是专门为了单元测试的想法而创建的。 MSTest 是为测试而创建的 - 也是一些单元测试。

正确的是,有很多误解认为 MSTest 与 Nunit 相同,但 MSTest 是一个通用框架。

<块引用>

NUnit 包含 PNunit(与 NUnit 运行并行测试)。 MSTest只是在vs 2010中添加了这个能力

正确的是,有一个 XML 配置设置允许控制并行度。

Roy, Bunch of your information is out of date especially as it relates to 2010;

Nunit contains a [TestCase] attribute that allows implementing parametrized tests. this does not exist in MSTest

This can be implemented using unit test extensibility in 2010.

MSTest's ExpectedException attribute has a bug where the expected message is never really asserted even if it's wrong - the test will pass.

Correct thats still there

NUnit has an Assert.Throws API to allow testing an exception on a specific line of code instead of the whole method (you can easily implement this one yourself though)

Jim implented a version of Assert.Throws for MSTest at the same time as he did the original implementation for NUnit, NUnit has included in subsequent releases, MSTest has not, its still possible to use though.

NUnit contains a fluent version of Assert API (as already mentioned - Assert.That..)

There are several of these implemented by 3rd parties for MSTest

NUnit is much faster

See Jamie's comment he has managed to get MSTest running faster :-)

NUnit can run tests in 32 and 64 bit (MSTest only runs them in 32 bit IIRC)

Not in 2010, 64 bit support is built in.

NUnit allows abstract classes to be test fixtures (so you can inherit test fixtures). MsTest does not.

This works but not across assemblies which does limit its usefulness.

NUnit allows non public classes to be test fixtures (as of the latest version)

Still there

NUnit was created SOLELY for the idea of unit testing. MSTest was created for Testing - and also a bit of unit testing.

Correct, there is a lot of misconception that MSTest is the same as Nunit but MSTest is a generalized framework.

NUnit contains PNunit (running parallel tests with NUnit). MSTest only adds this ability in vs 2010

Correct there is an XML config setting that allows control of degree of parallelism.

相守太难 2024-08-13 05:06:22

NUnit 有一个更丰富的断言 API。该 API 特别优雅(甚至流畅),例如,

Assert.That(Is.Unique, myResults);  // assert: myResults is a collection of unique items

如果您看过 Hamcrest< /a> JUnit 的扩展,您会认出这种风格。

它还具有不断增长的扩展集,例如性能测试和优秀的 VS 插件

NUnit has a richer assert API. The api is particularly elegant (fluent, even), for example

Assert.That(Is.Unique, myResults);  // assert: myResults is a collection of unique items

If you've seen the Hamcrest extensions to JUnit you'll recognise this style.

It also has a growing set of extensions, such as performance testing and an excellent VS plugin.

月亮邮递员 2024-08-13 05:06:22

我可以向您推荐一些有关 MSTest 的挫败感的博客:

公平地说,这些人正在尝试在非 TFS 构建服务器上设置 MSTest 。他们的一些问题不适用于您的情况。

我们主要是一家 Microsoft 商店,并使用 TFS 进行源代码控制。但是,我们使用 TeamCity 进行持续集成;我们喜欢它,并且它与 TFS 集成得相当好。我从未使用过 MSTest;我们已经使用 NUnit 多年了,并且没有看到任何改变的理由。

MSTest 应该与 Team Suite 紧密集成,这(因为您的公司已经为此支付了高昂的费用)是对其有利的一点。

NUnit 的供应商锁定较少,并且具有丰富的 API。正如 serg10 所指出的,Assert.That 语法特别强大且优雅。

最后,您可以编写良好的单元测试,而无需所有花哨的功能。其中一些甚至可能会妨碍(这是 xUnit.net 背后的理论)。我建议您的团队标准化一个测试框架;避免一些代码在 MSTest 中,而其他代码在 NUnit 中。

我认为编写好的测试比选择框架更重要。考虑阅读单元测试的艺术:.NET 中的示例,编写一些测试,然后看看 MSTest 是否足以满足您团队的需求。

编辑:《单元测试的艺术》附录 B 对 Microsoft 的单元测试框架有一些很好的评论。它提到 YUnit 作为扩展 MSTest 有多么麻烦的示例。然而,由于紧密集成,作者确实建议 Team System 用户使用 MSTest。

I can point you to a couple of blogs on frustrations with MSTest:

To be fair, these people are trying to set up MSTest on non-TFS build servers. Some of their issues are not going to apply to your situation.

We are primarily a Microsoft shop, and use TFS for source control. However, we use TeamCity for Continuous Integration; we like it, and it integrates reasonably well with TFS. I've never used MSTest; we've been using NUnit for years, and have seen no reason to change.

MSTest is supposed to have tight integration with Team Suite, which (since your company has already paid the outrageous fee for that) is a point in its favor.

NUnit comes with less vendor lock-in, and has a rich API. As serg10 pointed out, the Assert.That syntax is particularly powerful and elegant.

In the end, you can write good unit tests without all the fancy features. Some of them may even get in the way (which is the theory behind xUnit.net). I would recommend that your team standardize on one test framework; avoid having some code in MSTest and other code in NUnit.

I think writing good tests is more important than your choice of frameworks. Consider reading The Art of Unit Testing: with Examples in .NET, writing some tests, then seeing if MSTest is adequate for your team's needs.

EDIT: Appendix B of The Art of Unit Testing has some good comments on Microsoft's Unit Testing Framework. It mentions YUnit as an example of how cumbersome it is to extend MSTest. However, the author does suggest MSTest for Team System users due to the tight integration.

能怎样 2024-08-13 05:06:22

我在 MsTest 和 NUnit 之间有一个很好的方法。
您可以使用 MSTest 框架来运行测试(每个测试的 TestClass 属性和 TestMethod 属性),但使用 NUnit Assert API。

只需这样做:

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

现在您可以使用 Assert.That(..) 并且仍然拥有 TFS 自动构建和测试报告。
你的测试可以在VS、TestDriven.net或Resharper中运行,没关系,测试会失败或正确通过,失败输出将根据NUnit框架。

参见这里:
http:// /alsagile.com/archive/2010/03/09/stop-the-war- Between-nunit-and-mstest-make-them.aspx

I have a nice way in between MsTest and NUnit.
You can use the MSTest framework to run your test (TestClass attribute and TestMethod attribute for each test) but use the NUnit Assert API.

just do this :

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

now you can use Assert.That(..) and still have TFS automated build and test report.
your test can be run in VS, TestDriven.net or Resharper, it doesnt matter, the test will fail or pass correctly, and the fail output will be according to NUnit framework.

see here :
http://alsagile.com/archive/2010/03/09/stop-the-war-between-nunit-and-mstest-make-them.aspx

故事与诗 2024-08-13 05:06:22
  1. MSTest 测试运行程序是非确定性的,这足以让您不敢使用它。我无法使用集成测试运行程序从 TFS 2010 持续运行 MSTest;它会在项目构建和构建代理之间出现一些不同的错误消息(并且不一致)。
  2. MSTest 有时(并非始终)会因为泄漏内存而中断 - 即使使用最新版本的 Visual Studio,内存不足异常仍然会发生。这个问题的解决方法很糟糕。
  3. 我对 MSTest 还有其他一些小问题,并且在博客中写了一些一般使用 MSTest 的解决方法: http: //www.pseale.com/blog/TFSAsYourBuildCIServerOnlyPositiveTakeaways1Of2.aspx

我在将我们从 MSTest 切换到 NUnit 时发现了这个问题,因为由于 MSTest,我们无法再信任 CI 构建的结果。切换到 NUnit 将使我们(最终)相信 CI 构建失败是真实的,而不是另一个 MSTest 故障。这是真正的答案——只有使用 NUnit(或其他一些非 MSTest 测试运行程序)我们才能信任我们的 CI 构建

  1. The MSTest test runner is non-deterministic, which should be enough to scare you away from using it. I cannot consistently run MSTest from TFS 2010 using the integrated test runner; it breaks with a few different error messages (and, inconsistently) across project builds and across build agents.
  2. MSTest sometimes (not consistently) breaks because it leaks memory--out of memory exceptions still happen for us, even with the newest version of Visual Studio. The workaround for this problem is horrible.
  3. I have other minor quibbles with MSTest, and have blogged a bunch of workarounds for using MSTest in general: http://www.pseale.com/blog/TFSAsYourBuildCIServerOnlyPositiveTakeaways1Of2.aspx

I found this question as I am switching us to NUnit from MSTest because we can't trust the result of our CI build anymore because of MSTest. Switching to NUnit will allow us to (finally) trust that a CI build failure is real, not another MSTest glitch. This is the real answer--only with NUnit (or some other non-MSTest test runner) can we trust our CI build.

缪败 2024-08-13 05:06:22

请仔细阅读 ExpectedExceptionAttribute 文档,没有任何地方说明测试异常消息,因此这不是未断言的错误。

第二个参数是未抛出预期异常类型时显示的断言消息,而不是预期异常消息。即像 Assert.IsTrue(condition, message) 中的第二个参数一样。

Please read ExpectedExceptionAttribute documentation carefully, it is nowhere stated that tests the exception message so it's not a bug that is not asserted.

The second parameter is the assert message what is displayed when expected exception type is not thrown, not the expected exception message. I.e. like the second parameter in Assert.IsTrue(condition, message).

沉鱼一梦 2024-08-13 05:06:22

我一直致力于 TestDriven.Net 3.0 中对 MSTest 的一流支持。在以前的版本中,MSTest 支持非常基本(似乎很少有人使用 MSTest)。

从 TestDriven.Net 3.0 Beta 2 开始,对 MSTest 的所有单元测试相关属性都有相当全面的支持。甚至还支持使用 DataSource 属性的数据驱动测试。您的测试也将以接近 NUnit 的速度执行!

如果您使用 MSTest,我很想知道在使用 TestDriven.Net 3.0 Beta 2(或更高版本)执行时是否有任何单元测试失败。

请在您的 MSTest 项目中尝试一下,并让我知道您的进展情况:
http://www.testdriven.net/download.aspx

注意,它不支持DeploymentItem 或 HostType 属性。您可以使用“复制到输出目录”项目项设置而不是 DeploymentItem。

I've been working on first class support for MSTest in TestDriven.Net 3.0. In previous versions the MSTest support was very basic (there seemed to be few people using MSTest).

As of TestDriven.Net 3.0 Beta 2, there is pretty comprehensive support for all of the unit testing related attributes of MSTest. There is even support for data driven tests using the DataSource attribute. Your tests will also execute at close to NUnit speeds!

If you use MSTest, I'd be interested to hear if any of your unit tests fail when executed using TestDriven.Net 3.0 Beta 2 (or later).

Please try it on your MSTest projects and let me know how you get on:
http://www.testdriven.net/download.aspx

NOTE, it doesn't support the DeploymentItem or HostType attributes. You can use the 'Copy to Output Directory' project item setting instead of DeploymentItem.

心的位置 2024-08-13 05:06:22

您可以更改 NUnit 中的代码,因为它是开源的。 MSTest 无法做到这一点。

You can change the code in NUnit because it is open source. You can't do that with MSTest.

埖埖迣鎅 2024-08-13 05:06:22

请参阅 http:// Fluentassertions.codeplex.com。你可以做类似的事情

"ABCDEFGHI".Should().StartWith("AB").And.EndWith("HI").And.Contain("EF").And.HaveLength(9);

new[] { 1, 2, 3 }.Should().HaveCount(4, "because we thought we put three items in the 
collection"))

dtoCollection.Should().Contain(dto => dto.Id != null);

collection.Should().HaveCount(c => c >= 3);

dto.ShouldHave().AllPropertiesBut(d => d.Id).EqualTo(customer);

dt1.Should().BeWithin(TimeSpan.FromHours(50)).Before(dt2); 

Action action = () => recipe.AddIngredient("Milk", 100, Unit.Spoon);
action
   .ShouldThrow<RuleViolationException>()
   .WithMessage("Cannot change the unit of an existing ingredient")
   .And.Violations.Should().Contain(BusinessRule.CannotChangeIngredientQuanity

See http://fluentassertions.codeplex.com. You can do stuff like

"ABCDEFGHI".Should().StartWith("AB").And.EndWith("HI").And.Contain("EF").And.HaveLength(9);

new[] { 1, 2, 3 }.Should().HaveCount(4, "because we thought we put three items in the 
collection"))

dtoCollection.Should().Contain(dto => dto.Id != null);

collection.Should().HaveCount(c => c >= 3);

dto.ShouldHave().AllPropertiesBut(d => d.Id).EqualTo(customer);

dt1.Should().BeWithin(TimeSpan.FromHours(50)).Before(dt2); 

Action action = () => recipe.AddIngredient("Milk", 100, Unit.Spoon);
action
   .ShouldThrow<RuleViolationException>()
   .WithMessage("Cannot change the unit of an existing ingredient")
   .And.Violations.Should().Contain(BusinessRule.CannotChangeIngredientQuanity
日久见人心 2024-08-13 05:06:22

@戴夫·汉纳
MS Test 开箱即用,因此减少了一个需要担心部署和保持更新的组件。
它还在设计上与 Visual Studio 和其他 Microsoft 产品集成。

流畅的语法很好,但它不是功能上的差异,所以我会忽略它。更不用说您可以通过使用扩展库在 MS Test 中拥有相同的功能。

表现。 99% 的测试性能由被测系统控制,因此即使两个库之间有 100% 的性能差异,也会导致性能的总体差异可以忽略不计。

开源与非开源:您从使用开源库中受益的机会很小。例外情况是当更改经常被拉入主干或有足够的资源来保持修改的分支更新时。

@Dave Hanna
MS Test is available out of the box so one less component to worry about deploying and keeping updated.
It is also integrated with Visual Studio and other Microsoft products by design.

The fluent syntax is nice but it's not a functional difference so I'd ignore it. Not to mention that you can have the same in MS Test by using an extension library.

Performance. 99% of test performance is controlled by system under test, so even a 100% performance difference between two library would result in an ignorable overall difference in performance.

Open source vs. not open source: the chances that you'll benefit from using an open source library are minimal. The exceptions are when changes are pulled into trunk frequently or when there are enough resources to keep the modified branch updated.

紫竹語嫣☆ 2024-08-13 05:06:22

您可以非常轻松地使用 Mono 在 Linux 上运行 NUnit 测试,但我认为您不能通过 MSTest 测试来做到这一点。

You can very easily run NUnit tests on Linux with Mono and I don't think you can do that with tests for MSTest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文