Unit testing with Visual Studio 2013-Beginner

Source: Internet
Author: User

1. Open VS2013--Create a new project. Here we create a console project by default. Name is Unittestdemo

2. Add a unit test project to the solution. Name is Unittestdemotest

After creation, the project structure is probably as follows:

3. Enter the program class of the console project Unittestdemo and create an Add method.

Namespace unittestdemo{    class program    {        static void Main (string[] args)        {        } public        static int ADD (int num1, int num2)        {            return num1 + num2;}}    }

4. Our goal is to test whether the above Add method is working properly and returns the correct result.

5. Refer to the console project in the Unit test project, after the reference is complete, the project structure is as follows:

6. Unit test project inside UnitTest1 The name of this class gets too ugly, we change the name to Programtest, which indicates that this is the test program class. The following changes are completed:

Using system;using microsoft.visualstudio.testtools.unittesting;namespace unittestdemotest{    [TestClass]    public class Programtest    {        [TestMethod] public        void TestMethod1 ()        {        }}    }

7. The above is the default method generated by VS for the Programtest class. You can see that Programtest 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.

8. It can be understood that a method must have a "TestMethod" attribute and that the class it belongs to has a "TestClass" attribute, then this method will be recognized by VS as a "unit test method".

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".

9. "Vs don't take a method as a unit test method" "What does that mean?"

You can open Test Explorer by tapping the VS menu bar, Test window and test Explorer.

10. The Test Explorer displays all the unit tests in the current solution. The point is that it only shows the method that meets the "8th" requirement.

You might say, why doesn't the "TestMethod" feature and the TestClass "feature now all show up in the above diagram?"

The reason is that you have not generated the assembly yet. OK, press F6. You can see that "TESTMETHOD1" is displayed in the test Explorer:

11. We need to be clear about the purpose of our unit testing: tools to help us test the quality, stability, performance, etc. of the software. "VS" serves as a tool here.

12. Back to the question of naming, it is obvious that testmethod this name is too ugly, we changed it to Addtest to indicate that this is the test Add method.

Using system;using microsoft.visualstudio.testtools.unittesting;namespace unittestdemotest{    [TestClass]    public class Programtest    {        [TestMethod] public        void Addtest ()        {        }}    }

13. As of now, we have established a test framework that can be implemented, although it has not played a role, but the test can be implemented.

One way to perform the test is to right-click the name of the unit test method you want to test and select run:

After you run your tests, you can view the results of your tests in Test Explorer:

There are many ways to perform unit tests, and these need to be learned in constant learning.

14. Next we do something meaningful. Improved Addtest method:

Using system;using microsoft.visualstudio.testtools.unittesting;namespace unittestdemotest{    [TestClass]    public class Programtest    {        [TestMethod] public        void Addtest ()        {            int num1 = +;            int num2 = n;            Assert.AreEqual (Program.add (NUM1, num2);}}}    

Before this, please change the visibility of the program class to internal or public.

? 15. " Assert "A friend who has not been exposed to unit tests before, or if English is almost impossible to know." On Google Translate:

Clearly, "assert" means "assert". So "Assert.AreEqual" (Program.add (NUM1, num2), 300); I assert that the result of "Program.add (NUM1, num2)" is 300.

The correctness of the assertion only needs to be tested by testing tools. If the test is correct by stating that the assertion is true, the test fails.

There are a number of assertions, including the normal assert above, the string assertion, the set assertion, and so on.

16.ok, run the test to see the test pass.

17. If you understand the above, then you can write your own simple unit test code and perform the test as of this point.

One last thing to say is that if you think it's too cumbersome to write unit test classes and methods manually, you can use the "unit test Generator" plugin to help you generate unit tests and methods.

This way you can focus on writing the test logic.

"Unit Test Generator" can be obtained by using the VS menu bar "Tools"-"extensions and Updates"-search "unit test Generator".

18. Installation of "Unit Test Generator" requires restart vs.

Right-click on the classes and methods that require unit testing (the resulting unit test method is shown in the figure)

After clicking, a dialog will pop up, which requires you to configure a name rule, and then click OK to generate the test method.

19. Finally, we hope to develop a good habit of testing code at any time.

Unit testing with Visual Studio 2013-Beginner

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.