public void GetAttributeFromMemberReutrnsNullIfAttributeIsNotPresentOnMember() { Expression <Func <Person, int> > selector = p => p.Age; PropertyInfo property = selector.GetPropertyInfo(); TestClassAttribute attribute = property.GetAttribute <TestClassAttribute>(); Assert.IsNull(attribute); }
public void GetAttributeFromTypeReturnsAttribute() { Type type = typeof(Person); TestClassAttribute attribute = type.GetAttribute <TestClassAttribute>(); Assert.IsNotNull(attribute); Assert.AreEqual(1, attribute.Id); }
static private TestClassAttribute GetTestGroupAttribute(Type type) { TestClassAttribute testGroupAttr = null; foreach (Attribute attr in type.GetCustomAttributes(true)) { testGroupAttr = attr as TestClassAttribute; if (null != testGroupAttr) { return(testGroupAttr); } } return(null); }
public List <TestTypeInfo> GetTypeInfos(Assembly assembly) { List <TestTypeInfo> result = new List <TestTypeInfo>(); Type[] types = assembly.GetExportedTypes(); foreach (Type t in types) { TestClassAttribute attribute = t.GetCustomAttribute(typeof(TestClassAttribute)) as TestClassAttribute; if (attribute != null) { result.Add(TestTypeInfo(t, attribute.DocumentPath)); } } return(result); }
private void CollectTestMethods(Assembly assembly, ITestResultEndPoint results, out IList <TestMethodInfo> sequentialTestMethods, out IList <TestMethodInfo> parallelTestMethods) { _log.Debug(nameof(CollectTestMethods)); sequentialTestMethods = new List <TestMethodInfo>(); parallelTestMethods = new List <TestMethodInfo>(); foreach (Type type in assembly.GetTypes()) { _log.Debug($"Searching type {type.Format()}."); TestClassAttribute c_attr = type.GetCustomAttribute <TestClassAttribute>(); TestMode classLevelMode = c_attr != null ? c_attr.TestMode : TestMode.Parallel; Boolean classLevelIgnore = c_attr != null && c_attr.IsIgnored; foreach (MethodInfo testMethod in type.GetRuntimeMethods().Where(m => m.GetCustomAttributes <TestMethodAttribute>().Count() > 0)) { _log.Info($"Found test method {type.Format()}.{testMethod.Name.Format()}."); TestMethodAttribute m_attr = testMethod.GetCustomAttribute <TestMethodAttribute>(); if (classLevelIgnore) { _log.Debug($"Class ignored: {c_attr.IgnoreReason.Format()}"); results.IgnoreTestMethod(testMethod, $"Class ignored: {c_attr.IgnoreReason.Format()}"); } else if (m_attr.IsIgnored) { _log.Debug($"Method ignored: {m_attr.IgnoreReason.Format()}"); results.IgnoreTestMethod(testMethod, $"Method ignored: {m_attr.IgnoreReason.Format()}"); } else { if (classLevelMode == TestMode.Sequential || m_attr.TestMode == TestMode.Sequential || Configuration.TestMethodModeOverride == TestModeOverrides.Sequential) { _log.Info($"Adding sequential test method {type.Format()}.{testMethod.Name.Format()}."); sequentialTestMethods.Add(new TestMethodInfo(results, testMethod)); } else { _log.Info($"Adding parallel test method {type.Format()}.{testMethod.Name.Format()}."); parallelTestMethods.Add(new TestMethodInfo(results, testMethod)); } } } } }
/// <summary> /// Initializes a new instance of the <see cref="TestClassInfo"/> class. /// </summary> /// <param name="type">Underlying test class type.</param> /// <param name="constructor">Constructor for the test class.</param> /// <param name="testContextProperty">Reference to the <see cref="TestContext"/> property in test class.</param> /// <param name="classAttribute">Test class attribute.</param> /// <param name="parent">Parent assembly info.</param> internal TestClassInfo( Type type, ConstructorInfo constructor, PropertyInfo testContextProperty, TestClassAttribute classAttribute, TestAssemblyInfo parent) { Debug.Assert(type != null, "Type should not be null"); Debug.Assert(constructor != null, "Constructor should not be null"); Debug.Assert(parent != null, "Parent should not be null"); Debug.Assert(classAttribute != null, "ClassAtribute should not be null"); this.ClassType = type; this.Constructor = constructor; this.TestContextProperty = testContextProperty; this.BaseTestInitializeMethodsQueue = new Queue <MethodInfo>(); this.BaseTestCleanupMethodsQueue = new Queue <MethodInfo>(); this.Parent = parent; this.ClassAttribute = classAttribute; }
private TestClassUnit(Type type) { m_testGroupClass = type; m_attr = GetTestGroupAttribute(type); m_ignoreAttr = GetTestIgnoreAttribute(type); m_testCaseUnits = TestMethodUnit.GetTestCaseUnits(type); name = type.FullName; foreach (MethodInfo methodInfo in type.GetMethods()) { foreach (Attribute attr in methodInfo.GetCustomAttributes(true)) { if ((attr as AssemblyInitializeAttribute) != null) { AssemblyInitMethod = methodInfo; } if ((attr as AssemblyCleanupAttribute) != null) { AssemblyCleanupMethod = methodInfo; } if ((attr as ClassInitializeAttribute) != null) { ClassInitMethod = methodInfo; } if ((attr as ClassCleanupAttribute) != null) { ClassCleanupMethod = methodInfo; } if ((attr as TestInitializeAttribute) != null) { TestInitMethod = methodInfo; } if ((attr as TestCleanupAttribute) != null) { TestCleanupMethod = methodInfo; } } } // default is all enabled enable = true; if (m_testCaseUnits.Length == 0) { enable = false; } //if the Ignore attribute is specified, disable the test group if (m_ignoreAttr != null) { this.Enable = false; } }