Skip to content

Instantly share code, notes, and snippets.

@jnm2
Created February 10, 2019 01:25
Show Gist options
  • Save jnm2/0dfcf7dabfb8db76865c124d3a59b20f to your computer and use it in GitHub Desktop.
Save jnm2/0dfcf7dabfb8db76865c124d3a59b20f to your computer and use it in GitHub Desktop.
using System;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public sealed class WaitForAmbientTasksAttribute : Attribute, ITestAction
{
public ActionTargets Targets => ActionTargets.Test;
public void BeforeTest(ITest test)
{
AmbientTasks.BeginContext();
}
public void AfterTest(ITest test)
{
switch (TestContext.CurrentContext.Result.Outcome.Status)
{
case TestStatus.Failed:
case TestStatus.Inconclusive:
case TestStatus.Skipped:
return;
}
var task = AmbientTasks.WaitAllAsync();
if (!task.IsCompleted) Assert.Fail("The test started ambient tasks but did not wait for them.");
task.GetAwaiter().GetResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment