C# 单元测试、NUnit 还是 Visual Studio?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1554018/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:47:02  来源:igfitidea点击:

Unit test, NUnit or Visual studio?

c#visual-studiounit-testingnunit

提问by Tim

I'm using Visual studio (sometimes resharper) to run my unit test.

我正在使用 Visual Studio(有时是 resharper)来运行我的单元测试。

I heard about NUnit, but I don't know many things about it...

我听说过 NUnit,但我对它了解不多……

Should I care about it ? Can it offer something better than visual studio?

我应该关心它吗?它可以提供比视觉工作室更好的东西吗?

Should I Use NUnit and why?

我应该使用 NUnit,为什么?

采纳答案by Elisha

NUnit has few advantages over MS-Test

与 MS-Test 相比,NUnit 几乎没有什么优势

  1. Suite attribute - can aggregate tests and execute them separately (useful for large projects with fast and slow tests for example)
  2. Readable Assert method, e.g. Assert.AreEqual(expected, actual)vs Assert.That(actual, Is.EqualTo(expected))
  3. NUnit has frequent version updates - MS-Test has only one per VS version.
  4. Many integrated runners including Resharper and TestDriven.NET
  5. Expected exception message assertion - can be done using attribute in NUnit but must be done using Try-Catch in MS-Test
  6. [TestCase]! NUnit allows for parameter-ized tests.
  1. Suite 属性 - 可以聚合测试并单独执行它们(例如,对于具有快速和慢速测试的大型项目很有用)
  2. 可读断言方法,例如Assert.AreEqual(expected, actual)vsAssert.That(actual, Is.EqualTo(expected))
  3. NUnit 的版本更新频繁——MS-Test 每个 VS 版本只有一个。
  4. 许多集成的运行器,包括 Resharper 和 TestDriven.NET
  5. 预期异常消息断言 - 可以使用 NUnit 中的属性完成,但必须使用 MS-Test 中的 Try-Catch 完成
  6. [TestCase]!NUnit 允许参数化测试。

回答by Oliver Hanappi

NUnit is a unit testing framework, which is also supported by resharper. I think you are using Microsoft's unit testing framework, so NUnit is just an alternative to Microsoft's product ;)

NUnit 是一个单元测试框架,resharper 也支持。我认为您正在使用 Microsoft 的单元测试框架,因此 NUnit 只是 Microsoft 产品的替代品;)

Here is the link to the homepage of NUnit: http://nunit.org

这是 NUnit 主页的链接:http: //nunit.org

回答by bastijn

NUnit can be used in combinationwith visual studio. It is a framework not a separate program. So you could care an see if it suits you :).

NUnit 可以与 Visual Studio结合使用。它是一个框架而不是一个单独的程序。所以你可以关心看看它是否适合你:)。

alt text http://i3.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=nunitit&DownloadId=61802

替代文字 http://i3.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=nunitit&DownloadId=61802

"After installing the plugin you'll find a new submenu under the tools menu."

“安装插件后,您会在工具菜单下找到一个新的子菜单。”

See http://nunitit.codeplex.com/for more information on importing it.

有关导入它的更多信息,请参阅http://nunitit.codeplex.com/

Also, a lot can be found using the search of SO. This topiclists advantages of NUnit over MS standard testing for instance.

此外,使用 SO 的搜索可以找到很多。例如,本主题列出了 NUnit 相对于 MS 标准测试的优势。

回答by Vinay Pandey

I am not sure of others but NUnit provides nice GUI and console to run your Unit Tests and also you can generate report of the result of NUnit Test execution which would give the detail of whethere the test has failed or passed and also what time did it take for your unit test

我不确定其他人,但 NUnit 提供了很好的 GUI 和控制台来运行您的单元测试,您还可以生成 NUnit 测试执行结果的报告,该报告将提供测试是否失败或通过的详细信息,以及它的时间参加你的单元测试

回答by Per Erik Stendahl

NUnit works with the Standard edition of VS.

