Click here to Skip to main content
14,843,172 members
Articles / Programming Languages / C#
Technical Blog
Posted 20 Feb 2015

Tagged as

Stats

19.2K views
16 bookmarked

TFS Associate Automated Test with Test Case C# Code

Rate me:
Please Sign up or sign in to vote.
4.87/5 (12 votes)
15 Sep 2015CPOL2 min read
Associate Automated Test with TFS Test Case C# VB.NET Code

Introduction

To associate TFS Test Cases to automated tests via code, you should first create TFS Collection and Team Test Project. You can find more information in my previous post: “How to: Connect to TFS Team Project C# Code“.

Next you need to connect to a particular test plan. You can read more in the article: “Manage TFS Test Plans in MS Test Manager C# Code“. Crud operations with TFS Test Cases are discussed in my other post: “Manage TFS Test Cases in MS Test Manager C# Code“.

Image 1

How to Get Association Information from TFS Test Case Object?

First, you need to create an appropriate wrapper class for the automation properties - TestName, Assembly and Type.

public class AssociatedAutomation
{
    public const string NONE = "None";

    public AssociatedAutomation()
    {
        this.TestName = NONE;
        this.Assembly = NONE;
        this.Type = NONE;
    }

    public AssociatedAutomation(string testInfo)
    {
        string[] infos = testInfo.Split(',');
        this.TestName = infos[1];
        this.Assembly = infos[2];
        this.Type = infos[3];
    }

    public string Type { get; set; }

    public string Assembly { get; set; }

    public string TestName { get; set; }

    public override string ToString()
    {
        return string.Format("Type: {0} Assembly: {1} TestName: {2}", this.Type, this.Assembly, this.TestName);
    }
}

In order to create the object from an already associated TFS Test Case, use the following extension method to the core TFS Test Case entity ITestCase.

C#
public static AssociatedAutomation GetAssociatedAutomation(this ITestCase testCase)
{
    AssociatedAutomation associatedAutomation;
    if (testCase.Implementation == null)
    {
        associatedAutomation = new AssociatedAutomation();
    }
    else
    {
        associatedAutomation = new AssociatedAutomation(testCase.Implementation.ToString());
    }

    return associatedAutomation;
}

The ITestCase association property Implementation is just a string like the following:

10fd7a26-d91a-ddef-eea9-9a85ae81df7c,
WebDivision.UITests.UITestCases.TestCases.TeamPulseOnsiteTrainingForm.Frontend.AccountTickets.
SubmitTicketProductReplyAttachFile,webdivision.uitests.uitestcases.dll,Unit Test

The string uses the following format - MethodId, TestName, Assembly, Type.

How to Create Association in Code?

Before the actual association, you will need one more class. An abstraction around the properties of the automated test like full name, class name and method id.

C#
[Serializable]
public class Test
{
    public Test(string fullName, string className, Guid methodId)
    {
        this.FullName = fullName;
        this.ClassName = className;
        this.MethodId = methodId;
    }

    public string FullName { get; set; }

    public string ClassName { get; set; }

    public Guid MethodId { get; set; }

    public override string ToString()
    {
        return String.Format("{0}, {1}, {2}", this.FullName, this.ClassName, this.MethodId);
    }
}

The actual code for association is pretty simple. We use again an extension method to ITestCase.

C#
public static void SetAssociatedAutomation(this ITestCase testCase, ITestManagementTeamProject testManagementTeamProject, Test testForAssociation, string testType)
{
    try
    {
        log.InfoFormat("Associate test case with title= {0}, id= {1} to test: {2}, test type= {3}", testCase.Title, testCase.Id, testForAssociation, testType);
        ITmiTestImplementation imp = testManagementTeamProject.CreateTmiTestImplementation(testForAssociation.FullName, testType, testForAssociation.ClassName, testForAssociation.MethodId);
        testCase.Implementation = imp;
    }
    catch (NullReferenceException ex)
    {
        log.Error(ex);
    }
}

We use the properties previously initialized in our Test object in order to create the internal TFS entity ITmiTestImplementation which holds the association information. Then, we assigned it to our TFS Test Case. The data will be persisted when the test case object is saved. In order to prevent exceptions, you need to pass a valid information. The first three parameters- fullName, className, testType are easy to get. The method id is the hardest to obtain. Below, I will show you how to generate it.

public static Guid GenerateTestMethodId(MethodInfo methodInfo)
{
    string currentNameSpace = methodInfo.DeclaringType.FullName;
    string currentTestMethodShortName = methodInfo.Name;
    string currentTestMethodFullName = string.Concat(currentNameSpace, ".", currentTestMethodShortName);
    Guid testId = GuidFromString(currentTestMethodFullName);

    return testId;
}
     
private static Guid GuidFromString(string data)
{
    byte[] hash = cryptoServiceProvider.ComputeHash(System.Text.Encoding.Unicode.GetBytes(data));

    byte[] toGuid = new byte[16];
    Array.Copy(hash, toGuid, 16);

    return new Guid(toGuid);
}

Using reflection, you can get the metadata of your test method - stored in the reflection type MethodInfo. The method ID is as you can see a GUID created from the special hash value of the full name of the method.

You can find more information about how to associate TFS Test Cases to tests in my blog post: “Automated Test with TFS Test Case MS Test Manager“.

So Far in the TFS API Series

1. Connect to TFS Team Project C# Code
2. Manage TFS Test Plans C# Code
3. Manage TFS Test Cases C# Code
4. Manage TFS Test Suites C# Code
5. TFS Associate Automated Test with Test Case C# Code
6. Test Cases Statistics with SSRS and TFS Data Warehouse
7. SSRS SQL Server Reporting Services- Subscriptions for Reports

 

If you enjoy my publications, feel free to SUBSCRIBE
Also, hit these share buttons. Thank you!

Source Code

 

The post - Associate Automated Test with TFS Test Case C# VB.NET Code appeared first on Automate The Planet.

All images are purchased from DepositPhotos.com and cannot be downloaded and used for free. License Agreement

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Anton Angelov
CEO Automate The Planet
Bulgaria Bulgaria
CTO and Co-founder of Automate The Planet Ltd, inventor of BELLATRIX Test Automation Framework, author of "Design Patterns for High-Quality Automated Tests: High-Quality Test Attributes and Best Practices" in C# and Java. Nowadays, he leads a team of passionate engineers helping companies succeed with their test automation. Additionally, he consults companies and leads automated testing trainings, writes books, and gives conference talks. You can find him on LinkedIn every day.

Comments and Discussions

 
QuestionGuid Pin
Elie El-Tawil26-Jan-16 3:17
MemberElie El-Tawil26-Jan-16 3:17 
AnswerRe: Guid Pin
Anton Angelov28-Jan-16 10:18
MemberAnton Angelov28-Jan-16 10:18 
Hi Elie,

I think it is necessary to use the FullName and the Name. Actually, of course, you can generate assign random guide but in my opinion, the labels in the test case application will not be as expected. The parameter of the last mentioned function is the test method information that you want to assign to a test case, you get it via Reflection.

-Anton

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.