Creating tests using IntelliTest

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.

Getting ready

You will need to use Visual Studio Enterprise 2015 to be able to create and run IntelliTests.

How to do it…

  1. Before you go on, ensure that you have added the code contracts using statement to the top of your Recipes.cs class file:
    using System.Diagnostics.Contracts;
  2. Add a new class called CodeContractTests to your Recipes.cs file:
    public class CodeContractTests
    {    
    
    }
  3. Then, add a method called 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;
        }
    }
  4. Select the Calculate() method and right-click on it. From the context menu, click on the Create IntelliTest menu item:
    How to do it…
  5. Visual Studio will then show the Create IntelliTest window. Here, you can define several settings for your IntelliTest. One thing to note is that you can use a different test framework than MSTest. For our purposes, however, we will use MSTest and keep the rest of the settings set to their defaults:
    How to do it…
  6. When you click on the OK button, Visual Studio will continue to create a new test project for you:
    How to do it…
  7. When the project creation is complete, you will see the new test project created in the Solution Explorer. In this case, because we kept the default settings in the Create IntelliTest window, our new test project will be called Chapter8.Tests:
    How to do it…
  8. Go ahead and expand the 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)
        }
    }
  9. Back in the CodeContractTests class, right-click on the Calculate() method and select Run IntelliTest from the context menu:
    How to do it…
  10. IntelliTest will jump into action and open the IntelliTest Exploration Results window:
    How to do it…
  11. From the test results we ran for the 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:
    How to do it…
  12. Let's modify the 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;
    }
  13. From the additional code contracts, we can see that by requiring the 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:
    How to do it…
  14. Right-click on the 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:
    How to do it…

How it works…

IntelliTest allows developers to quickly and efficiently create tests for your code contracts with a few clicks of your mouse.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.14.132