Ines's profileInès' CribPhotosBlogListsMore Tools Help

Blog


    August 27

    Reflect On This!

    DISCLAIMER: C# Snippet. If you are not interested in programming languages and Reflection, skip this post.
    I love C#! What a simple language to use . Yes, better than Java in my opinion...
     
    I work on MSN Messenger, and as you might have guessed, writing test cases often involves automating 2-machine scenarios: Signing in into Messenger on one computer with an account, then signing in into Messenger one another computer with another account. Often times, the users (local and remote) information can be stored in an XML file. So I had a choice to:
    1. Generate XML files by hand (one per test case), containing a user name and password for each machine
    2. Write a quick snippet that automatically gnerates the hotmail account strings for the accounts on each machine (local and remote) using the test case names.

    I opted for option 2 . If you care, I have posted the snippest that traverse the test case DLLs, and using reflection, retrieve each test case name and use part of that name to generate (just the string) a local and remote hotmail account to be written to the XML file.

    Sample Output (where D00 represents an ID for a test case in a test case group):

    <TestCaseGroupName>
         <Setting Name="LocalEmailAddress" Value="
    D00_local@hotmail.com" Description="Email address for local user account." />
        <Setting Name="LocalPassword" Value="password" Description="Password for local user account." />
        <Setting Name="RemoteEmailAddress" Value="
    D00_remote@hotmail.com" Description="Email address for remote user account." />
        <Setting Name="RemotePassword" Value="password" Description="Password for the remote user." />
    </TestCaseGroupName>

     
    DISCLAIMER: Obviously, the above are not REAL hotmail accounts!
     
    Look for a "TestCaseGroup" attribute in DLLs specified in command line (args):
    Assembly currentAssembly = null; 
    Module[] moduleList = null; 
    Type[] typeList = null; 
    object[] attributeList = null; 
    
    // Find types of TestCaseGroup attribute 
    foreach (string assemblyName in args) 
    { 
        currentAssembly = Assembly.LoadFile(ASSEMBLY_PATH + assemblyName); 
        moduleList = currentAssembly.GetModules(); 
        // Traverse assembly modules 
        foreach (Module module in moduleList) 
        { 
            // Get types for the module 
            typeList = module.GetTypes(); 
            foreach (Type type in typeList) 
            { 
                // Find class with TestCaseGroup attribute 
                attributeList = type.GetCustomAttributes(typeof(TestCaseGroup), true); 
                if (attributeList != null && attributeList.Length != 0) 
                { 
                    // Create XML file fo Type type 
                } 
            } 
        } 
    }
    Create XML File using the test case name given the TestCaseGroup class type:
    string[] xmlArguments = null; 
    string testCaseName = string.Empty; 
    string testCaseID = string.Empty; 
    object[] attributeList = null; 
    
    // Get methods from the type (class) 
    MethodInfo[] methodList = type.GetMethods(); 
    
    // Traverse the methods in the class 
    foreach (MethodInfo method in methodList) 
    { 
        // Get attributes for the mtehod 
        attributeList = method.GetCustomAttributes(typeof(TestCase), true); 
        if (attributeList.Length != 0) 
        { 
            // Find a TestCase attribute 
            foreach (object attribute in attributeList) 
            { 
                testCaseName = ((TestCase)attribute).Name; 
                testCaseID = testCaseName.Substring(0, testCaseName.IndexOf(":")); 
                xmlArguments = new string[] { type.Name, testCaseID + "_local@hotmail.com", testCaseID + "_remote@hotmail.com" }; 
                // Write xml args to file 
            } 
        } 
    }
    Code formatting (HTML) provided by Wes' Code HTMLer. Thanks Wes!