简体   繁体   English

单元测试,NUnit或Visual studio?

[英]Unit test, NUnit or Visual studio?

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吗?为什么?

NUnit has few advantages over MS-Test NUnit与MS-Test相比几乎没有什么优势

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

From my current perspective (after 8 months of development with about 10 developers on average) I would advise against using 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 eg the metadata etc.). 需要保留一个Test-Metadata文件,当几个开发人员正在处理它时,它总是会导致复杂化(重新创建例如元数据等)。 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. 换句话说,如果我必须在8个月前再次决定,我可能会采取NUnit。 I may not have the integrated test results report, but developers would have a more seamless testing experience. 我可能没有集成的测试结果报告,但开发人员可以获得更加无缝的测试体验。

Here is my experience with MS Test 这是我对MS Test的体验

  • We are running MS Test with around 3800 Test. 我们正在运行MS测试,大约3800测试。
  • 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. 执行测试需要大约1GB的内存。 No, it is not due to memory leaks in our tests. 不,这不是由于我们的测试中的内存泄漏。 Frequently we run into OutOfMemoryExceptions. 我们经常遇到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. 例如,如果从测试中删除[Ignore]属性,则它无法识别它,因为它会在某处缓存有关测试的信息。 You need to refresh the testlist, which sometimes solves the problem, or restart VS. 您需要刷新测试列表,有时可以解决问题,或重新启动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. 在vsmdi和testrunco​​nfig文件中隐藏(在测试代码中不可见)信息。 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. 在功能上它可能与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. 由于OutOfMemoryExceptions和其他不稳定性问题,不可能再从Visual Studio中运行它们。 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). 在Visual Studio中查看测试结果会很容易,但是当解决方案打开时,VS会崩溃(每次)。 So we need to search the failing tests using text search. 所以我们需要使用文本搜索来搜索失败的测试。 There is no advantage of an integrated tool anymore. 再也没有集成工具的优势。


Another Update : We are using VS 2013 now. 另一个更新 :我们现在正在使用VS 2013。 A lot of things changed. 很多事情发生了变化。 They rewrote the MS Test test runner for the third time since we started. 自从我们开始以来,他们第三次重写了MS Test测试运行器。 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. 我们很高兴我们没有使用MS Test的精彩功能,因为它们都不再受支持了。 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). Visual Studio需要几分钟才能开始运行测试(编译后的时间测量,直到第一次测试开始)。 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. 但是,Resharper在运行相同测试时要快得多。

Conclusion : At least in combination with Resharper, MS Test is useful. 结论 :至少与Resharper结合使用,MS Test非常有用。 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. 我希望他们最终能够找到如何编写测试运行器,并且在下次更新Visual Studio时不会进行这种重大更改。

NUnit can be used in combination with visual studio. NUnit可与visual studio 结合使用。 It is a framework not a separate program. 它是一个框架而不是一个单独的程序。 So you could care an see if it suits you :). 所以你可以关心看看它是否适合你:)。

alt text http://i3.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=nunitit&DownloadId=61802 alt text 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. 此外,使用搜索SO可以找到很多。 This topic lists advantages of NUnit over MS standard testing for instance. 本主题列出了NUnit优于MS标准测试的优势。

Biggest advantage MS-Test over NUnit is MS-Test can generate mock objects using Reflection. NUnit的最大优势MS-Test是MS-Test可以使用Reflection生成模拟对象。 I found it very usefull 我发现它非常有用

NUnit适用于VS的标准版。

NUnit is a unit testing framework, which is also supported by resharper. NUnit是一个单元测试框架,resharper也支持它。 I think you are using Microsoft's unit testing framework, so NUnit is just an alternative to Microsoft's product ;) 我认为您使用的是Microsoft的单元测试框架,因此NUnit只是Microsoft产品的替代品;)

Here is the link to the homepage of NUnit: http://nunit.org 这是NUnit主页的链接: http//nunit.org

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

In NUnit , tests are not executed in parallel. NUnit中 ,测试不是并行执行的。 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. 在MSTest中,每个测试都在一个单独的线程上实例化,这会导致运行交错。 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. 因此,如果测试A依赖于测试B的成功,它可能会失败,因为测试B可能会在测试A运行时开始运行。

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");

        }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM