Unit Testing with Visual Studio 2015's IntelliTest

Posted by David Jung - July 31, 2015

header-picture

When you run IntelliTest, it examines your .NET code to generate not only unit tests but also test data for the unit tests to use. Based on the logical branch conditions it finds, tries to create tests to provide the maximum amount of code coverage. In case you were wondering, Microsoft has been working on this technology for a while. IntelliTest is based on the Pex framework that originally came from Microsoft Research.

As covered in the previous IntelliTest article, the IntelliTest Exploration Results pane displays the results of the IntellTest unit tests. Depending on the method or function, there will be a number of conditions that pass and/or fail. Not all the failed tests are bad though. In the following screenshot, IntelliTest passed null values to both the Employee and Calculator parameters. We should expect to see that an exception thrown and for this test to fail. This shows us that we probably are not using the IsNullOrEmpty method in our code to verify that at least a value is passed.

IntelliTest - Null Value

In the other failed test, IntelliTest passed a numeric value for the Location parameter rather than an enumerated value. In this case, we like the fact that this exception was raised because we are not expecting numeric values; therefore, this should not be considered a failed test but an acceptable unit test scenario.

IntelliTest-Allow1

In order for this test to past in the future, select the test and from the context menu, click the Allow menu option.

IntelliTest-Allow2

This unit test will be added to the PexAllowedException attribute and the next time IntelliTest is run against this method, this particular unit test scenario will pass.

IntelliTest-Allow3

A Brief Look Behind the Scenes

You might have noticed there are eight (8) warnings in this particular run. The warnings show up in the IntelliTest Exploration Results in the far right of the toolbar. When you click on the warning message, the Results pane will show you a breakdown of the warnings and provide some insight as to how IntelliTest generated the tests for your code. In most cases, IntelliTest is forced to make some assumptions on how to create certain objects based on your code (and we all know what happens when you assume). Looking at the Object Creation tab, you can see that IntelliTest had to guess how create the objects. When you select an event, the Details pane shows you how IntelliTest created the object.

IntelliTest-ObjectCreation

You can accept or decide to explicitly declare how the IntelliTest factory creates the objects by selecting Fix either from the Toolbar or from the context menu. The generated code will be placed into a new Test Project within your solution.

IntelliTest-ObjectCreation-Fix

The IntelliTest engine is a great way to unit test your code without writing unit tests, but it does have some limitations and you will be alerted to them as well. In this example, when you click on the Limitation tab, we can see that there are several limitations listed. Upon closer inspection, you can try going through your code to try to resolving the limitation or if there is nothing you cannot do anything about, you can chose to Surpress the warning. That's done by selecting the Event warning and choosing Suppress either in the context menu or from the Panes toolbar.

IntelliTest-Limitation

Create IntelliTest versus Create Unit Test

Besides using the Run IntelliTest feature from the Code Window's context menu, there is another IntelliTest feature called Create IntelliTest. Do not confused this with the Create Unit Test feature. Assuming your project does not have a Test Project in it yet, the Create Unit Test feature only creates a test project, test class, and test stub. Very basic stuff. You will still need to write the actual unit test, but its a start.

CreateUnitTest

Create IntelliTest does more than just setup the basic unit test structure like Create Unit Test does. It uses the same engine that Run IntelliTest uses to create the unit tests on demand and saves them as code in the Test Project's test file. All you need to do is add values the test method is going to use to execute the test with and you're cooking with gas!

CreateIntelliTest

IntelliTest is one of those tools in Visual Studio 2015 that can really help developers raise the quality bar in the applications they are developing by helping add unit tests quickly where it makes sense to add them. Whether you follow Test Driven Development techniques to the letter or trying to introduce unit tests into applications that haven't any, adding unit tests with IntelliTest is now just a right-mouse click. So go "Run IntelliTest" and improve your unit test code coverage.

Excelsior!

 
 
 

Topics: Blog


Recent Posts

InCycle Recognized Across Americas

read more

InCycle, Microsoft & Cowboys

read more

InCycle Named Azure Data Explorer (ADX) Partner

read more