Pex – Step in to Automated Exploratory Testing for .NET

Pex automatically generates test suites with high code coverage. Right from the Visual Studio code editor, Pex finds interesting input-output values of  methods, which can be saved as a small test suite with high code coverage. Microsoft Pex is a Visual Studio add-in for testing .NET Framework applications.Pex is coming under Microsoft Power Tools and its a result of one of Microsoft research projects. Here’s the link for Pex

Following example will give a brief introduction on Pex and will be useful to get an idea on how it works.

Simple WPF application has ben created and it accepts two parameters(expecting two integers) from its silverlight page and displays addition of those two integers. There is a seperate Class called “Calculation” in the same namespace and it has “CalculateResult” method which adding two integers and return the result back. The program is working properly with two integers but as developers we cannot expect that the end user will always enters two integers and in this program it has not handled. In any parameterized method in a program, there can be unhandled scenarios like this(actually not  simple like this more complicated than this 🙂 )

 Following method will get two parameters and will try to return sum of those two.
Calculate Result Method
Lets run Pex against the CalculateResults method.
 Running Pex
Under the Pex Exploration Results window it shows all the test input parameters and the results generated with the exceptions if any. Pex Explorer shows the project, methods and their tests carried out.
 Pex Exploration Results
It shows all the posibble scenarios that the method can be failed. The tests fails comtain Format Exceptions, Argument Exceptions and Overflow exceptions as well. Under the Details Tab of the Pex Exploration Results window it gives seperate test method for each tests and StackTrace also available in the Pex Exploration Results window.
Will try to handle null exceptions for both parameters, run Pex and see.
 No Argument Exceptions in The Results
Tests can be saved as a new Test Project in the solution and later developers can debug and see.When adding the Test Project it asks about the test frame work to be selected and here Visual Studio Unit Test has bee selected as the framework.
 Save Tests
 Add New Test Project
Every Unit Test project needs a Unit Test framework for its functionalities. So developer has to select the Unit Test framework before generating the Test Project.
 Test Project
After handling null exceptions it doesnt throw Argument Exceptions according to the Pex Exploration Results.Just watch folllowing video and try to understand how Pex works, from the Reseach Engineers.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s