Monday, March 26, 2012

Unit Testing with Test Cases in MTM

In Microsoft Test Manager, you can store test cases that contain steps, and parameters for testing. One of the great advantages to using Microsoft Unit Testing Framework, is you can have a test look at the parameters of a test case, and use those parameters to run automated tests. This way all of the critical information is stored between the test case and the automation.

First start by creating a Test Case in Microsoft Test Manager. In the test case outline the simple steps that your automation will be following. Example:



Notice in the above image I have identified each of the steps in my test. I have also identified the parameters I am going to use for my testing, and the validating parameters needed to assert if the test passes or fails. The parameters are preceded with an @ symbol in the steps section, and for each parameter identified in the steps section, there is a correlating column in the parameter section of the test case. In this section I have put in all of the different variations that I want to test for. In this example I am testing a search function which has 3 different filters Age, Gender, Ethnicity, and also a search text box. I am testing each of the different fields by running through a series of tests.
Now that the test case has all of the parameters, and scenarios identified we now create the automation needed to run the scenarios. When creating the automation you will need to have the following.
1. Make sure your test class is using the using the reference Microsoft.VisualStudio.TestTools.UnitTesting.

2. Instantiate an instance of TestContext. Example:

private TestContext m_testContext;
public TestContext TestContext
{
get { return m_testContext; }
set { m_testContext = value; }
}

2. On or with your [TestMethod] add a DataSource() attribute. Template:

Example:
[TestMethod]
[DataSource(
"Microsoft.VisualStudio.TestTools.DataSource.TestCase",
"http://icstfs.blogspot.org:8080/tfs/blogspot;Quality Management", //TFS Server
"77300", //Test Case ID
Microsoft.VisualStudio.TestTools.UnitTesting.DataAccessMethod.Sequential)]

3. Call the parameters from the test case into your method using TestContext.DataRow[].ToString(); For example:

public void SearchPerson()
{
string gender = TestContext.DataRow["Gender"].ToString();
string age = TestContext.DataRow["Age"].ToString();
string ethnicity = TestContext.DataRow["Ethnicity"].ToString();
string searchText = TestContext.DataRow["SearchText"].ToString();
string validPerson = TestContext.DataRow["ValidPerson"].ToString();
string invalidPerson = TestContext.DataRow["InvalidPerson"].ToString();
string validDescription = TestContext.DataRow["ValidDescription"].ToString();
if(!_meetMormonFilter.IsMeetMormonsVisible())

{
_menu.GoToHome(); //If Meet Mormon wigit isn't visible go to home page
}

_meetMormonFilter.FindPeople(gender, age, ethnicity, searchText); //Find a person using the wigit
_peopleSearchResults.ViewByListClick(); //View the search to list to see names and descriptions

Assert.IsTrue(_peopleSearchResults.ListFinderHasPerson(validPerson),"Person: "+validPerson+" Not found in grid"); //Check person name is in grid
Assert.IsFalse(_peopleSearchResults.ListFinderHasPerson(invalidPerson),"Person: "+invalidPerson+" Is found in grid and should not be"); //Check a invalid person is not in grid
string description = _peopleSearchResults.GetProfilDescription(validPerson); //Get Description associated with Valid User
Assert.IsTrue(description.Contains(validDescription), "Description for Person: " + validPerson + " did not contain phrase: "+validDescription);//Check that description contains the text stored in Valid Description
_peopleSearchResults.ListFinderSelectProfile(validPerson); //Go to the User Profile
}

Finally with the automation linked to the test case, visual studio will run the test once for each row of parameters that you have. After the test is done running you will see one test that has failed or passed in the Test Result. However if you double click on the test result, you will be able to see all of the tests ran.



2 comments:

  1. Hello,
    The Article on Unit Testing with Test Cases in MTM is very informative. It give detail information about it .Thanks for Sharing the information on Unit Testing. mobile application testing

    ReplyDelete
  2. Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Visit here for Penetration testing services

    ReplyDelete