Unit tests for C # programs using VS2013

Source: Internet
Author: User

Failed to make the successful unit test as expected, stumbling reference to the following two blogs roughly made, so it is necessary to record the process.

Http://www.cnblogs.com/duasonir/p/5299732.html (according to this I have successfully made unit test)

Http://www.cnblogs.com/Look_Sun/p/4514732.html (This I almost studied the day, but finally did not make out, finally see the above classmate's reference content and this article, after reading also made semi-finished)

Because the program is simple addition, and my own ideas are not included, the project name and some of the class name will not be changed, some of the terms are not accurate enough to look at.

Unit tests for C # console programs (generating unit tests and methods using the "unit Test Generator" plugin)

"Unit Test Generator" can be extended and updated with the VS menu bar: Tools---Search "" Unit Test Generator "", I have downloaded it, no prompt to download, after the installation is completed need to restart vs.

After the installation is ready for unit testing, this unit test is mainly based on the former blog successfully made unit tests.

1. Open vs2013: File--New project (click) and see the following interface

Click OK as follows:

2. Write the code, generate a new class, write the constructor and the Add () function. The code is as follows:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 Namespace Unittestdemo 8 {9     class PROGRAM10     {one         static void Main (string[] args)         {         }14< C7/>}15 public     class test16     {publicly         test ()         {         }21 public         int Add (int a, int b) 22< C14/>{23             return a + b;24         }25     }26}

3. Right-click the unit Test Generator in the code blank, and the default option is OK.

Then the interface looks like this:

4. Write the test code in the Testtest () function, as shown in the code below.

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using Unittestdemo; 7 using Microsoft.VisualStudio.TestTools.UnitTesting; 8 namespace Unittestdemo.tests 9 {ten     [TestClass ()]11 public     class testTests12     {         [TestMethod ()]14 Public         void Testtest ()         {             int a = 1, b = 2, expect = 3;17             Test t = new test ();             int real = T.add (A, b);             assert.areequal (re AL, expect);             //assert.fail ();         }23     }24}

5. Right-click in the code blank and run test (T) and the result will automatically pop up as shown:

Open Test Explorer: Click VS2013 's Menu bar: Test Explorer, window, Windows.

If the expected value is wrong, the test will not pass, such as:

Here is a test that does not pass and gives a picture of what the actual and expected values are. Source: http://www.cnblogs.com/libaoquan/p/5296384.html

These are the most successful unit tests, with no indication of actual values and expectations in addition to testing errors. (I did my best)

Ii. Unit Testing of C # console programs (manual unit test classes and methods)

Main References Blog: http://www.cnblogs.com/Look_Sun/p/4514732.html

1. The same as above. 1;

2. In the solution, add new project, right-click.

Add a unit test project. The name is unittestdemotest. Such as:

3. Go to the Program class of the console project Unittestdemo and create an Add method (the purpose is to test that the Add method is running properly and return the result correctly). Here the Add method is written to the program class, the unit test above is to generate a new class test, write the constructor and add () function.

The code is as follows:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 Namespace Unittestdemo 8 {9     class PROGRAM10     {one         static void Main (string[] args)         {         }14 public         static int Add (int num1, int num2)-         {             21 return NUM1 + num2;18         }19     }20     

4. In the Unit test Project Unittestdemotest, reference the console project Unittestdemo (right-click "Add Reference" at "References"), followed by the following interface:

After the reference is complete, the project structure is as follows:

The following is the default method generated by VS for the UnitTest1 class. You can see that UnitTest1 has a "TestClass" feature that indicates that this is a test class. TestMethod1 has a "TestMethod" feature, which indicates that this is a test method.

A method must have a "TestMethod" attribute, and the class to which it belongs has a "TestClass" attribute, then this method will be recognized as a "unit test method" by vs.

Without one of the two attributes above, there is no problem with compiling the build. But vs does not use it as a "unit test method".

1 using System; 2 using Microsoft.VisualStudio.TestTools.UnitTesting; 3  4 Namespace unittestdemotest 5 {6     [TestClass] 7 public     class UnitTest1 8     {9         [testmethod]10         public void TestMethod1 () one         {         }13     }14}

5. As of now, a test framework that can be implemented has been established, although it has not played a role, but the test can already be executed (click on all the operations in the test Explorer directly). Test results such as:

6. Add a test function to the test method in the unit test, and the code is as follows:

1 using System; 2 using Microsoft.VisualStudio.TestTools.UnitTesting; 3 using Unittestdemo; 4  5 Namespace Unittestdemotest 6 {7     [TestClass] 8 Public     class UnitTest1 9     {ten         [testmethod]11         public void TestMethod1 ()         {int             num1 = 100;14             int num2 = 200;15             (assert.areequal) Program.add ( NUM1, num2),         }18     }19}

Note: Be sure to refer to the project in the function that needs to be tested, I just do not have a reference to the error (I will allow this unprofessional explanation for what I do not understand). After this reference, change the visibility of the program class to internal or public.

Only in the explorer inside the reference to add a reference to the following error will appear (I was the following error, the whole thing, no foundation really bitter force, even add header files are not created-! ):

7. Code tests such as (test pass and non-pass interface):

The test does not pass when the expected and actual values are not indicated. O (╯-╰) o

Third, in fact, the class Wu Xiaoyong students to demonstrate the C + + unit testing.

Http://www.cnblogs.com/xiaoyongwu/p/5289964.html

At first I followed Wu Xiaoyong's blog, ready to write a C + + unit test. The unit test for C # has been written since the error has occurred.

The main is the occasion, the degree Niang let me see hope, feel very detailed, refer to unit test.

Http://www.cnblogs.com/Look_Sun/p/4514732.html

Later has been wrong, asked the class TA after the test has been able to pass but still do not know where the problem, confused when the following this blog, and finally successfully write this semi-finished unit test.

Http://www.cnblogs.com/duasonir/p/5299732.html

Add a unit test for C + + later. Ing...

Unit testing of C # programs using VS2013 (GO)

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.