NUnit 与 VS 的标准版一起使用。

回答by flq

From my current perspective (after 8 months of development with about 10 developers on average) I would advise againstusing MSTest for the following reasons

从我目前的角度来看(经过 8 个月的开发,平均约有 10 名开发人员)我建议不要使用 MSTest,原因如下

  • The framework in itself is quite slow. I don't mean the test code that you write - that's under your control. I mean the framework running those tests is slow, whether it's running a test suite, single tests etc.
  • The need to keep a Test-Metadata file which always leads to complications when several developers are working on it (recreating e.g. the metadata etc.). Every other test suite doesn't need a metadata file. It is kind of nice to organize your tests but you can achieve the same through namespaces, classes and method names.
  • Doing Continuous Integration, if you want to run unit tests on your build machine you will need to install Visual Studio on that machine.
  • 框架本身很慢。我不是指您编写的测试代码 - 这在您的控制之下。我的意思是运行这些测试的框架很慢,无论是运行测试套件、单个测试等。
  • 需要保留一个测试元数据文件,这在几个开发人员正在处理它时总是会导致复杂化(重新创建例如元数据等)。每个其他测试套件都不需要元数据文件。组织您的测试是一种很好的方式,但是您可以通过命名空间、类和方法名称来实现相同的目的。
  • 做持续集成,如果你想在你的构建机器上运行单元测试,你需要在该机器上安装 Visual Studio。

In other words, if I would have to decide again 8 months ago, I would probably take NUnit. I may not have the integrated test results report, but developers would have a more seamless testing experience.

换句话说,如果我必须在 8 个月前再次决定,我可能会选择 NUnit。我可能没有集成的测试结果报告,但开发人员会有更无缝的测试体验。

回答by Stefan Steinegger

Here is my experience with MS Test

这是我使用 MS Test 的经验

  • We are running MS Test with around 3800 Test.
  • It takes very long for the tests just to start executing, which is painful when running single tests.
  • It takes around 1GB Memory to execute the tests. No, it is not due to memory leaks in our tests. Frequently we run into OutOfMemoryExceptions.
  • Because it uses that much resource, we are starting to execute the tests from batch-files. So what's the whole integration good for?
  • It is buggy and unstable:
    • For instance, if you remove the [Ignore] Attribute from a test, it does not recognize it, because it caches information about tests somewhere. You need to refresh the testlist, which sometimes solves the problem, or restart VS.
    • It randomly does not copy reference assemblies to theout directory.
    • Deployment Items (additional files to be used) just don't work properly. They are ignored randomly.
  • There is hidden (not visible in the test code) information in vsmdi and testrunconfig files. If you don't care about it, it might not work.
  • Functionally it might be comparable to NUnit, but it is very expensive if you consider using VS tester edition.
  • 我们正在使用大约 3800 测试运行 MS 测试。
  • 测试开始执行需要很长时间,这在运行单个测试时很痛苦。
  • 执行测试需要大约 1GB 内存。不,这不是由于我们测试中的内存泄漏。我们经常遇到 OutOfMemoryExceptions。
  • 因为它使用了那么多资源,我们开始从批处理文件中执行测试。那么整个集成有什么好处呢?
  • 它有缺陷且不稳定:
    • 例如,如果您从测试中删除 [Ignore] 属性,它不会识别它,因为它会将有关测试的信息缓存在某处。需要刷新testlist,有时可以解决问题,或者重启VS。
    • 它不会随机将引用程序集复制到 out 目录。
    • 部署项目(要使用的其他文件)无法正常工作。它们被随机忽略。
  • vsmdi 和 testrunco​​nfig 文件中有隐藏(在测试代码中不可见)信息。如果你不关心它,它可能不起作用。
  • 在功能上它可能与 NUnit 相当,但如果您考虑使用 VS 测试版,它会非常昂贵。


