In the previous post, we went through the exploration testing process to exercise a simple method, CheckPositive. In this post, we'll try the same exploration testing, but will let Pex do it.
// mehod under test
1 void CheckPositive(int i, bool @throw) {
2 if (i < 0) {
3 Console.WriteLine("not ok");
4 if (@throw)
5 throw new ArgumentException();
6 }
7 else
8 Console.WriteLine("ok");
9 }
// hand-crafted unit tests
[TestMethod] void Zero() {
CheckPositive(0, false);
}
[TestMethod] void MinusOne() {
CheckPositive(-1, false);
}
[TestMethod] void MinusOneAndThrow() {
CheckPositive(-1, true);
}
Exploration testing with Pex
To let Pex explore the CheckPositive, we write a little test wrapper around that method:
[TestClass, PexClass]
public partial class ExplorationTesting {
[PexMethod]
public void Test(int i, bool @throw) {
CheckPositive(i, @throw);
}
We also instrumented the original method with additional methods to track down the path conditions that Pex computes along the execution traces. Pex generates 3 pairs of values which are equivalent to what the test we manually created:
- 0, false
- int.MinValue, false
- int.MinValue, true (throws)