Pex – test input generator
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
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?
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.
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 :
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:
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.