IntelliTest allows developers to create and run tests against their code contracts. This allows developers to create the most robust code possible by creating additional code contracts to pass the test failures reported by IntelliTest. One thing to note, however, is that IntelliTest is included in the Visual Studio Enterprise only.
You will need to use Visual Studio Enterprise 2015 to be able to create and run IntelliTests.
using
statement to the top of your Recipes.cs
class file:using System.Diagnostics.Contracts;
CodeContractTests
to your Recipes.cs
file:public class CodeContractTests { }
Calculate()
to the CodeContractTests
class and pass two integer values as parameters to the Calculate()
method. Inside the Calculate()
method, add a code contract to ensure that the result from this method is never equal to zero:public class CodeContractTests { public int Calculate(int valueOne, int valueTwo) { Contract.Ensures(Contract.Result<int>() >= 1, ""); return valueOne / valueTwo; } }
Calculate()
method and right-click on it. From the context menu, click on the Create IntelliTest menu item:Chapter8.Tests
:Chapter8.Tests
project and then click on the CodeContractTestsTest.cs
file created for you. You will see the following code created for you by Visual Studio:/// <summary>This class contains parameterized unit tests for CodeContractTests</summary> [PexClass(typeof(CodeContractTests))] [PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperati onException))] [PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentExcept ion), AcceptExceptionSubtypes = true)] [TestClass] public partial class CodeContractTestsTest { /// <summary>Test stub for Calculate(Int32, Int32)</summary> [PexMethod] public int CalculateTest( [PexAssumeUnderTest]CodeContractTests target, int valueOne, int valueTwo ) { int result = target.Calculate(valueOne, valueTwo); return result; // TODO: add assertions to method CodeContractTestsTest.CalculateTest (CodeContractTests, Int32, Int32) } }
CodeContractTests
class, right-click on the Calculate()
method and select Run IntelliTest from the context menu:Calculate()
method, we can see that we have three failed tests and one successful test. The test failures reported are DivideByZeroException
, ContractException
, and OverflowException
. Clicking on individual test failures allows you to view the test details as well as the Stack trace:Calculate()
method by adding the following additional code contracts:public int Calculate(int valueOne, int valueTwo) { Contract.Requires(valueOne > 0, "Parameter must be greater than zero"); Contract.Requires(valueTwo > 0, "Parameter must be greater than zero"); Contract.Requires(valueOne > valueTwo, "Parameter values will result in value <= 0"); Contract.Ensures(Contract.Result<int>() >= 1, ""); return valueOne / valueTwo; }
valueTwo
parameter to be greater than zero, we have resolved the DivideByZeroException
. We can also see that the code contract that requires valueOne
is always greater than valueTwo
. Thus, we have resolved the ContractException
. Finally, by requiring that both parameters be greater than zero, we have automatically resolved the OverflowException
:Calculate()
method and run the IntelliTest again. This time, you will see that all the tests have passed, and our method under contract is now ready for use in production code:3.145.14.132