Pex 0.11 Released: Delegates, Exception Trees, and Stubs
We just released Pex v0.11.40421.0, which you can download here. This release brings support for Delegates as Parameters, a new Exception Tree View, Stubbed Events, and Recursive Stubs. This also release fixes a blocking issue (incorrect registration of the Stubs Visual Studio Add-in), and it brings Pex in sync with the latest Code Contracts release.
Update: This release may still require a separate download of OpenMP dlls, which part of the vcredist.
Delegate As Parameters
Pex now understands Parameterized Unit Tests that take delegates as parameters. Pex will automatically generate a delegate instance and its behavior: if the delegate returns a value, Pex will generate a new symbolic value for it, track its use, and then generate different values depending on the program behavior. Let’s consider a simple method to illustrate this new feature. I wrote the following Parameterized Test method which takes a Func<int> delegate and throws an exception if the delegate returns a particular value:
When executing Pex on Test, Pex will generate a Func<int> which ‘asks’ Pex to choose the returned int (it uses PexChoose under the hood). Therefore, for each call to that delegate, Pex has the liberty to return a diferent value. Based on how it is used, Pex will generate different values to increase coverage. In this case, Pex ‘chooses’ to return 123 on the first call, which is exactly what we need to cover the exception branch. The following code is generated by Pex; it starts by setting up the desired values, and then it calls the parameterized unit test.
Pex Explorer: Exception Tree View
We have started to work on improving the experience when applying Pex to a large number of explorations. To this end, a new window called Pex Explorer will show various views over the events produced by Pex. The Exception Tree View provides the tree of exception types that Pex found. This is really helpful to quickly drill through the (really) bad exceptions first.
Pex Explorer: Contract Failures Tree View
If you are using Code Contracts, Pex also provides a specialized view to sort the contract failures kind.
Events in Stubs
Stubs now support events: the stubs simply expose the backing delegate fields (which hold the event delegate) as a public fields. As a result, one can clear, set, and invoke the event as any other member. Let’s see this with an example:
In order to raise the SomeEvent in a test, we would simply have to invoke the backing delegate field, which happens to have the same name as the event:
It’s that simple.
Recursive Stubs
Another common feature of mock/stub frameworks is to support nested invocation of members. Stubs now lets you recursively invoke property getters. Instead of assigning the property getter delegate, you can use new helper methods ending in ‘AsStub’ that take care of allocating the nested stub, assigning it to the property getter and returning it. Let’s see this with an example: assume we want to test the little snippet below.
In order to set up our stub, we would need to have IParent.Child return a stub of IChild, then set up IChild.Name to return ‘joe’. For property getters, Stubs generates a helper method that does just that:
The ChildGetAsStub call instantiated a SIChild instance, assigned to the Child property and returned it. It’s that simple.
Fixed Bugs
- .Stubx fail to generate the stubs. We fixed a bug with the registration of Stubs in Visual Studio.
Breaking Changes
- Microsoft.Stubs.dll has been renamed to Microsoft.Stubs.Framework.dll. The public Stubs type has been moved under the Microsoft.Stubs.Framework namespace.
- The generated stub class names are built by prepending ‘S’ to the type name (‘IFoo’ –> ‘SIFoo’). In the previous version, the ‘I’ of interface name was trimmed which sometimes created name clashes.