使用 Visual Studio 在 C# 中进行单元测试入门
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/497118/
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
Getting Started with Unit Testing in C# with Visual Studio
提问by Malfist
I know Visual Studio offers some Unit Testing goodies. How do I use them, how do you use them? What should I know about unit testing (assume I know nothing).
我知道 Visual Studio 提供了一些单元测试的好东西。我如何使用它们,你如何使用它们?关于单元测试我应该知道些什么(假设我一无所知)。
This questionis similar but it does not address what Visual Studio can do, please do not mark this as a duplicate because of that. Posted as Community Wiki because I'm not trying to be a rep whore.
这个问题很相似,但它没有解决 Visual Studio 可以做什么,因此请不要将其标记为重复。发布为社区维基,因为我不想成为一名代表妓女。
回答by Eric
The unit testing structure in VS is similar to NUnit in it's usage. One interesting (and useful) feature of it does differ from NUnit significantly. VS unit testing can be used with code that was not written with unit testing in mind.
VS 中的单元测试结构在用法上类似于 NUnit。它的一个有趣(且有用)的特性与 NUnit 有很大不同。VS 单元测试可用于未考虑单元测试的代码。
You can build a unit testing framework after an application is written because the test structure allows you to externally reference method calls and use ramp-up and tear-down code to prep the test invironment. For example: If you have a method within a class that uses resources that are external to the method, you can create them in the ramp-up class (which VS creates for you) and then test it in the unit test class (also created for you by VS). When the test finishes, the tear-down class (yet again...provided for you by VS) will release resources and clean up. This entire process exists outside of your application thus does not interfere with the code base.
您可以在编写应用程序后构建单元测试框架,因为测试结构允许您从外部引用方法调用并使用启动和拆除代码来准备测试环境。例如:如果您在类中有一个使用方法外部资源的方法,您可以在加速类(VS 为您创建)中创建它们,然后在单元测试类(也创建VS 为您提供)。测试完成后,拆卸类(再次...由 VS 为您提供)将释放资源并进行清理。整个过程存在于您的应用程序之外,因此不会干扰代码库。
The VS unit testing framework is actually a very well implemented and easy to use. Best of all, you can use it with an application that was not designed with unit testing in mind (something that is not easy with NUnit).
VS 单元测试框架实际上是一个很好实现且易于使用的框架。最重要的是,您可以将它与未考虑单元测试的应用程序一起使用(这对于 NUnit 来说并不容易)。
回答by tvanfosson
First thing I would do is download a copy of TestDriven.Netto use as a test runner. This will add a right-click menu that will let you run individual tests by right-clicking in the test method and selecting Run Test(s). This also works for all tests in a class (right-click in class, but outside a method), a namespace (right click on project or in namespace outside a class), or a entire solution (right-click on solution). It also adds the ability to run tests with coverage (built-in or nCover) or the debugger from the same right-click menu.
我要做的第一件事是下载TestDriven.Net的副本以用作测试运行程序。这将添加一个右键单击菜单,您可以通过右键单击测试方法并选择运行测试来运行单个测试。这也适用于类中的所有测试(在类中右键单击,但在方法之外)、命名空间(右键单击项目或在类外的命名空间中)或整个解决方案(右键单击解决方案)。它还增加了从相同的右键单击菜单运行具有覆盖率(内置或 nCover)或调试器的测试的能力。
As far as setting up tests, generally I stick with the one test project per project and one test class per class under test. Sometimes I will create test classes for aspects that run across a lot of classes, but not usually. The typical way I create them is to first create the skeleton of the class -- no properties, no constructor, but with the first method that I want to test. This method simply throws an NotImplementedException.
至于设置测试,通常我坚持每个项目一个测试项目,每个测试类一个测试类。有时我会为跨越很多类的方面创建测试类,但通常不会。我创建它们的典型方法是首先创建类的骨架——没有属性,没有构造函数,而是使用我想要测试的第一种方法。这个方法只是抛出一个 NotImplementedException。
Once the class skeleton is created, I use the right-click Create Unit Tests in the method under test. This brins up a dialog that lets you create a new test project or select an existing one. I create, and name appropriately, a new test project and have the wizard create the classes. Once this is done you may want to also create the private accessor functions for the class in the test project as well. Sometimes these need to be updated (recreated) if your class changes substantially.
创建类骨架后,我在被测方法中使用右键单击“创建单元测试”。这会出现一个对话框,让您可以创建一个新的测试项目或选择一个现有的项目。我创建并适当命名一个新的测试项目,并让向导创建类。完成此操作后,您可能还想为测试项目中的类创建私有访问器函数。如果您的课程发生重大变化,有时需要更新(重新创建)这些。
Now you have a test project and your first test. Start by modifying the test to define a desired behavior of the method. Write enough code to (just barely) pass the test. continue on with writing tests/writing codes, specifying more behavior for the method until all of the behavior for the method is defined. Then move onto the next method or class as appropriate until you have enough code to complete the feature that you are working on.
现在您有一个测试项目和您的第一个测试。首先修改测试以定义方法的所需行为。编写足够的代码来(勉强)通过测试。继续编写测试/编写代码,为方法指定更多行为,直到定义了方法的所有行为。然后根据需要转到下一个方法或类,直到您有足够的代码来完成您正在处理的功能。
You can add more and different types of tests as required. You can also set up your source code control to require that some or all tests pass before check in.
您可以根据需要添加更多不同类型的测试。您还可以将源代码控制设置为要求在签入前通过部分或全部测试。
回答by Scott Dorman
Easily the most significant difference is that the MSTest support is built in to Visual Studio and provides unit testing, code coverage and mocking support directly. In order to do the same types of things in the external (third-party) unit test frameworks generally requires multiple frameworks (a unit testing framework and a mocking framework) and other tools to do code coverage analysis.
很明显,最显着的区别是 MSTest 支持内置于 Visual Studio 中,并直接提供单元测试、代码覆盖率和模拟支持。为了在外部(第三方)单元测试框架中做同类型的事情,一般需要多个框架(一个单元测试框架和一个mocking框架)等工具来做代码覆盖率分析。
The easist way to use the MSTest unit testing tools is to open the file you want to create unit tests for, right click in the editor window and choose the "Create Unit Tests..." menu from the context menu. I prefer putting my unit tests in a separate project, but that's just personal perference. Doing this will create a sort of "template" test class, which will contain test methods to allow you to test each of the functions and properties of your class. At that point, you need to determine what it means for the test to pass or fail (in other words, determine what should happen given a certain set of inputs).
使用 MSTest 单元测试工具的最简单方法是打开要为其创建单元测试的文件,在编辑器窗口中右键单击并从上下文菜单中选择“创建单元测试...”菜单。我更喜欢将我的单元测试放在一个单独的项目中,但这只是个人喜好。这样做将创建一种“模板”测试类,其中包含允许您测试类的每个功能和属性的测试方法。此时,您需要确定测试通过或失败意味着什么(换句话说,确定在给定一组输入的情况下应该发生什么)。
Generally, you end up writing tests that look similar to this:
通常,您最终会编写类似于以下内容的测试:
string stringVal = "This";
Assert.IsTrue(stringVal.Length == 4);
This says that for the varaible named stringVal, the Length property should be equal to 4 after the assignment.
这表示对于名为 stringVal 的变量,长度属性在赋值后应该等于 4。
The resources listed in the other thread should provide a good starting point to understandng what unit testing is in general.
另一个线程中列出的资源应该提供一个很好的起点来理解单元测试一般是什么。