Configuring the “system under test” in VS

The system under test for the purpose of this walkthrough is a simple calculator web service with its unit tests.

Start by creating a new team project in VS. Connect to the TFS server and create a team project (Labwalkthru). The solution is attached to the blog post.

Calculator app:

  1: using System.Web.Services;
  2: 
  3: [WebService(Namespace = "http://tempuri.org/")]
  4: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  5: public class CalculatorService : System.Web.Services.WebService
  6: {
  7:     public CalculatorService()
  8:     {
  9:     }
 10: 
 11:     [WebMethod]
 12:     public long Add(long lNum1, long lNum2)
 13:     {
 14:         return lNum1 + lNum2;
 15:     }
 16: 
 17:     [WebMethod]
 18:     public long Subtract(long lNum1, long lNum2)
 19:     {
 20:         return lNum1 - lNum2;
 21:     }
 22: 
 23:     [WebMethod]
 24:     public long Multiply(long lNum1, long lNum2)
 25:     {
 26:         return lNum1 * lNum2;
 27:     }
 28: 
 29:     [WebMethod]
 30:     public long Divide(long lNum1, long lNum2)
 31:     {
 32:         return lNum1 / lNum2;
 33:     }
 34: }

Test code:

  1: using Microsoft.VisualStudio.TestTools.UnitTesting;
  2: using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
  3: 
  4: namespace CalcTest
  5: {
  6:     /// <summary>
  7:     ///This is a test class for CalculatorServiceTest and is intended
  8:     ///to contain all CalculatorServiceTest Unit Tests
  9:     ///</summary>
 10:     [TestClass()]
 11:     public class CalculatorServiceTest
 12:     {
 13:         private TestContext testContextInstance;
 14: 
 15:         /// <summary>
 16:         ///Gets or sets the test context which provides
 17:         ///information about and functionality for the current test run.
 18:         ///</summary>
 19:         public TestContext TestContext
 20:         {
 21:             get
 22:             {
 23:                 return testContextInstance;
 24:             }
 25:             set
 26:             {
 27:                 testContextInstance = value;
 28:             }
 29:         }
 30:         
 31:         /// <summary>
 32:         ///A test for Add
 33:         ///</summary>
 34:         [TestMethod()]
 35:         [HostType("ASP.NET")]
 36:         [UrlToTest("http://localhost/Calc")]
 37:         public void AddTest()
 38:         {
 39:             CalculatorService_Accessor target = new CalculatorService_Accessor(); 
 40:             long lNum1 = 1;
 41:             long lNum2 = 1;
 42:             long expected = 2;
 43:             long actual;
 44:             actual = target.Add(lNum1, lNum2);
 45:             Assert.AreEqual(expected, actual);
 46:         }
 47: 
 48:         /// <summary>
 49:         ///A test for Subtract
 50:         ///</summary>
 51:         [TestMethod()]
 52:         [HostType("ASP.NET")]
 53:         [UrlToTest("http://localhost/Calc/Default.aspx")]
 54:         public void SubtractTest()
 55:         {
 56:             CalculatorService_Accessor target = new CalculatorService_Accessor();
 57:             long lNum1 = 5;
 58:             long lNum2 = 2;
 59:             long expected = 3;
 60:             long actual;
 61:             actual = target.Subtract(lNum1, lNum2);
 62:             Assert.AreEqual(expected, actual);
 63:         }
 64: 
 65:         /// <summary>
 66:         ///A test for Multiply
 67:         ///</summary>
 68:         [TestMethod()]
 69:         [HostType("ASP.NET")]
 70:         [UrlToTest("http://localhost/Calc/Default.aspx")]
 71:         public void MultiplyTest()
 72:         {
 73:             CalculatorService_Accessor target = new CalculatorService_Accessor(); 
 74:             long lNum1 = 2;
 75:             long lNum2 = 5;
 76:             long expected = 10;
 77:             long actual;
 78:             actual = target.Multiply(lNum1, lNum2);
 79:             Assert.AreEqual(expected, actual);
 80:         }
 81: 
 82:         /// <summary>
 83:         ///A test for Divide
 84:         ///</summary>
 85:         [TestMethod()]
 86:         [HostType("ASP.NET")]
 87:         [UrlToTest("http://localhost/Calc/Default.aspx")]
 88:         public void DivideTest()
 89:         {
 90:             CalculatorService_Accessor target = new CalculatorService_Accessor();
 91:             long lNum1 = 10;
 92:             long lNum2 = 2;
 93:             long expected = 5;
 94:             long actual;
 95:             actual = target.Divide(lNum1, lNum2);
 96:             Assert.AreEqual(expected, actual);
 97:         }
 98:     }
 99: }
100: 

When extracting the solution from the zip file, you can extract the attached zip file and from an elevated command prompt

C:\calc>AddCalctoIIS.cmd
APP object "Default Web Site/Calc" added
VDIR object "Default Web Site/Calc" added
added new IIS VDir
APP object "Default Web Site/Calc" changed
set the app pool to run under ASP.NET v4.0
processed file: C:\calc\Calc
Successfully processed 1 files; Failed processing 0 files
setup the right permissions

Add the solution to the Labwalkthru project in source control and build it. For running these tests in a remote lab environment, these tests to be part of the TCM in TFS. Import the test cases to TCM by using the tcm command. Navigate to the Visual Studio Command Prompt (2010) from Start->All Programs –> Microsoft Visual Studio 2010 –>Visual Studio Tools.

C:\calc\calctest\bin\Debug>

         

tcm testcase /import /collection:http://varadatfs:8080/tfs/DefaultCollection /teamproject:Labwalkthru /storage:CalcTest.dll


Modification   Title
-------------- -----------------------------------------------------------
Added          AddTest
Added          SubtractTest
Added          MultiplyTest
Added          DivideTest
Summary
--------------
Added          4
Updated        0
Removed        0
Total          4

 

Create the rest of the Test assets:

We should provide the BVT experience a test suite to run, so launch the “Microsoft Test and Lab Manager” and connect to the project.

clip_image002

Create a test plan by clicking on Add

clip_image004

Let us call it “BVT tests” and select the “Select plan”

clip_image006

In the tests add the tests that we just imported

clip_image008

Create a Lab Environment for running the tests:

In MTLM, move from test center to Lab center.

image

Compose a new virtual environment

image

Composing a virtual environment is another new feature in the RC build. The idea here is to get started with lab faster using existing active virtual machines already present in the organization.

image 

and in the machine tab, select the test VM, set its role to “Web Server”

image 

Add “Run test” and “Run workflow” capabilities

image 

and click the “Finish” button. This will configure the build and test agent on the VM to work with the TFS system. When complete, you should see the environment, with the workflow capability and test capability in ready state.

image

In our case, on the environment, we are running web tests as NetworkService (the test agent is configured by default to run as networkservice). To run tests in ASP.NET, the user has to be administrator on the machine. So connect to the lab machine,

image

logon to the VM and add Networkservice as member of the administrators group and then reboot the machine.

C:\>net localgroup administrators /add "NT AUTHORITY\Network Service"

Reboot the machine to allow for this to take effect.

When the tests run, you would want to restore the environment to a clean snapshot. Take the snapshot, by clicking on the “Take Snapshot” icon.

image

Let us call the Snapshot, “CleanCP”

Create test settings for the automated run:

Create a new test setting and set the test run to automated.

clip_image028

In the Roles tab, select the web server role to be the role where tests are run. This is needed in case where your environment has multiple roles, you can pick the right role where the tests get run.

image

And finish.

In the next blog post, we will connect all the dots to execute the test runs as part of the development workflow.