Note: This article is the author’s original, can be reproduced at will, but please indicate the source. As is not willing to indicate can be left blank, strongly opposed to change the original source.





TDD(Test-driven Development) is a core practice and technology in agile Development, as well as a design methodology. TDD works by writing unit test case code before developing functional code, which determines what product code needs to be written. Unit testing is the most basic testing step. Located at the bottom of the V model of the entire product development process.

As shown below, after RA&PSD is completed in various development processes, the UAT solution can be completed without the underlying foundation, as can the following steps. UT, as a basic and CODING step, is the foundation that every CODER must master.

RA( Requirement Analysis)  &

PSD ( Productive Specification Design)              UAT( User Acceptance Testing)

/ /

/ /

/ /

FSD(Functional Specification Design)             SIT(System Integration Testing)

/ /

/ /

/ /

Construction(Coding)         UT(Unit Tesing)

/ /

/ /

/ /

/ /

/ /

/ /

/ /

When writing small amounts of code, you can often test by creating a New Console Application, a new Web Form, etc., and typing in the test code. Once the amount of code and logical relationships become complex, such tests are difficult to deploy and not maintainable. And a lot of functional code, can not be completely separated from the whole project to achieve. For example, in a WEB project, there is a library that writes some MODEL layer methods. In order to verify the correctness of the method, to test the method, you may have to reference a large number of models, interfaces, and library functions in the project. When the compilation passes, it turns out that you must log in to invoke the method or the Directory you are in does not have access, so you have to modify the Authorization configuration or add login methods. So layer by layer, you’re testing almost every corner of the project, and the test scheme is not highly reusable.

Let’s take a look at unit testing in Visual Studio using VS2012 as an example.

1. First, right-click the Solution to bring up the Context menu. Select Add – New Project. In the given template, select Visual C# – test-unit Test Project as shown.



2. Obtain the template as shown in the figure.













3. Add the code you want to Test in the Test method (default TestMethod1, usually changed to the method name you want to Test +Test). But first, you need to reference the project you want to test and the libraries you want to use. In this case, I refer to Microsoft’s MVC4.0 framework and the Controller in the project to be tested. Here I test the Controller’s ProductCategories() method. This method returns an ActionResult. There are three lines in the test code. The first line instantiates the Controller and the second line calls the controller’s methods. If only these two lines pass the test, the code is syntactically correct and works with the rest of the code. The third line uses an assertion to check if the result returned by the method is null. Here I assert that it is not null, and if it is, the test will fail with an error.



4. After the Test code is written, you can click test-run-AllTests, etc.



After the test. A list of results is generated below. The testCases that failed are in red. To DEBUG it, right click on the red TestCase and select DEBUG Selected Tests. After modification, you can also right-click the TestCase you want to retest and select Run Selected Tests.