public void WhenActionCompletesNormally_RunReturnsPassedAndLogsNothing() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Passed, sandbox.Run(writer, delegate { }, null)); Assert.AreEqual("", writer.ToString()); }
/// <summary> /// Creates a wrapper for a <see cref="ITestContext" />. /// </summary> /// <param name="inner">The context to wrap.</param> /// <param name="sandbox">The sandbox to use, or null if none.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="inner"/> is null.</exception> private TestContext(ITestContext inner, Sandbox sandbox) { if (inner == null) throw new ArgumentNullException("inner"); this.inner = inner; this.sandbox = sandbox; }
public void WhenActionThrowsANonTestException_RunReturnsFailedAndLogsTheException() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Failed, sandbox.Run(writer, delegate { throw new InvalidOperationException("Foo"); }, null)); Assert.Contains(writer.ToString(), "InvalidOperationException"); }
public void WhenActionThrowsTestExceptionWithExcludedStackTraceAndDefaultMessage_RunReturnsOutputAndLogsNothing() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Canceled, sandbox.Run(writer, delegate { throw new SilentTestException(TestOutcome.Canceled); }, null)); Assert.AreEqual("", writer.ToString()); }
public void WhenActionThrowsTestExceptionWithExcludedStackTraceAndNonDefaultMessage_RunReturnsOutputAndLogsTheMessageButNotTheException() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Error, sandbox.Run(writer, delegate { throw new SilentTestException(TestOutcome.Error, "Message."); }, null)); Assert.DoesNotContain(writer.ToString(), "SilentTestException"); Assert.Contains(writer.ToString(), "Message."); }
/// <summary> /// Disposes the sandbox. /// </summary> /// <remarks> /// <para> /// All currently executing actions are aborted with <see cref="TestOutcome.Error" /> /// if <see cref="Abort(TestOutcome, string)" /> has not already been called. /// </para> /// </remarks> public void Dispose() { Abort(TestOutcome.Error, "The sandbox was disposed.", false); Sandbox cachedParent; lock (syncRoot) { if (isDisposed) { return; } cachedParent = parent; parent = null; isDisposed = true; } if (cachedParent != null) { cachedParent.Aborted -= HandleParentAborted; } }
public void RunCanBeAbortedInProgress() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); ManualResetEvent ready = new ManualResetEvent(false); bool completed = false; Sandbox sandbox = new Sandbox(); Tasks.StartThreadTask("Background abort.", () => { ready.WaitOne(); sandbox.Abort(TestOutcome.Canceled, "Test was canceled."); }); TestOutcome outcome = sandbox.Run(writer, () => { ready.Set(); Thread.Sleep(10000); completed = true; }, "Run Description"); Assert.IsFalse(completed, "The action should have been aborted prior to completion."); Assert.AreEqual(TestOutcome.Canceled, outcome); Assert.AreEqual(TestOutcome.Canceled, sandbox.AbortOutcome); Assert.AreEqual("Test was canceled.", sandbox.AbortMessage); Assert.IsTrue(sandbox.WasAborted); Assert.Contains(writer.ToString(), "Run Description"); Assert.Contains(writer.ToString(), "Test was canceled."); }
public RunTestAction(PatternTestExecutor executor, ITestCommand testCommand, Model.Tree.TestStep parentTestStep, Sandbox parentSandbox, PatternTestActionsDecorator testActionsDecorator) { this.executor = executor; this.testCommand = testCommand; this.parentTestStep = parentTestStep; this.parentSandbox = parentSandbox; this.testActionsDecorator = testActionsDecorator; result = new TestResult(TestOutcome.Error); }
/// <summary> /// Disposes the sandbox. /// </summary> /// <remarks> /// <para> /// All currently executing actions are aborted with <see cref="TestOutcome.Error" /> /// if <see cref="Abort(TestOutcome, string)" /> has not already been called. /// </para> /// </remarks> public void Dispose() { Abort(TestOutcome.Error, "The sandbox was disposed.", false); Sandbox cachedParent; lock (syncRoot) { if (isDisposed) return; cachedParent = parent; parent = null; isDisposed = true; } if (cachedParent != null) cachedParent.Aborted -= HandleParentAborted; }
public void RunThrowsIfTestLogWriterIsNull() { Sandbox sandbox = new Sandbox(); Assert.Throws<ArgumentNullException>(() => sandbox.Run(null, delegate { }, "description")); }
public void RunThrowsIfActionIsNull() { Sandbox sandbox = new Sandbox(); Assert.Throws<ArgumentNullException>(() => sandbox.Run(MockRepository.GenerateStub<MarkupDocumentWriter>(), null, "description")); }
public void WhenSandboxEntersProtectedContext_AbortsAreDeferred() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); bool completed = false; Sandbox sandbox = new Sandbox(); ManualResetEvent barrier = new ManualResetEvent(false); Tasks.StartThreadTask("Wake", () => { barrier.WaitOne(); sandbox.Abort(TestOutcome.Canceled, "Canceled for testing purposes."); }); TestOutcome outcome = sandbox.Run(writer, () => { using (sandbox.Protect()) { barrier.Set(); Thread.Sleep(300); completed = true; } Thread.Sleep(300); }, "Run Description"); Assert.IsTrue(completed); Assert.AreEqual(TestOutcome.Canceled, outcome); Assert.Contains(writer.ToString(), "Canceled for testing purposes."); }
public WhenAborted(bool abortTwice) { abortedSandbox = new Sandbox(); abortedSandbox.Abort(TestOutcome.Canceled, "Abort message."); if (abortTwice) abortedSandbox.Abort(TestOutcome.Passed, "A different message."); }
/// <summary> /// Creates a child sandbox. /// </summary> /// <remarks> /// <para> /// When the parent sandbox is aborted, the child will likewise be aborted. This policy /// offers a mechanism to scope actions recursively. /// </para> /// </remarks> /// <returns>The child sandbox.</returns> /// <exception cref="ObjectDisposedException">Thrown if the sandbox was disposed.</exception> public Sandbox CreateChild() { ThrowIfDisposed(); Sandbox child = new Sandbox(); child.parent = this; Aborted += child.HandleParentAborted; return child; }
public RunTestAction CreateActionToRunTest(ITestCommand testCommand, Model.Tree.TestStep parentTestStep, Sandbox parentSandbox, PatternTestActionsDecorator testActionsDecorator) { return new RunTestAction(this, testCommand, parentTestStep, parentSandbox, testActionsDecorator); }
public void CanCatchThreadAbortException() { StructuredDocumentWriter writer = new StructuredDocumentWriter() ; Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Failed, sandbox.Run(writer, delegate { Thread.CurrentThread.Abort(this); }, "Execute")); Assert.Contains(writer.ToString(), "ThreadAbortException"); Assert.Contains(writer.ToString(), "Execute"); }
public void WhenActionFailsAndADescriptionWasProvided_TheDescriptionAppearsInTheLog() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Failed, sandbox.Run(writer, delegate { throw new InvalidOperationException("Foo"); }, "SetUp")); Assert.Contains(writer.ToString(), "InvalidOperationException"); Assert.Contains(writer.ToString(), "SetUp"); }
/// <summary> /// Prepares a <see cref="TestContext" /> wrapper for the given inner context. /// </summary> /// <remarks> /// <para> /// The wrapper is cached for the duration of the lifetime of the inner context. /// </para> /// </remarks> /// <param name="inner">The new inner context.</param> /// <param name="sandbox">The sandbox to use, or null if none.</param> /// <returns>The wrapper context.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="inner"/> is null.</exception> internal static TestContext PrepareContext(ITestContext inner, Sandbox sandbox) { TestContext context = new TestContext(inner, sandbox); inner.Data.SetValue(GallioFrameworkContextKey, context); inner.Finishing += context.NotifyFinishing; return context; }
public void WhenActionThrowsTestExceptionWithIncludedStackTraceAndNonDefaultMessage_RunReturnsOutputAndLogsTheException() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Failed, sandbox.Run(writer, delegate { throw new AssertionException("Reason."); }, null)); Assert.Contains(writer.ToString(), "AssertionException"); Assert.Contains(writer.ToString(), "Reason."); }
private static void RunWithTimeout(TimeSpan waitTime, TimeSpan? timeout) { StructuredDocumentWriter writer = new StructuredDocumentWriter(); bool completed = false; Sandbox sandbox = new Sandbox(); TestOutcome outcome = TestOutcome.Error; using (sandbox.StartTimer(timeout)) { outcome = sandbox.Run(writer, () => { Thread.Sleep(waitTime); completed = true; }, "Run Description"); } if (timeout.HasValue && timeout.Value < waitTime) { Assert.IsFalse(completed, "The action should have been aborted prior to completion."); Assert.AreEqual(TestOutcome.Timeout, outcome); Assert.AreEqual(TestOutcome.Timeout, sandbox.AbortOutcome); Assert.AreEqual(String.Format("The test timed out after {0} seconds.", timeout.Value.TotalSeconds), sandbox.AbortMessage); Assert.IsTrue(sandbox.WasAborted); Assert.Contains(writer.ToString(), "Run Description"); Assert.Contains(writer.ToString(), String.Format("The test timed out after {0} seconds.", timeout.Value.TotalSeconds)); } else { Assert.IsTrue(completed, "The action should have completed because no timeout occurred."); Assert.AreEqual(TestOutcome.Passed, outcome); Assert.IsFalse(sandbox.WasAborted); } }