Export (0) Print
Expand All

Walkthrough: Identify Which Unit Tests to Run to Verify a Code Change

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

In Visual Studio Team System Development, you can respond to changes in code more efficiently by identifying and running only those unit tests that help verify those changes. By using Test Impact Analysis, you avoid both running tests that are unnecessary and missing tests that are useful. For more information about Test Impact Analysis, see Identifying Code Change Impact on Tests.

Before you can identify the test impact of a code change, you must first perform the following tasks:

  • Check in your source code and unit tests to version control for Team Foundation.

  • Collect data about the impact on tests by running your unit tests. You typically collect data by using Team Foundation Build. You can also run your unit tests locally and then upload the results.

    Important noteImportant

    You must have run Team Foundation Build at least once before you can upload test results that you gathered locally.

In this walkthrough, you will perform the following tasks:

  1. Create a simple class library that contains a class with a few members

  2. Create unit tests to verify the behavior of those members, group those tests into a test list, and run the tests to verify that they run successfully

  3. Check in your code and unit tests to version control

  4. Use Team Foundation Build to build your library and run unit tests

  5. Modify your local source code and identify the affected tests

You must have installed Team System Development, you must be connected to Visual Studio Team System Team Foundation Server, and you must have the required permissions to add files to version control and to create and run a build definition.

To create a class library project

  1. On the File menu, point to New, and click New Project.

    The New Project dialog box appears.

  2. In Project types, click Visual C#.

  3. In Templates, click Class Library.

  4. In Name, type MyMathLibrary, click OK.

    In Solution Explorer, the MyMathLibrary project appears, and the source code for the class opens in the code editor.

    Next you will update the class definition to contain several methods.

To define a simple class

  1. In the code editor, update the class definition to match the following statements:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    Namespace MyMathLibrary
    {
        public class MathClass
        {
            public MathClass()
            {
            }
            public int Add(int aValue, int toValue)
            {
                int returnVal = 0;
                returnVal = aValue + toValue;
                return (returnVal);
            }
            public int Subtract(int aValue, int fromValue)
            {
                int returnVal = 0;
                returnVal = fromValue - aValue;
                return (returnVal);
            }
            public int Fancy(int aValue, int bValue, int cValue)
            {
                int returnVal = 0;
                // returns c - (a + b)
                returnVal = this.Subtract(cValue, this.Add(aValue, bValue));
                return (returnVal);
            }
            public int IntDivide(int dividend, int divisor)
            {
                int returnVal = 0;
                // Note: the following statement will fail
                // if divisor is zero.
                returnVal = dividend / divisor;
                return (returnVal);
            }
        }
    }
    
    
  2. On the File menu, click Save Class1.cs.

    Next, you will define unit tests to verify the behavior of these methods.

To generate unit tests for your methods

  1. In the code editor, click in the definition of the Add method, and click Create Unit Tests.

    The Create Unit Tests dialog box appears.

  2. In the Types list, select the check boxes that correspond to the Add, IntDivide, and Subtract methods.

  3. In Output Project, click Create a new Visual C# test project, and click OK.

    The New Test Project dialog box appears.

  4. In Enter a name for your new project, type MyMathTestProject, and click Create.

    The test project is added to your solution, and the code for the unit test appears in the code editor. The project contains methods to test each method that you specified earlier in this procedure. Next, you modify the generated methods to perform a simple verification of the behavior of each method.

    NoteNote

    When you create tests for real-world applications, you typically create more than one test for each method. The tests in this walkthrough are kept brief to minimize the length of the walkthrough.

To modify the unit test methods to verify your code

  1. In the code editor, modify the definition for the AddTest method so that it matches the following statements:

            /// <summary>
            ///A test for Add
            ///</summary>
            [TestCategory("BasicMath")]
            [TestMethod()]
            public void AddTest()
            {
                MathClass target = new MathClass();
                int aValue = 6;
                int toValue = 8;
                int expected = 14;
                int actual;
                actual = target.Add(aValue, toValue);
                Assert.AreEqual(expected, actual);
            }
    
    
  2. In the code editor, modify the definition for the SubtractTest method so that it matches the following statements:

            [TestCategory("BasicMath")]
            [TestMethod()]
            public void SubtractTest()
            {
                MathClass target = new MathClass(); 
                int aValue = 6; 
                int fromValue = 8; 
                int expected = 2; 
                int actual;
                actual = target.Subtract(aValue, fromValue);
                Assert.AreEqual(expected, actual);
            }
    
    
  3. In the code editor, modify the definition for the FancyTest method so that it matches the following statements:

            /// <summary>
            ///A test for Fancy
            ///</summary>
            [TestCategory("BasicMath")]
            [TestMethod()]
            public void FancyTest()
            {
                MathClass target = new MathClass();
                int aValue = 6;
                int bValue = 8;
                int cValue = 2;
                int expected = 12;
                int actual;
                actual = target.Fancy(aValue, bValue, cValue);
                Assert.AreEqual(expected, actual);
            }
    
    
  4. In the code editor, modify the definition for the IntDivideTest method so that it matches the following statements:

            [TestCategory("BasicMath")]
            [TestMethod()]
            public void IntDivideTest()
            {
                MathClass target = new MathClass(); 
                int dividend = 100;
                int divisor = 10;
                int expected = 10;
                int actual;
                actual = target.IntDivide(dividend, divisor);
                Assert.AreEqual(expected, actual);
            }
    
    
  5. In the code editor, add the following method definition to define a second test for the IntDivide method:

            [TestCategory("BasicMath")]
            [TestMethod()]
            public void IntDivideTruncateTest ()
            {
                MathClass target = new MathClass(); 
                int dividend = 33;
                int divisor = 10;
                int expected = 3;
                int actual;
                actual = target.IntDivide(dividend, divisor);
                Assert.AreEqual(expected, actual);
            }
    
    
  6. On the File menu, click Save MathClassTest.cs.

    Next, you create a test list that contains your unit tests. You create this list because you must specify it when you run tests from Team Foundation Build.

