C# NUnit 与 xUnit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9769047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NUnit vs. xUnit
提问by Ruslan
What are the differences between NUnitand xUnit.net? What's the point of developing two of them, not only one?
NUnit和xUnit.net之间有什么区别?开发其中的两个,而不是一个,有什么意义?
I've read that xUnit is being developed by inventor of NUnit:
我读到 xUnit 是由 NUnit 的发明者开发的:
xUnit.net is a unit testing tool for the .NET Framework. Written bythe original inventor of NUnit
xUnit.net 是 .NET Framework 的单元测试工具。由NUnit的原始发明者编写
On the other hand:
另一方面:
NUnit is a unit-testing framework for all .Net languages .. the current production release, version 2.6, is the seventh major release of this xUnit based unit testing tool
NUnit 是适用于所有 .Net 语言的单元测试框架 .. 当前的生产版本 2.6 是这个基于 xUnit 的单元测试工具的第七个主要版本
So where is the truth?
那么真相在哪里呢?
回答by Joey
回答by TakinosaJi
xUnit Pros:
xUnit 优点:
xUnit follows a new concept by avoiding the old "SetUp" and "TearDown" methods. It forces us to use IDisposable and a constructor as we should do as .NET developers. Also xUnit has a clear context sharing concept.
xUnit 通过避免旧的“SetUp”和“TearDown”方法遵循一个新概念。它迫使我们像 .NET 开发人员一样使用 IDisposable 和构造函数。xUnit 也有明确的上下文共享概念。
xUnit Cons:
xUnit 缺点:
Availability to get the test context is not implemented yet.
尚未实现获取测试上下文的可用性。
回答by Rad
One benefit of xUnit is that it finds tests in different classes; it runs them in parallel. This can save a lot of time if you have many test cases.
xUnit 的一个好处是它可以在不同的类中找到测试;它并行运行它们。如果您有很多测试用例,这可以节省大量时间。
You can of course turn this off, or control its operation (number of threads, threads per class, tests per assembly, etc.
您当然可以关闭它,或控制它的操作(线程数、每个类的线程、每个程序集的测试等。
Check out this sample solution with two test projects, one using xUnit the other NUnit.
通过两个测试项目查看此示例解决方案,一个使用 xUnit 另一个使用 NUnit。
You can read more about parallel tests in xUnit here.
您可以在此处阅读有关 xUnit 中并行测试的更多信息。
回答by akazemis
At the time of writing this answer the latest NUnit version is v3.5 and xUnit.net is v2.1.
在撰写此答案时,最新的 NUnit 版本是 v3.5,xUnit.net 是 v2.1。
Both of the frameworks are awesome, and they both support parallel test running (in a different way though). NUnithas been around since 2002, it's widely used, well documented and has a large community, whereas xUnit.netis more modern, more TDD adherent, more extensible, and also trending in .NET Core development. It's also well documented.
这两个框架都很棒,而且都支持并行测试运行(虽然方式不同)。NUnit自 2002 年以来一直存在,它被广泛使用、有据可查并且拥有庞大的社区,而xUnit.net更现代、更符合 TDD、可扩展性更强,并且也是 .NET Core 开发的趋势。它也有据可查。
In addition to that, the main difference I noticed is the way that xUnit.net runs the test methods. So, in NUnit, we've got a test class and a set of test methods in it. NUnit creates a new instance of the test class and then runs all of the tests method from the same instance. Whereas xUnit.net creates a new instance of the test class for very each of the test methods.. Therefore, one cannot use fields or properties to share data among test methods which is a bad practice, as our test methods would be dependent to each other which is not acceptable in TDD. So if you use xunit.net you could be sure that your test methods are completely isolated.
除此之外,我注意到的主要区别是 xUnit.net 运行测试方法的方式。因此,在NUnit 中,我们有一个测试类和一组测试方法。NUnit 创建测试类的新实例,然后从同一实例运行所有测试方法。而xUnit.net 为每个测试方法创建了一个新的测试类实例。. 因此,不能使用字段或属性在测试方法之间共享数据,这是一种不好的做法,因为我们的测试方法将相互依赖,这在 TDD 中是不可接受的。因此,如果您使用 xunit.net,您可以确保您的测试方法是完全隔离的。
If you're willing to share some data among your test methods though, xUnit will let you do so. Therefore, by default all test methods are completely isolated, but you can break this isolation in specific cases intentionally. I fancy this attitude, that's why I like it better.
如果您愿意在您的测试方法之间共享一些数据,xUnit 将允许您这样做。因此,默认情况下所有测试方法都是完全隔离的,但您可以在特定情况下有意打破这种隔离。我喜欢这种态度,这就是为什么我更喜欢它。