Addition:We have some more tests now, can't even say how many. It is impossible to run them all anymore from Visual Studio, because of OutOfMemoryExceptions and other instability problems. We run the tests from scripts. It would be easy to view test results in Visual Studio, but when the solution is open, VS crashes (every time). So we need to search the failing tests using text search. There is no advantage of an integrated tool anymore.

补充:我们现在还有一些测试,甚至不能说有多少。由于 OutOfMemoryExceptions 和其他不稳定问题,不可能再从 Visual Studio 运行它们。我们从脚本运行测试。在 Visual Studio 中查看测试结果很容易,但是当解决方案打开时,VS 崩溃(每次)。所以我们需要使用文本搜索来搜索失败的测试。集成工具不再具有优势。



Another Update: We are using VS 2013 now. A lot of things changed. They rewrote the MS Test test runner for the third time since we started. This caused a lot of breaking changes, but neither new version was doing anything better. We are glad that we didn't use the fancy features of MS Test, because they are all not supported anymore. It's really a shame. We are still using scripts to build and run all unit tests, because it is handier. Visual Studio required a few minutes to start running tests (time measures after compilation until first test starts). They probably fixes it with an update and this might be a specific problem of our project. However, Resharper is much quicker when running the same tests.

另一个更新:我们现在使用 VS 2013。很多事情都变了。自我们开始以来,他们第三次重写了 MS Test 测试运行程序。这导致了许多重大更改,但两个新版本都没有做得更好。我们很高兴我们没有使用 MS Test 的花哨功能,因为它们都不再受支持。真是太可惜了。我们仍然使用脚本来构建和运行所有单元测试,因为它更方便。Visual Studio 需要几分钟来开始运行测试(从编译到第一个测试开始的时间度量)。他们可能会通过更新修复它,这可能是我们项目的一个特定问题。但是,在运行相同的测试时,Resharper 要快得多。

Conclusion: At least in combination with Resharper, MS Test is useful. And I hope that they finally find out how the test runner should be written and won't do this kind of breaking changes when we update Visual Studio next time.

结论:至少与 Resharper 结合使用,MS Test 是有用的。并且我希望他们最终找到应该如何编写测试运行程序,并且在我们下次更新 Visual Studio 时不会做这种破坏性的更改。

回答by Maciej

Biggest advantage MS-Test over NUnit is MS-Test can generate mock objects using Reflection. I found it very usefull

MS-Test 相对于 NUnit 的最大优势是 MS-Test 可以使用反射生成模拟对象。我发现它非常有用

回答by Nipuna Prashan

If you are using Visual Studio You have to Use NUnit for doing unit test, and if you are running java(Netbeans) you have to use JUnit for unit test.

如果您使用的是 Visual Studio,则必须使用 NUnit 进行单元测试,如果您正在运行 java(Netbeans),则必须使用 JUnit 进行单元测试。

Here is an example for a simple calculator unit test

这是一个简单的计算器单元测试的例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimpleCalculator;
using NUnit.Framework;

namespace CalculatorTest
{
    [TestFixture]
    public class Class1
    {
        public Calculator _calculator;
        [TestFixtureSetUp]
        public void Initialize()
        {
            _calculator = new Calculator();
        }
        [Test]
        public void DivideTest()
        {
            int a = 10;
            int b = 2;
            int expectedValue = a / b;
            int actualvalue = _calculator.Divide(a, b);
            Assert.AreEqual(expectedValue, actualvalue,"Failure");

        }
    }
}

回答by Mourad Samy

In NUnit, tests are not executed in parallel. Rather, it appears that all tests execute on a single thread. In MSTest, each test is instantiated on a separate thread, this results the runs being interleaved. Therefore, if test A depends on test B for its success, it likely will fail as test B will likely start running as test A is running.

NUnit 中,测试不是并行执行的。相反,似乎所有测试都在单个线程上执行。在 MSTest 中,每个测试都在单独的线程上实例化,这导致运行交错。因此,如果测试 A 的成功依赖于测试 B,则它可能会失败,因为测试 B 可能会在测试 A 运行时开始运行。