Ines's profileInès' CribPhotosBlogListsMore ![]() | Help |
|
|
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
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:
I opted for option 2 Sample Output (where D00 represents an ID for a test case in a test case group): <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 } } } |
|
|