Code contracts do not seem to work on VS2012

I read Code Contracts, which at first glance seem pretty revolutionary, but I can't get them to work. I am running Windows 8 and Visual Studio 2012 Premium (releases versions of both). Then I installed the code contracts from here by clicking the "Download Code Contract" link.

Then I wrote the following code in a new console application:

class Program { static void Main(string[] args) { var answer = Add(0, 5); Console.Write(answer); Console.ReadLine(); } static int Add(int x, int y) { Contract.Requires(x > 0 && y > 0); return x + y; } } 

I expect the compilation to fail, since the first Add parameter is 0, but the program succeeds and displays 5 on the console.

I tried with the default contract code settings, and also dumped things a little uselessly. My current settings are as follows:

enter image description here

Any ideas what I'm doing wrong?

UPDATE:

Here are the results from the build window. It seems that he is doing something, but just warning about errors. In the video I watched, these things were marked as compilation errors, and the program did not even start.

 1>------ Build started: Project: DeleteMe, Configuration: Debug Any CPU ------ 1> DeleteMe -> c:\users\mike\documents\visual studio 2012\Projects\DeleteMe\DeleteMe\bin\Debug\DeleteMe.exe 1> CodeContracts: Task manager is unavailable. 1> CodeContracts: DeleteMe: Run static contract analysis. 1> CodeContracts: Suggested requires: Contract.Requires(false); 1> CodeContracts: DeleteMe: Validated: 0.0 % 1> CodeContracts: DeleteMe: Contract density: 0.87 1> CodeContracts: DeleteMe: Total methods analyzed 4 1> CodeContracts: DeleteMe: Methods with 0 warnings 3 1> CodeContracts: DeleteMe: Total time 4.974sec. 1243ms/method 1> CodeContracts: DeleteMe: Methods with necessary preconditions: 1 1> CodeContracts: DeleteMe: Discovered 1 new candidate preconditions in 00:00:00.1718843 1> CodeContracts: DeleteMe: Retained 1 preconditions after filtering 1> CodeContracts: DeleteMe: Inferred 0 object invariants 1> CodeContracts: DeleteMe: Retained 0 object invariants after filtering 1> CodeContracts: DeleteMe: Detected 0 code fixes 1> CodeContracts: DeleteMe: Proof obligations with a code fix: 0 1>c:\Users\Mike\Documents\Visual Studio 2012\Projects\DeleteMe\DeleteMe\Program.cs(14,10,14,33): warning : CodeContracts: requires is false: x > 0 && y > 0 1>c:\Users\Mike\Documents\Visual Studio 2012\Projects\DeleteMe\DeleteMe\Program.cs(22,10,22,44): warning : CodeContracts: location related to previous warning 1> CodeContracts: Checked 1 assertion: 1 false 1> CodeContracts: DeleteMe: 1> CodeContracts: DeleteMe: Static contract analysis done. ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ========== 
+20
c # visual-studio-2012 code-contracts
Nov 17 '12 at 20:56
source share
2 answers

Thus, the problem was a combination of several restrictions and gotchas with code contracts. Hope this answer helps people like me just get started.

Firstly, Code Contracts has supported Visual Studio 2012 (any version other than Express) since the build of 1.4.50327.0, although you need to run devenv.exe /setup if your build is older than 1.4.50910.0. See the Release Notes for more details.

The first problem I encountered was that the "Cache Results" checkbox was checked in the "Static Validation" section of the contract code properties tab. This option is enabled by default, as well as SQL Server CE for storing cached data that is not installed on Windows 8, VS2012, or Code Contracts. Unfortunately, your program will continue to compile just fine, and you will have to manually iterate over the build result to see the error:

CodeContracts: xxx: Unhandled exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Data.SqlServerCe, Version = 3.5.1.0, Culture = neutral, PublicKeyToken = 89845dcd8080cc91' or one of its dependencies. The system cannot find the specified file.

Uncheck the "Cache Results" box to fix this problem, as well as installing SQL Server CE.

The second problem is that contract code violations are treated as warnings rather than error compilation . Even if you turn on β€œHandle warnings as errors,” your program will continue to compile and run. If you have a larger project with many warnings that you ignore, it can potentially be difficult to notice these new Code Contract warnings. In the demo video that I saw , these warnings were also reflected in the Visual Studio IDE (the call code had a blue underline), however, I don’t know, it looks like this is a behavior in Visual Studio 2012.

This design decision bothers me. If I define a contract in my code that the function should take an integer greater than 0, and I frankly pass in 0, this is an error. Not a warning. I broke this contract, simple and simple.

All in all, I would say that Code Contracts is extremely efficient and can potentially change the way you test software. MS Research has definitely done an excellent job. However, I do not think that he is really ready for the mainstream. Some improvement is required to work, it does not integrate into the Visual Studio build process, and is also rather slow. On small projects, it worked as expected, but when I connected it to a larger project, it took ten minutes to analyze the entire code.

+13
Nov 17
source share

Sir, you will need to enable runtime checking. You have placed an image under your window.

β€œFull” will mean that all conditions are verified. The rest are self-evident.

  • Static validation is performed only in the background and checks everything that can improve inside your code. Caution - this slows down your builds a bit.
+2
Aug 16 '13 at 23:11
source share



All Articles