This article focuses on some tips for the visual Studio (2012+) Unit Testing Framework:
- How to emulate the constructor of a class
- Optimized code for easy testing
first, how to simulate the constructor of a class1.1 code under test
The base code, IShape respectively, have rectangle and triangle sub-categories. There is a simple CalculateArea method.
namespaceblogdemo.utdemo{ Public InterfaceIShape {intCalculateArea (); } classRectangle:ishape { Public intCalculateArea () {return 1; } } classTriangle:ishape { Public intCalculateArea () {return -; } }}
Class being tested: This class calls different subclasses to operate according to the parameters passed in.
class " Span style= "color: #000000;" > App { public void Calculate ( Span style= "color: #0000ff;" >string ShapeType) { if (ShapeType = = t " Triangle (). CalculateArea (); else {
new Rectangle (). CalculateArea (); } } }
1.2 test target
Now to test the Calculatef method of the app class, test the class for a case that can be divided into two
- Instantiate the Triangle class when the input (ShapeType) is T.
- Instantiate the rectangle class when the input is non-T.
The test above is primarily to test if the dispatch of the If is correct, and does not care what calculate is doing. So the validation of the above test should not be verified by the value of CalculateArea because it violates the unit test's "unit" word.
1.3 Test Code
Under the Ms.test framework, constructors for classes named ClassName can be simulated by shimclassname.constuctor
[TestClass] Public classapptests {[TestMethod] Public voidcalculatetest () {using(Shimscontext.create ()) {vartriangleconstructed =false; Shimtriangle.constructor= (@this) = =//the constructor for the Triangle class is simulated here, and triangleconstructed is true if triangle is instantiated{triangleconstructed=true; }; varrectangleconstructed =false; Shimrectangle.constructor= (@this) = =//the constructor for the rectangle class is simulated here, and rectangleconstructed is true if rectangle is instantiated{rectangleconstructed=true; }; App.calculate ("T"); Assert.isfalse (rectangleconstructed); Assert.istrue (triangleconstructed); } } }
Because App.calculate ("T"), the Triangle class is instantiated (Triangleconstructed is true), and the rectangle class is not instantiated (Rectangleconstructed is false).
Instead, the following test results are obtained:
[TestClass] Public classapptests {[TestMethod] Public voidcalculatetest () {using(Shimscontext.create ()) {vartriangleconstructed =false; Shimtriangle.constructor= (@this) = =//the constructor for the Triangle class is simulated here, and triangleconstructed is true if triangle is instantiated{triangleconstructed=true; }; varrectangleconstructed =false; Shimrectangle.constructor= (@this) = =//the constructor for the rectangle class is simulated here, and rectangleconstructed is true if rectangle is instantiated{rectangleconstructed=true; }; App.calculate ("F");//not T. Rectangle will be instantiatedassert.istrue (rectangleconstructed); Assert.isfalse (triangleconstructed); } } }
second, the code optimization.
In TTD there is a saying that code is not testable, in fact, if the ms.test framework is not shim powerful, the above code is actually not testable. The above code can be fully optimized.
2.1 Optimized Code
Public classApp { Public Static voidCalculate (stringShapeType) { varShape =Createshape (ShapeType); Shape. CalculateArea (); } Public StaticIShapeCreateshape(stringShapeType) { if(ShapeType = ="T") { return NewTriangle (); } Else { return NewRectangle (); } } }
The above uses a simple factory to build a IShape object, so that the build process is published in the form of a method, you can test this createshape method alone.
2.2 Optimized test code
[TestMethod] Public voidcreateshape_triangleconstructed_test () {varShape = App.createshape ("T"); Assert.AreEqual (Shape. GetType (),typeof(Triangle)); } [TestMethod] Public voidcreateshape_rectangleconstructed_test () {varShape = App.createshape ("F"); Assert.AreEqual (Shape. GetType (),typeof(Rectangle)); }
Simply adjusting the code, testing the code is very simple, and very clear, of course, for the optimized code before using the Shim method of test code can continue to run.
Iii. Conclusion
Using Microsoft's test framework many of the previously "can't test" code can still continue testing, but this is not a good habit, testing should not be too dependent on the feature of the test framework. Instead, you should slowly adjust the code during the test so that the code becomes "tested".
Unit Test Via Visual studio-part4