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 http://research.microsoft.com/en-us/projects/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. |
![]() |
Lets run Pex against the CalculateResults method. |
![]() |
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. |
![]() |
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. |
![]() |
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. |
![]() |
![]() |
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. |
![]() |
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. |
http://channel9.msdn.com/Blogs/briankel/Pex-Automated-Exploratory-Testing-for-NET/player?w=512&h=288 |