Over the past few posts we have discussed about many topics on Unit Testing in Visual Studio. We have learned, How to write your very first Unit Test , Exploring and Managing Unit Tests and we have seen how the code coverage is one the important aspect to identify the uncovered portion of code. Keeping all these learning on mind, lets learn another aspect of writing Smart Unit Test, using “IntelliTest” in Visual Studio 2015. IntelliTest helps us to create Unit Test automatically that give maximum of code coverage.
The overall process is very simple looks
Let’s have a look into how this works. Consider we have following method in a class which adds two positive number. ( We are using same example for all the unit test scenarios to understand it better and keep it simple) .
Now, Right Click on the Method, and Select “Run IntelliTest” from the context menu.
Once you select “Run IntelliTest” , Visual Studio performs certain steps to analyze and generate unit test for us. You can view these steps from Output Window –> IntelliTest
As soon as the processing is done, Visual Studio will launch an another new windows called “IntelliTest Exploration Results”, that generates set of all possible unit tests based on the method parameters and which can cover the maximum of code path of the given code block. These generated test covers all possible combination of parameter values; including boundary condition. As you can see in the below windows, the generated methods with it’s targets and the number of parameter (a, b) and the value that has been used for test parameter.
If you select any of these targeted method from the IntelliTest windows, you would be able to see the Details of Generated Unit Test by the Pex. This shows how internally a generated unit test getting called and cover the certain percentage of the code.
Select all the Unit Tests and Click in the Save Icon in the “IntellTest Exploration” Windows.
This will add a new Unit Test Project in the solution with all required assembly added. At this point of time you should be able to build the overall solution.
Now you can take help of Test Explorer to review, run and manage all the Unit Tests generated through the IntelliTest.
You can also take help of codelens to see the reference Tests methods, along with parameter used for test.
The last and final things is Code Coverage. Read the below highlighted post to understand the basic of code coverage. Similarly, Code coverage for test methods generated through the IntelliTest can also be shown using the Code Coverage Windows.
Must Read :
You can refer and read this post for more internal details of this feature.
Hope this helps !