How to get the testId generated by Visual Studio in a .loadtest file

During these days I have been working on a tool that automates the execution of a load test.

Our objective was to parse a .loadtest template file, based on a template, which has been configured for a specific scenario with a set of counters. Once we have parsed the template we are ready to add the TestMix node and a TestProfile node for each test that we want to execute from those defined on test assembly.

The Obstacle

When I was creating the TestProfile node, I realized that each unit test has an specific Id (represented by a unique identifier a.k.a Guid). What I didn't noticed until we've run the load test is that this identifier was generated dynamically by Visual Studio.

We tried going under the hood , unluckily, using reflector to analyze some of Visual Studio assemblies in order to find how these identifiers were generated.

The suggested workaround

Searching on the web I found a post with a method which generates the Guid we’re looking for:

private static Guid GetGuidFromString(string value)
{
    SHA1CryptoServiceProvider provider = new SHA1CryptoServiceProvider();
    byte[] buffer1 = provider.ComputeHash(Encoding.Unicode.GetBytes(value));
    byte[] buffer2 = new byte[0x10];
    Array.Copy(buffer1, buffer2, 0×10);
    return new Guid(buffer2);
}

Using the recently created function we’re able to retrieve the Guid for a given assembly as I’m showing below

private static IList<TestEntry> GetUnitTests()
{
    IList<TestEntry> unitTests = new List<TestEntry>();
    Assembly assembly = Assembly.LoadFrom(Tests\\PerformanceTests.dll);
    Type[] assemblyTypes = assembly.GetTypes(); 
 
    foreach (Type type in assemblyTypes)
    {
        if (type.IsClass && 
            type.GetCustomAttributes(typeof(TestClassAttribute), false).Length > 0)
        {
            MethodInfo[] info = type.GetMethods(); 
 
            foreach (MethodInfo m in info)
            {
                if (m.GetCustomAttributes(typeof(TestMethodAttribute), false).Length > 0)
                {
                    Guid testGuid = GetGUIDFromString(type.FullName + . + m.Name);
                    unitTests.Add(new TestEntry(testGuid, m.Name));
                }
            }
        }
    } 
 
    return unitTests;
}

Hope you find it useful!

Published: October 17 2007

  • category:
blog comments powered by Disqus