To enable collection of data about test impact

  1. In Solution Explorer, double-click Local.testsettings.

  2. In the navigation list, click Execution Criteria.

  3. In the Collectors list, select the Test Impact Collector check box, click Apply, and then click OK.

    Next, you will run your unit tests locally to verify that they work correctly before you check in your code and tests to version control.

To build and run your unit tests

  1. On the Test menu, point to Windows, and click Test View.

  2. In Test View, highlight all of the tests, right-click any test, and then click Run Selected Tests.

    The Test Results window displays the result for each test as it is completed. All tests should pass successfully. Next you check in your code and your unit tests.

To add your solution to version control

  1. In Solution Explorer, click the Solution 'MyMathLibrary' node.

  2. On the File menu, point to Source Control, and click Add Solution to Source Control.

    The Add Solution MyMathLibrary to Source Control dialog box appears.

  3. In Team Project Location, click the team project and folder to which you want to add your solution, and then click OK.

    The files in your solution are added to version control. Next, you check in all pending changes.

To check in all pending changes

  1. In the Pending Changes window, in the Comment field, type Initial check-in of the solution.

  2. On the toolbar for the Pending Changes window, click Check In.

    Your files are checked in to version control, and the Pending Changes window displays no pending changes. Next you build your solution and run your unit tests.

To create a build definition

  1. In Team Explorer, right-click the Builds node, and click New Build Definition.

    The New Build Definition window appears.

  2. In Build definition name, type MyMathBuild.

  3. In the navigation bar, click Build Defaults.

  4. In Copy build output to the following drop folder (UNC path, such as \\server\share), specify a folder to contain the build output.

    You can specify a shared folder on your local computer or on any network location to which the build process will have permissions.

  5. In the navigation bar, click Process, and scroll to the Compilation group.

  6. In Projects to Build, click the browse (…) button.

  7. In the Build Project List Editor dialog box, click Add.

  8. Specify the solution file (.sln) that you added to version control earlier in this walkthrough, and click OK.

    The solution appears in the Project or solution files to build list.

  9. Click OK.

  10. In the Testing group, verify that Analyze Test Impacts is set to True.

  11. In Test Assembly Filespec, type **\*test*.dll.

  12. In Test Category, type BasicMath.

  13. In Test Container TestSettings File, click the browse (…) button.

  14. In the Browse dialog box, click the Local.testsettings file, and then click OK.

  15. On the File menu, click Save MyMathBuild.

    You have created a build definition. Next you run the new build.

To run the build definition

  1. In Solution Explorer, open the Builds folder, right-click MyMathBuild, and click Queue New Build.

    The Queue Build "TeamProjectName" dialog box appears, where TeamProjectName is the name of the team project to which you added your build definition.

  2. Click Queue.

    The Build Explorer window appears and shows the build status. The build should complete successfully.

    Next, you will change the local version of your code, and then you will identify the tests that you must run to verify that change.

To modify your code and identify affected tests

  1. In Solution Explorer, double-click Class1.cs.

    The code for MathClass appears in the code editor.

  2. On the Test menu, point to Windows, and click Test Impact View.

    Because you have not yet changed any code, no tests appear in the Test Impact View window.

  3. In the code editor, add the following statement to the top of Class1.cs:

    using System.Diagnostics;
    
    
  4. In the code editor, update the definition of the IntDivide method so that it matches the following statements:

            public int IntDivide(int dividend, int divisor)
            {
                Debug.Assert(divisor != 0);
                int returnVal = 0;
                if (divisor != 0)
                {
                    // Note: the following statement will fail
                    // if divisor is zero.
                    returnVal = dividend / divisor;
                }
                return (returnVal);
            }
        }
    
    
    
  5. On the File menu, click Save Class1.cs.

    When you save the file, the Test Impact View window shows the tests that you must run to verify your changes. In the Impacted Tests list, IntDivideTest and IntDivideTruncateTest appear.

  6. In the code editor, update the definition of the Add method so that it matches the following statements:

            public int Add(int aValue, int toValue)
            {
                // change initialization of returnVal
                int returnVal = 1;
                returnVal = aValue + toValue;
                return (returnVal);
            }
    
    
  7. On the File menu, click Save Class1.cs.

    When you save the file, the Test Impact View window shows the tests that you must run to verify your changes. AddTest and FancyTest appear in the window in the Impacted Tests list.

  8. In the Test Impact View window, click FancyTest.

    In the Changed Methods list, Add(int, int) appears. You must re-run the tests for the Fancy method because the Fancy method calls the Add method.

    Next, you re-run those tests locally to verify that your changes have not caused any affected unit tests to fail.

To verify your code changes

  • In the toolbar of the Test Impact View window, click Run All Impacted Tests.

    The unit tests run and pass. The Test Impact View window updates, and those tests appear in the Verified Tests list.

    At this point, you could check in your pending changes because you have verified them.

When you change your own applications, you might author additional unit tests to verify those changes. For example, you could add a negative test for the code in this walkthrough to verify that the IntDivide method fails appropriately when a zero dividend is passed to it.

Show:
© 2014 Microsoft