Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do You Configure Pex to Respect Code Contracts?

Given the following example code, how can I configure Pex to respect my Code Contracts?

    public static IEnumerable<User> Administrators(this UserGroup userGroup)
    {
        Contract.Requires(userGroup != null);
        Contract.Requires(userGroup.UserList != null);

        return userGroup.UserList.Where(ul => ul.IsAdmin == true);
    }

Current Problem: When I run Pex, it's still generating test cases which violate the specified code contracts.

FYI: Here are the 'Code Contracts' settings in my csproj file.


EDIT: Did something break in SP1?

like image 334
Jim G. Avatar asked May 26 '11 20:05

Jim G.


People also ask

What is Code Contracts?

Code Contracts provide a language-agnostic way to express coding assumptions in . NET programs. The contracts take the form of preconditions, postconditions, and object invariants. Contracts act as checked documentation of your external and internal APIs.

Which of the following methods is used in pre conditions and post conditions for code contracts in net?

NET 4.0. Code Contracts API includes classes for static and runtime checks of code and allows you to define preconditions, postconditions, and invariants within a method.


4 Answers

First you need to use a typed version of Requires

use ArgumentNullException as T

Also in you project properties you need to tell code cotracts to use the standard rewriter. DO NOT click assert on failure ;)

Contract.Requires<ArgumentNullException>(i != null);

then your code will throw an argumetn null exception and pex can add an attribute to your pexmethod to say that it is allowed to throw it and will create a passing test that throws the exception

you can then promote those and save the unit tests

like image 124
John Nicholas Avatar answered Oct 02 '22 16:10

John Nicholas


It violates the contracts on purpose to verify that an exception is thrown when the contract is violated. A lot of people will compile away the Requires methods in the Release build where some would say the edge cases should still be handled. It is also possible to write a custom contract failure handler which might not throw an exception or assertion failure. If you have a custom contract failure handler that doesn't prevent further execution you might cause even larger problems to happen further down the line.

What Pex does do is write the tests that violate a contract so that it passes when an exception is thrown.

TL/DR You shouldn't worry about it.

like image 20
Bryan Anderson Avatar answered Oct 05 '22 16:10

Bryan Anderson


I had the same problem. There are two things:

1) Check that Run-time rewriting is enabled (as John suggests)

2) Make sure that your test class is decorated with [PexClass(typeof(MyClass))]

I have written the test manually so I have forgot that PexClass attribute and the contracts were treated as regular exceptions by Pex - so they were failing.

Honza

like image 37
hoonzis Avatar answered Oct 05 '22 16:10

hoonzis


I also had to turn on Contract Reference Assembly to Build (I also emitted the XML doc file so I can see the contracts in VS).

This seems to be necessary for Pex to understand the contracts.

like image 42
nicodemus13 Avatar answered Oct 01 '22 16:10

nicodemus13