Class AmbientTestMethodResolver
- Namespace
- TinyBDD
- Assembly
- TinyBDD.dll
Provides an ambient (per-async-flow) implementation of ITestMethodResolver that stores the current test MethodInfo in an AsyncLocal<T>.
public sealed class AmbientTestMethodResolver : ITestMethodResolver
- Inheritance
-
AmbientTestMethodResolver
- Implements
- Inherited Members
Examples
// MSTest example
[TestInitialize]
public void Init()
{
Bdd.Register(AmbientTestMethodResolver.Instance);
var mi = typeof(MyTests).GetMethod(TestContext.TestName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
AmbientTestMethodResolver.Set(mi);
Ambient.Current.Value = Bdd.CreateContext(this, traits: new MsTestTraitBridge(TestContext));
}
[TestCleanup]
public void Cleanup()
{
Ambient.Current.Value = null;
AmbientTestMethodResolver.Set(null);
}
Remarks
TinyBDD needs to know the currently executing test method to read attributes like ScenarioAttribute and TagAttribute. Test adapters (xUnit, NUnit, MSTest) can set the active method at the start of each test and clear it afterward. This avoids brittle stack walking and works well with parallel execution.
Typical usage is:
- Register the resolver once via Register(ITestMethodResolver).
- Set the current method at test start using Set(MethodInfo?).
- Clear it at test end by calling Set(MethodInfo?) with null.
If no ambient method is set, TinyBDD may fall back to a stack-trace based resolver (if available).
Properties
Instance
Gets the singleton instance of AmbientTestMethodResolver.
public static AmbientTestMethodResolver Instance { get; }
Property Value
Remarks
Register this instance once (e.g., in a test base class or adapter attribute) with Register(ITestMethodResolver), then call Set(MethodInfo?) at the beginning of each test.
Methods
GetCurrentTestMethod()
Gets the current test MethodInfo previously set for the active async flow.
public MethodInfo? GetCurrentTestMethod()
Returns
- MethodInfo
The current MethodInfo if one has been set via Set(MethodInfo?), otherwise null.
Remarks
When this returns null, TinyBDD may attempt a slower, stack-trace based resolution if configured to do so.
Set(MethodInfo?)
Sets the current test MethodInfo for the active async flow.
public static void Set(MethodInfo? method)
Parameters
methodMethodInfoThe MethodInfo representing the test method that is about to run, or null to clear the ambient value at the end of the test.
Remarks
This method is typically invoked by a framework adapter (e.g., an attribute or base class) in a per-test setup/teardown hook. Because it uses AsyncLocal<T>, it is safe to use in parallel test execution scenarios.