[Unit test] VS-run the code to add a unit test prompt: No classes or namespaces in this assembly, vs-namespaces

Source: Internet
Author: User

[Unit test] VS-run the code to add a unit test prompt: No classes or namespaces in this assembly, vs-namespaces

In Visual Studio 2012, you are planning to add a unit test for the following method, but a prompt is displayed: No classes or namespaces in this assembly.

 1 namespace UnitTest 2 { 3      class Program 4     { 5         public int Add(int a, int b) 6         { 7             return a + b; 8         } 9         public int Divide(int a, int b)10         {11             return a / b;12         }13         static void Main(string[] args)14         {15         }16     }17 }

The reason is that the access permission to the Program class is set to internal by default. This class can only be accessed within the Assembly. Creating a new test project already belongs to another Assembly. At this time, the test project certainly cannot view the classes in the project to be tested. Therefore, the public access modifier must be added before the class to be tested.

I was fooled by the default access modifier again.

This section describes how to complete a unit test by adding a unit test project. In unit testing, a simple understanding is to write a test class. In this test class, enter test parameters and expected results. Then, run the method of the class to be tested in the method of the test class to check whether the running results meet the expected results. You need to enter a series of test examples to complete the test.

 

1. Create a unit test project in the solution, as shown in, and name it UnitTestProject1.

  

2. The newly created solution interface is as follows. Pay attention to the [TestClass] and [TestMethod] features, indicating that this is a test class. The Microsoft. VisualStudio. TestTools. UnitTesting namespace is automatically added to the program.

Because we want to test the project in the UnitTest set in this unit test project. Therefore, you must first Add a reference to the project to be tested. The entire program interface is as follows:

 

  

3. Now is the time to write the test code. We need to write a test program for the method we want to verify in TestMethod. The basic method of unit test is to call the function of the tested code, enter the parameter value of the function, obtain the returned result, and then compare it with the expected test result. If it is equal, the test is passed, otherwise, the test fails. Assertion-based tests are performed in unit tests in. The assertion class is used to complete this judgment. It compares the test results with the expected results and then outputs the test results when running the test statement. The whole process is the same as calling a class method.

1 [TestClass] 2 public class UnitTest1 3 {4 [TestMethod] 5 public void TestMethod1 () 6 {7 UnitTest. program method2test = new Program (); 8 int a = 1; // TODO: Initialize one of the input parameters 9 int B = 3 as required; // TODO: initialize the input parameter 2 10 int Ct = 4 as required; // TODO: Initialize the correct result as required 11 int actual; // It is used to store the running result 12 actual = method2test. add (a, B); 13 // start the comparison result, Assert asserted 14 Microsoft. visual Studio. testTools. unitTesting. assert. areEqual (CT, actual); 15} 16}

  

  Common assertions: Assert, CollectionAssert, StringAssert

 

4. Run the unit test. Open Test Explorer. Generate all Test items and click Run in Test Explorer. The result is as follows:

 

5. You can view the Test results in Test Exporer. You can see whether the code to be tested has passed the test. You can right-click the test result to view the code coverage rate:

  

  

For articles about code coverage, see: http://www.cnblogs.com/coderzh/archive/2009/03/29/1424344.html

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.