/// <summary> /// Initializes a new instance of the <see cref="UnitTestLevelAttribute"/> class. /// </summary> /// <param name="unitTestLevel"></param> public UnitTestLevelAttribute(UnitTestLevel unitTestLevel) { this.UnitTestLevel = unitTestLevel; }
/// <summary> /// Validates the derived class adheres to a common set of conventions. /// </summary> /// <remarks> /// This method discovers and executes all the test methods in this current <see cref="UnitTest"/> /// base class. To add more validation, simply add more test methods to this base class. /// Please mark the test methods private so that they cannot be inherited by the derived tests. /// </remarks> protected void ValidateUnitTestClass() { UnitTestLevel classLevel = UnitTestLevelOfMember(this.GetType()); MethodInfo[] baseMethods = typeof(UnitTest).GetMethods(publicOrPrivateDeclaredInstanceBindingFlags) .Where((m) => m.GetCustomAttributes(typeof(TestMethodAttribute), false).Any()) .Where((m) => UnitTestLevelOfMember(m) <= classLevel) .ToArray(); StringBuilder sb = new StringBuilder(); foreach (MethodInfo methodInfo in baseMethods) { try { methodInfo.Invoke(this, new object[0]); } catch (TargetInvocationException targetInvocationException) { // Strip off portion MSTest adds if we know this is one of our messages. string message = targetInvocationException.InnerException.Message; int indexOfPrefix = message.IndexOf(testErrorPrefix); if (indexOfPrefix >= 0) { message = message.Substring(indexOfPrefix); } // First error gets newline to put all summary errors below MSTest "Assert.Fail" message if (sb.Length == 0) { sb.AppendLine(); } sb.AppendLine(message); } } if (sb.Length != 0) { Assert.Fail(sb.ToString()); } else { string untestedMembers; switch (this.UnitTestLevel) { case UnitTestLevel.None: System.Diagnostics.Debug.WriteLine( string.Format( "'{0}' is not yet enabled for unit test verification.\r\nAdd [UnitTestLevel(UnitTestLevel.NotReady)] to the class to enable basic verification.", this.GetType().Name)); break; case UnitTestLevel.NotReady: untestedMembers = this.GetSummaryOfUntestedMembers(); if (!string.IsNullOrWhiteSpace(untestedMembers)) { System.Diagnostics.Debug.WriteLine(untestedMembers); } Assert.Inconclusive(string.Format("'{0}' passed basic verification but is marked 'Not Ready'.\r\nAdd [UnitTestLevel(UnitTestLevel.InProgress)] or [UnitTestLevel(UnitTestLevel.Complete)] to the class when it is ready for check in.", this.GetType().Name)); break; case UnitTestLevel.InProgress: System.Diagnostics.Debug.WriteLine(string.Format("'{0}'passed basic verification but is marked 'In Progress'.\r\nAdd [UnitTestLevel(UnitTestLevel.Complete)] to the class when it is complete.", this.GetType().Name)); untestedMembers = this.GetSummaryOfUntestedMembers(); if (!string.IsNullOrWhiteSpace(untestedMembers)) { System.Diagnostics.Debug.WriteLine(untestedMembers); } break; case UnitTestLevel.Complete: break; } } }