Initial contact and unit test contact

Source: Internet
Author: User

Initial contact and unit test contact

I tested the program in a primitive way and directly debugged and run it. Two days ago, I learned to attach the process for debugging with my colleagues. I think it is amazing, but even if the append process still needs some previous test code. It is also troublesome. So I thought about unit testing. Of course, the test framework provided by VS is used. The specific operating principle is not discussed in detail. I will understand it later and write another article in detail. This is just the first time of contact, and even so it provides great convenience for my work. Every time you run a unit test project, the "fail" prompt is displayed. I don't understand it anymore. Later, I found that debugging can be performed step by step, so I debugged it. Finally, I found that the result of comparing two variables is output. In fact, my usage is very simple, it is to debug it directly to see how much the current value is. In line with my expectation, the debugging will be terminated directly. This is much easier than debugging a project.

Below is a record of my demo steps

1. Create a class library project and a unit test project

2. Write a public method in the class library, reference the class library project, and call this method in the test project.

Code of the class library Test method:

    public static class Class1    {        public static int Add(int a, int b)        {            return a + b;        }    }

TEST project code:

Using System; using Microsoft. visual Studio. testTools. unitTesting; using UnitTestLibrary; namespace UnitTestProject1 {[TestClass] public class UnitTest1 {[TestMethod] public void TestMethod1 () {int result = Class1.Add (3, 5 );} // set a breakpoint here }}
3. directly use the debugging mode to check whether the value during running is the expected value.

Run the unit test in debug mode and you can see the results of the monitored local variables.

Supplement:

In actual test projects, some tested methods need to access the database. In this case, you need to add an App to the test project. config file, and configure the database connection in the file. Can be tested normally, otherwise an error will be reported. I have no specific errors. It is not posted here. Because I tested a Web project, I tried to add the Web. Config file before, and the result would not work.

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.