public void Initialize() { // run following command to write test method metadata to console: // dotnet test --logger:"console;verbosity=normal" MethodInfo methodInfo = this.GetType().GetMethod(this.TestContext.TestName); DescriptionAttribute descriptionAttribute = methodInfo.GetCustomAttribute <DescriptionAttribute>(); Console.WriteLine($"Description : {descriptionAttribute.Description}"); TestCategoryAttribute testCategoryAttribute = methodInfo.GetCustomAttribute <TestCategoryAttribute>(); Console.WriteLine($"Category : {string.Join(',', testCategoryAttribute.TestCategories)}"); PriorityAttribute priorityAttribute = methodInfo.GetCustomAttribute <PriorityAttribute>(); Console.WriteLine($"Priority : {priorityAttribute.Priority}"); OwnerAttribute ownerAttribute = methodInfo.GetCustomAttribute <OwnerAttribute>(); Console.WriteLine($"Owner : {ownerAttribute.Owner}"); Stopwatch testMethodStopwatch = new Stopwatch(); testMethodStopwatch.Start(); this.TestContext.Properties["TestMethodStopwatch"] = testMethodStopwatch; }
/// <summary> /// Get test method CachedMode information /// </summary> public void GetTestCatgoryInformation() { // Initialize the test output folder path testingfolderPath = TestContext.TestDeploymentDir; testName = TestContext.TestName; Type classType = Type.GetType(TestContext.FullyQualifiedTestClassName); MethodBase method = classType.GetMethod(TestContext.TestName); object[] CustomAttributes = method.GetCustomAttributes(typeof(TestCategoryAttribute), true); if (CustomAttributes.Length > 0) { TestCategoryAttribute attr = (TestCategoryAttribute)CustomAttributes[0]; if (attr != null && attr.TestCategories.Count > 0) { if (attr.TestCategories[0] == "WOPI") { isWOPI = true; } else { isWOPI = false; } } } }
/// <summary> /// Get test method CachedMode information /// </summary> public void GetTestCatgoryInformation() { Type classType = Type.GetType(TestContext.FullyQualifiedTestClassName); MethodBase method = classType.GetMethod(TestContext.TestName); object[] customAttributes = method.GetCustomAttributes(typeof(TestCategoryAttribute), true); if (customAttributes.Length > 0) { TestCategoryAttribute attr = (TestCategoryAttribute)customAttributes[0]; if (attr != null && attr.TestCategories.Count > 0) { if (attr.TestCategories[0] == "CachedMode") { UpdateOutlookConfiguration(true); } else { UpdateOutlookConfiguration(false); } } } // Initialize the test output folder path TestingfolderPath = TestContext.TestDeploymentDir; TestName = TestContext.TestName; }
private void HasValidTestCategoryAttribute() { StringBuilder sb = new StringBuilder(); foreach (MethodInfo methodInfo in this.TestMethods) { TestCategoryAttribute testCategory = methodInfo.GetCustomAttributes(typeof(TestCategoryAttribute), false).Cast <TestCategoryAttribute>().FirstOrDefault(); if (testCategory == null) { sb.AppendLine(string.Format(" {0}: should have [TestCategory].", methodInfo.Name)); } else if (string.IsNullOrWhiteSpace(string.Join(" ", testCategory.TestCategories))) { sb.AppendLine(string.Format(" {0}: [TestCategory] cannot be empty.", methodInfo.Name)); } } if (sb.Length != 0) { Assert.Fail(string.Format("{0}these test methods have incorrect [TestCategory] attributes:\r\n{1}.", testErrorPrefix, sb.ToString())); } }
public static void Check() { StackTrace trace = new System.Diagnostics.StackTrace(); StackFrame[] frames = trace.GetFrames(); //find who is calling me System.Reflection.MethodBase method = frames[1].GetMethod(); DrsrTestAttribute.Site = EnvironmentConfig.TestSite; object[] attrs = method.GetCustomAttributes(false); if (attrs == null) { return; } string level = null; foreach (object o in attrs) { //for out test attribute, invoke "Do" method, for MSTEST attributes, do accordingly Type thisType = o.GetType(); switch (thisType.Name) { case "DescriptionAttribute": { EnvironmentConfig.TestSite.Log.Add(LogEntryKind.Comment, "Test description: " + typeof(DescriptionAttribute).GetProperty("Description").GetValue(o, null).ToString()); } break; case "PriorityAttribute": { EnvironmentConfig.TestSite.Log.Add(LogEntryKind.Comment, "Implementation priority of this scenario is: " + typeof(PriorityAttribute).GetProperty("Priority").GetValue(o, null).ToString()); } break; case "TestCategoryAttribute": { TestCategoryAttribute tca = o as TestCategoryAttribute; foreach (string s in tca.TestCategories) { if (s.StartsWith("Win")) { level = s; continue; } // Check if SDC, RODC exist when TestCategory contain SDC, RODC if (s.Equals("SDC")) { if (!EnvironmentConfig.MachineStore.ContainsKey(EnvironmentConfig.Machine.WritableDC2)) { EnvironmentConfig.TestSite.Assume.Fail("The test requires a Secondary writable DC in the environment. Please set the corresponding field in PTF config, or make sure the machine can be connected."); } continue; } if (s.Equals("RODC")) { if (!EnvironmentConfig.MachineStore.ContainsKey(EnvironmentConfig.Machine.RODC)) { EnvironmentConfig.TestSite.Assume.Fail("The test requires a Read-Only DC in the environment. Please set the corresponding field in PTF config, or make sure the machine can be connected."); } continue; } } } break; default: if (thisType.BaseType == typeof(DrsrTestAttribute)) { try { thisType.GetMethod("Do").Invoke(o, null); } catch (Exception e) { DrsrTestAttribute.Site.Assert.Fail(e.InnerException.Message); } } break; } } if (level == null) { throw new Exception("Test Case not set in any domain functional level category"); } FunctionLevelAttribute fl = null; switch (level) { case "Win2000": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WIN2000); break; case "Win2003": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WIN2003); break; case "Win2008": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WIN2008); break; case "Win2008R2": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WIN2008R2); break; case "Win2012": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WIN2012); break; case "Win2012R2": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WIN2012R2); break; case "WinThreshold": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WINTHRESHOLD); break; case "Winv1803": ADCommonServerAdapter adapter = new ADCommonServerAdapter(); adapter.Initialize(DrsrTestAttribute.Site); ServerVersion curVersion = adapter.PDCOSVersion; if (curVersion < ServerVersion.Winv1803) { DrsrTestAttribute.Site.Assert.Inconclusive("Test case expects PDCOSVersion {0} should be Equal or Greater than Winv1803", curVersion); } fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WINTHRESHOLD); break; default: throw new Exception("Unknown domain functional level category: " + level); } fl.Do(); }
private static bool ValidValueForTestCategory(TestCategoryAttribute attribute, IEnumerable <string> validCategoryNames) { return(attribute.TestCategories.Count(t => validCategoryNames.Contains(t)) == 1); }
private void GetTestMethodData(MemberInfo member, ref DataRow row) { this.TestMethodData(); object[] attributes = member.GetCustomAttributes(true); bool testmethod = false; object[] objArray = attributes; int num = 0; while (num < (int)objArray.Length) { if (!(objArray[num] is TestMethodAttribute)) { num++; } else { testmethod = true; break; } } if (testmethod) { row[this.rTestName] = member.Name; HashAlgorithm Provider = new SHA1CryptoServiceProvider(); byte[] hash = Provider.ComputeHash(Encoding.Unicode.GetBytes(string.Concat(member.DeclaringType.FullName, ".", member.Name))); byte[] to = new byte[16]; Array.Copy(hash, to, 16); string str = this.rTestGUID; Guid guid = new Guid(to); row[str] = guid.ToString(); string p = ""; string o = ""; string c = ""; object[] objArray1 = attributes; for (int i = 0; i < (int)objArray1.Length; i++) { object att = objArray1[i]; PriorityAttribute pri = att as PriorityAttribute; OwnerAttribute own = att as OwnerAttribute; TestCategoryAttribute cate = att as TestCategoryAttribute; if (pri != null) { p = pri.Priority.ToString(); } if (own != null) { o = own.Owner; } if (cate != null) { foreach (string ca in cate.TestCategories) { c = string.Concat(c, ca, ";"); } } } row[this.rTestPriority] = p; row[this.rTestOwner] = o; if (c.Length > 0) { row[this.rTestCategory] = c.Remove(c.Length - 1); } } }