Use Visual Studio for unit testing

Source: Internet
Author: User

With the unit test function in Visual Studio, you can easily create unit test projects, write unit test code, and execute unit tests. How to Use the unit test function in Visual Studio is described in this article and later.

1. Create a test project
Before creating a test project, we also need a piece of code to be tested. Below is a short piece of code I have prepared.

namespace BigMan.UnitTest{    public class Program    {        public static int Add(int a, int b)        {            return a + b;        }        public static int Div(int a, int b)        {            return a / b;        }        static void Main(string[] args)        {        }    }}

You can create a test project by right-clicking in the code window and selecting create unit test ].

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1951026017-0.png "style =" width: 578px; height: 635px; "title =" 00.png" border = "0" height = "635" hspace = "0" vspace = "0" width = "578"/>

After you click it, the create unit test dialog box is displayed. Select the method to perform the unit test and select the corresponding output project. If a test project already exists in the solution, the project is selected by default in the drop-down box of output project. If no, you need to create a test project. Click OK to create the test project.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1951021C1-1.png "title =" 02.png"/>


In Visual tudio 2010, the unit test is created by default in the right-click menu of the code window, but it disappears in Visual Studio 2012, need you to manually tune it out, the specific method can refer to the http://www.jb51.net/softjc/83751.html

Another method is to add a test project in Solution Explorer by right-clicking it, which is the same as adding another project. You can select a unit test project when selecting a project. After the project is created in this way, all the test code for each method needs to be manually written. Of course, you can also follow the above method to make Visual Studio automatically generated, you only need to select the newly created test project as the output project in the create unit test window.

2. Maintain the test code


Using BigMan. unitTest; using Microsoft. visual Studio. testTools. unitTesting; using System; namespace UnitTestProject1 {// <summary> // This is the test class of ProgramTest, the objective is to // include all ProgramTest unit tests /// </summary> [TestClass ()] public class ProgramTest {private TestContext testContextInstance; /// <summary> /// obtain or set the test context. The context provides /// information about the current test run and its functions. /// </Summary> public TestContext {get {return testContextInstance;} set {testContextInstance = value ;}} # additional region test features # endregion // <summary> // Div test /// </summary> [TestMethod ()] public void DivTest () {int a = 1; // TODO: Initialize to an appropriate value int B = 1; // TODO: Initialize to an appropriate value int expected = 1; // TODO: initialize to the appropriate value int actual; actual = Program. div (a, B); Assert. areEqual (expected, actual); // Ass Ert. Inconclusive ("verify the correctness of this test method. ") ;}/// <Summary> /// Add Test /// </summary> [TestMethod ()] public void AddTest () {int a = 1; // TODO: Initialize to the appropriate value int B = 1; // TODO: Initialize to the appropriate value int expected = 2; // TODO: Initialize to the appropriate value int actual; actual = Program. add (a, B); Assert. areEqual (expected, actual); // Assert. inconclusive ("verify the correctness of this test method. ");}}}

Briefly introduce the test code. First, we use the TestClass feature to identify the test methods contained in this class. Next is the TestContext attribute, which is used to provide context information during the test. In actual use, there are multiple purposes. I will introduce it in detail later, this attribute is not used during the test in this article. The code in "additional test features" is too long because it is not available for the time being, so it is deleted by me. The content in it will be gradually introduced later. The bottom two methods are the two test methods generated for the Add () and Div () methods in our code. Like the test class, the test method also has a feature to identify -- TestMethod ()].

ToMethodAddTest () is used as an example to describe the basic structure of the test method. Step 1: declare and initialize a, B, and expected, which correspond to the two parameters of the Add (int, int) method and the expected return value respectively. Declare actual, and call the Add method to assign the actual result to it. After these two steps, the data is ready, and the test result is judged, which is called Assert. In this exampleAssert. AreEqual ()For the first time, Assert also has a series of asserted methods, such as AreNotEqual, IsNull, IsNotNull, and IsInstanceOfType, which are not listed here, you can learn through the smart prompts of code in Visual Studio, or simply take a look at the MSDN documentation. The Assert. Inconclusive ("in the last line verifies the correctness of this test method. "); You can comment it out. Otherwise, the test method will be skipped and a prompt will be displayed."Verify the correctness of this test method ". Modify parameters a and B in the test method and the expected result values, and then run the test method.

3. Run the test and view the test result.

There are also multiple methods to run the test. You can right-click the code window and choose run test from the menu ], you can also choose test> RUN> all tests on the menu bar to run the test. In Visual Studio 2010, you can also click the button on the toolbar to execute the test, however, in Viual Studio 2012, it seems that it also disappears by default. You need to manually set it.

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/1951023925-2.png" title="03.png" />

The final test result will show the information including the executed test, running time, and Failure Information for tracking and solving the problem. Modify the code, save the code, and click "Run all", or select "Run failed tests" to test the modification results until all tests pass.


So far, a basic and incomplete unit test has been completed, and the next article will continue to improve the testing of these two methods.


This article is from the "BigManOnTheWay" blog, please be sure to keep this source http://bigman.blog.51cto.com/7862336/1288802

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.