Export (0) Print
Expand All

Walkthrough: Create And Run Unit Tests As Part of a Team Build

Visual Studio 2008

This topic presents the steps to create a C# project, generate unit tests to exercise a particular method in this project, bind the project's source files to a Team Foundation server, and then build the project by using Team Foundation Build. By following these steps, you can configure Team Foundation Build so that the unit tests run as part of the build.

Prerequisites

Required Permissions

To complete this walkthrough, you must have the Administer a build and Administer workspaces permission set to Allow. You must also have the Check in and Check out permissions set to Allow. To create a team project, you must have the Create new projects permission set to Allow, and you must be a member of the SharePoint Central Admins group in Windows SharePoint Server and have Content Manager permissions in SQL Reporting Services.

You must have install permission on the computer on which you run the installer created by Team Foundation Build.

In this walkthrough, you perform the following procedures:

Creating a C# application

Adding units tests to the application

Organize unit tests in a test list

Run unit tests as part of a Team Foundation Build

To create the bank account application

  1. Open Visual Studio Team System Test Edition.

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

  3. In the New Project dialog box, under Project Types, click Visual C#.

  4. Under Templates, click Class Library.

  5. In the Name box, type BankAccount.

  6. Accept the default location and solution name for the solution.

  7. Select the Add To Source Control check box, and then click OK.

    The new Bank Account project is created and shown in Solution Explorer.

  8. In Solution Explorer, double-click the file Class1.cs.

  9. Replace the original contents of Class1.cs with the following code:

using System;
namespace BankAccountNS
{
    /// <summary> 
    /// Bank Account demo class. 
    /// </summary> 
    public class BankAccount
    {
        public enum accountType
        {
            Gold,
            Platinum,
        }

        public BankAccount(string customerName, double balance)
        {
            m_customerName = customerName;
            m_balance = balance;
        }

        public string CustomerName
        {
            get { return m_customerName; }
        }

        public double Balance
        {
            get { return m_balance; }
        }

        public accountType GetAccountType
        {
            get
            {
                if (Balance > 15.00)
                    return accountType.Platinum;
                else
                    return accountType.Gold;
            }
        }
        private string m_customerName;
        private double m_balance;
    }
}

/* The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious.  No association with any real company, organization, product, domain name, email address, logo, person, places, or events is intended or should be inferred. */
  1. Save Class1.cs.

To create unit tests to exercise the GetAccountType() method

  1. In Class1.cs, find the public method name GetAccountType(), right-click it, and then click Create Unit Tests.

  2. In the Create Unit Tests dialog box, expand the BankAccount project node to confirm that the GetAccountType check box is selected.

  3. Click OK.

  4. In the New Test Project dialog box, accept the default name, and click Create.

  5. In Solution Explorer, under TestProject1, double-click BankAccountTest.cs.

  6. Find the method GetAccountTypeTest() and change it to match the following code:

        [TestMethod()]
        public void GetAccountTypeTest()
        {
            string customerName = "Mr. Bryan Walton";
            double balance = 15.01; 
            BankAccount target = new BankAccount(customerName, balance); 
            BankAccount.accountType actual;
            actual = target.GetAccountType;
            Assert.AreEqual(actual, BankAccount.accountType.Platinum);
        }
  1. Return to the GetAccountType() method in Class1.cs, right-click GetAccountType(), and then click Create Unit Tests.

    The Create Unit Tests dialog box appears with GetAccountType selected.

  2. Click OK.

    This action creates another test method in BankAccountTest.cs called GetAccountTypeTest1().

  3. Open BankAccountTest.cs, find GetAccountTypeTest1(), and change it to match the following code:

        [TestMethod()]
        public void GetAccountTypeTest1()
        {
            string customerName = "Mr. Bryan Walton";
            double balance = 14.99; 
            BankAccount target = new BankAccount(customerName, balance); 
            BankAccount.accountType actual;
            actual = target.GetAccountType;
            Assert.AreEqual(actual, BankAccount.accountType.Gold);
        }
  1. Save BankAccount.cs.

Create a test list and check it in to Team Foundation source control

  1. On the Test menu, point to Windows, and then click Test List Editor.

  2. Right-click the List of Tests node, and then click New Test List.

  3. In the Create New Test List dialog box, in the Name box, type UnitTests, and then click OK.

    The new test list appears in the test list hierarchy pane.

  4. Click All Loaded Tests to show the two unit tests that you created earlier.

    NoteNote:

      Do not select the check boxes next to the tests.

  5. Drag both tests into the newly-created UnitTests list.

  6. Select the UnitTests check box.

  7. On the Test menu, point to Run, and then click Tests in Current Context.

    The Test Results pane shows the results of the tests. Both unit tests should pass. If they do not, check the source code to ensure that it matches the code that was provided earlier in this exercise.

  8. Check in the test project along with the other C# code to Team Foundation version control by performing the following steps:

    1. On the View menu, point to Other Windows, and then click Pending Changes.

    2. In the Pending Changes pane, click Check In.

To configure a build definition to run your unit tests

  1. On the View menu, click Team Explorer.

  2. In Team Explorer, select the team project that holds the source-controlled C# solution files and test list.

  3. On the Build menu, click New Build Definition.

  4. In the Build definition name box, type UnitTestsBuildDefinition.

  5. Click the Project File tab.

  6. Click Create.

  7. In the MSBuild Project File Creation Wizard, select the C# solution that you checked into version control: BankAccount.sln, clear the selections for any other files, and then click Next.

  8. Click Next to accept the default configuration settings.

  9. On the Options tab, select the Run test check box.

  10. Under Test list to run, select UnitTests, and then click Finish.

  11. Click the Build Defaults tab, and in the Build agent list select a working build agent.

  12. In the Builds will be staged to the following share box, type a build staging location, and then click OK to create the build definition.

  13. In Team Explorer, expand the Builds folder for the team project, and then click the name of the build definition that you just created.

  14. On the Build menu, click Queue New Build.

  15. In the Queue Build dialog box, click Queue.

    The Build Explorer pane displays a list of queued build definitions. The build that you just queued will be in the list.

  16. When the build finishes, click the Completed tab to display completed builds.

    Your build definition should have a check mark in a green circle next to it, which signifies that the build succeeded.

  17. To verify that your two unit tests passed as part of the build, right-click the build definition name, and then click Open.

  18. Expand the Result details for Any CPU/Release node.

    On the Test results line, a summary of the test results appears.

Community Additions

ADD
Show:
© 2014 Microsoft