C #, unit testing entry

C #, unit testing entry

C #, unit testing entry (following may come from network)

First, what is called unit testing (unit testing)?

It refers to the smallest testable software unit checking and validation. For the meaning of the unit test unit, in general, be determined according to the actual situation of its specific meaning, such as C language, refers to a function unit, Java classes in a cell refers to, the graphical software may refer to a window or a menu Wait. In general, the smallest unit measured is a predetermined function module human. Unit testing is the lowest level of testing activities in the software development process to be carried out, an independent software unit will be tested in the case of the rest of the program phase isolated.

C #, a method, a class, a test window. That unit tests.

Second, why should unit test.

For the correctness of the program, as soon as the discovery BUG program, to facilitate the latter part of the development and debugging, and maintenance.

Third, who do this work?

The answer is: the programmers themselves, because they would understand their own programmers to write code, to know to achieve the effect.

Fourth, when to do it?

The sooner the better unit testing, early to what extent? Extreme Programming (Extreme Programming, or simply XP) pay attention to the TDD, ie, test-driven development , write test code first, then development. In the actual work, you can not over-emphasize the first what is important is efficient and comfortable. From experience, the first product to write a function of the frame , and then write the test function , written for product features function test , and then write the code product function, write a function point per test run at any time to add test cases. The so-called write your product function framework , means to write an implementation of function space, there is a direct return an appropriate return value, and compile and then write test code, time, function name, parameter list, return type should be determined down the possibility of subsequent written test code to be modified relatively small.

Five, C # test project DEMO, this demo written in VS2015.

1. Create a new console application. Name called: UnitTestDemo

 

2, write a method to be tested.

 

Copy the code

    public class Program
    {
        static void Main(string[] args)
        {
        }

        public static int Add(int pNum1,int pNum2)
        {
            return pNum1 + pNum2;
        }
    }

Copy the code

 

 

3, create a unit test project. Name called: UnitTestDemoTest

 

 

Modify the class name, and method name. UnitTest1 rename ProgramTest, method name: TestMethod1 changed: AddTest.

Note that, with the surface characteristics of the above method: [TestMethod] This is necessary. It tells the compiler that this is a French test.

write the code. Call the method to be tested.

 

Copy the code

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UnitTestDemo;

namespace UnitTestDemoTest
{
    [TestClass]
    public class ProgramTest
    {
        [Test Method]
        public void AddTest()
        {
            int a = 100;
            int b = 10;
            Assert.AreEqual(Program.Add(a, b), 110);
        }
        [Test Method]
    }
}

Copy the code

 

 

4, open the "Test Resource Manager" in the "test" -> Window -> Test Resource Manager

 

 

We can see there is no testing program.

 

Now to generate a test project.

 

Back to see. Is one more test

Right-click on it. Select "Run the selected test"

 

We can see in front of one by marking. The test passed.

 

If it does not pass. A red X, we write a method that did not pass.

Note that the characteristics of the method must be added [TestMethod], and then generates the application, the test run again.

Copy the code

        public void AddTest2()
        {
            int a = 100;
            int b = 10;
            Assert.AreEqual(Program.Add(a, b), 50);

        }

Copy the code

 

 

 

5、  回到主项目。看方法提示行。上面有标测测试通过。

 

在方法上面有一个清楚的提示。

这里说一下VS的强大功能。这个提示很好用。

A、 可以提示方法的引用数量,并快速定位,

B、 还可以提示单元测试的结果。

C、 还可以提示源代码版本管理器,提交及修改的情况。

 

6、  以上是手工建立测试项目的。还有一种快捷的方法建立测试项目。在要测试的方法行,右击。选择创建单元测试。可弹出建立单元测试对话框。

 

 六、现在说说单元测试里Assert这个类。

(一)、Assert类的使用

1、Assert类所在的命名空间为Microsoft.VisualStudio.TestTools.UnitTesting 在工程文件中只要引用Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll就可以使用 了。

2、使用Assert类可以对特定功能进行验证,单元测试方法执行开发代码中的方法代码,但只有包含Assert语句时才能报告代码行为方面的内容。

3、Assert在测试方法中,可以调用任意数量的Assert类方法,如Assert.AreEqual()方法。Assert类有很多方法可供选择,其中许多方法具有多个重载。

4、使用CollectionAssert类可比较对象集合,也可以验证一个或多个集合的状态。

5、使用StringAssert类可以对字符串进行比较。此类包含各种有用的方法。如:StringAssert.Contains、StringAssert.Matches和StringAssert.StartWith。

6, AssertFailedException as long as the test fails, it will lead to AssertFailedException exception. If the test timeout, causing unexpected exception, or contains the results generated Failed Assert statement, the test fails.

7, as long as the test results generated AssertInconclusiveException is Inconclusive, will lead AssertInconclusiveException. Typically, the test is still processing Assert.Inconclusive added to indicate that the test is not yet ready, not run.

 

(Two), Assert class main static member

 

1, AreEqual: N times a method is overloaded, the main function is to determine whether the two values ​​are equal; if the two values ​​are not equal, then the test fails.

2, AreNotEqual: N times a method is overloaded, the main function is to determine whether the two values ​​are not equal; if the two values ​​are equal, then the test fails.

3, AreNotSame: whether or not the object referenced by the same; if both inputs contents refer to the same object, the test fails.

4, AreSame: object references are the same; if the two inputs are not the same object reference in the test fails

5, Fail: Assertion failed.

 

6, Inconclusive: said it could not prove to be true or false test results

7, IsFalse: specify whether a condition is false; if the condition is true, the test fails.

8, IsTrue: specify whether a condition is true; if the condition is false, the test fails

9, IsInstanceofType: testing whether the specified instance of the object type is required; if the instance is not required inheritance hierarchy of the object, loss of the test

 

10, IsNotInstanceofType: testing whether the specified instance of the object type is desired; instance if desired the inheritance hierarchy of the object, the test fails

11, IsNull: object test whether the specified non-empty

12, IsNotNull: object test whether the specified non-empty

 

Guess you like

Origin blog.csdn.net/cxu123321/article/details/91967447