Pex – test input generator



Microsoft Research, PEX, MOLE, Test, .NETPex – test input generator

Microsoft Research, PEX, MOLE, Test, .NET


this post will focus on the Pex tool.

Pex is an automatically test input generates.

it try to get both high code coverage and potential failures.


you can download Pex from here.


So why do we need another testing tool?

Pex does not intend to replace any of the existing testing frameworks,

in matter of fact, its generated test can be saved as testing code using any of the leading

testing frameworks.


When to use Pex?

i guess it would be smart to use Pex after you wrote your unit tests (using your unit test framework of choice),

but before you actually sign the feature as done


Which tests would you write for the following code snippet?
Code Snippet
  1. public decimal Divide(decimal a,decimal b)
  2. {
  3.     return  a / b;
  4. }
  6. public double Divide(double a,double b)
  7. {
  8.     return a / b;
  9. }

this is a fairly small code so i guess you can think of most of the relevant tests,

even with out the Pex help 🙂


let see which tests will be chosen by Pex.

How to run Pex

after installing Pex, you can run Pex using the right click context menu.


Microsoft Research, PEX, MOLE, Test, .NET

doing so, Pex will try to understand the code and figure out which test are relevant,

in terms of code coverage and failure potentials.


for the decimal dividing Pex figure out the following tests :

Microsoft Research, PEX, MOLE, Test, .NET

as you can see (under columns a, b columns) the input values that was chosen by Pex is not a random ones.

it chose 0, decimal.MaxValue and decimal.MinValue.

we can see that 2 of the test were actually result with exceptions.


Saving Pex result as tests

one of the great feature of Pex is that it allow us to save the test results as unit test,

(by default it is using VS testing framework but you can change it using its setting,

you can find new tab under the VS tools-> options menu named Pex).


saving is very simple task. all you have to do is select one or more Pex test line, and press the save button

(Pex will generate test project for you if needed).



How to modify Pex generated unit tests?

Pex generated unit tests are build using 2 layers:

  • the layer of the unit testing framework (N-Unit, VS Test, MB-Unit, ext…),
    which is actually a facade that target the second layer (do not change code on this layer
    because Pex code generation may override it
  • the second layer called parameterized test and this is where you want to add assertions or
    even writing more parameterized tests and ask Pex the generate unit test for those custom tests.


this is how the layers look in the VS solution explorer:



Pex suggestions

final feature for this post is the suggestion window.

Pex will supply suggestion for making your code better.


which is looking as follow:




Pex is a great tool for making our code better,

we did not cover all of Pex capability so check the learn more section to get more on this topic.


Learn more


kick it on Shout it

Add comment
facebook linkedin twitter email

Leave a Reply to forex robot Cancel Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



  1. forex robot2010/03/30 ב 08:49

    Wow this is a great resource.. I’m enjoying it.. good article

  2. dor cohen2016/11/20 ב 13:08

    But a smiling visitor here to share the love (:, btw great design and style .