/// <summary> /// Wraps code containing a series of assertions, which should all /// be executed, even if they fail. Failed results are saved and /// reported at the end of the code block. /// </summary> /// <param name="testDelegate">A TestDelegate to be executed in Multiple Assertion mode.</param> public static void Multiple(TestDelegate testDelegate) { TestExecutionContext context = TestExecutionContext.CurrentContext; Guard.OperationValid(context != null, "Assert.Multiple called outside of a valid TestExecutionContext"); context.MultipleAssertLevel++; try { testDelegate(); } finally { context.MultipleAssertLevel--; } if (context.MultipleAssertLevel == 0) { int count = context.CurrentResult.AssertionResults.Count; if (count > 0) { var writer = new TextMessageWriter("Multiple Assert block had {0} failure(s).", count); int counter = 0; foreach (var assertion in context.CurrentResult.AssertionResults) { writer.WriteLine(string.Format(" {0}) {1}", ++counter, assertion.Message)); } throw new AssertionException(writer.ToString()); } } }
/// <summary> /// Verifies that a delegate does not throw an exception /// </summary> /// <param name="code">A TestSnippet delegate</param> /// <param name="message">The message that will be displayed on failure</param> /// <param name="args">Arguments to be used in formatting the message</param> public static void DoesNotThrow(TestDelegate code, string message, params object[] args) { try { code(); } catch (Exception ex) { TextMessageWriter writer = new TextMessageWriter(message, args); writer.WriteLine("Unexpected exception: {0}", ex.GetType()); Assert.Fail(writer.ToString()); } }