Class TestRunner<TContext, TTest>
A base class that provides default behavior when running a test. This includes support for skipping tests.
public abstract class TestRunner<TContext, TTest> : TestRunnerBase<TContext, TTest> where TContext : TestRunnerContext<TTest> where TTest : class, ITest
Type Parameters
TContextThe context type used by the runner
TTestThe test type used by the test framework. Must derive from ITest.
- Inheritance
-
TestRunnerBase<TContext, TTest>TestRunner<TContext, TTest>
- Derived
- Inherited Members
Remarks
This class does not make any assumptions about what it means to run an individual test, just that at some point, the test will be run. The intention with this base class is that it can serve as a base for non-traditional tests.
Constructors
TestRunner()
Initializes a new instance of the TestRunner<TContext, TTest> class.
protected TestRunner()
Methods
CreateTestClassInstance(TContext)
Override to creates and initialize the instance of the test class.
protected abstract ValueTask<(object? Instance, SynchronizationContext? SyncContext, ExecutionContext? ExecutionContext)> CreateTestClassInstance(TContext ctxt)
Parameters
ctxtTContextThe context that describes the current test
Returns
- ValueTask<(object Instance, SynchronizationContext SyncContext, ExecutionContext ExecutionContext)>
Returns the test class instance, the sync context that is current after the creation, and a capture of the execution context so that it can be restored later.
Remarks
This method runs during Running and any exceptions thrown will contribute to test failure. Since the method is potentially async, we depend on it to capture and return the sync context so that it may be propagated appropriately.
DisposeTestClassInstance(TContext, object)
Disposes the test class instance. By default, will call DisposeAsync() if it's implemented, falling back to Dispose() if not.
protected virtual ValueTask DisposeTestClassInstance(TContext ctxt, object testClassInstance)
Parameters
ctxtTContextThe context that describes the current test
testClassInstanceobjectThe test class instance
Returns
Remarks
This method runs during CleaningUp and any exceptions thrown will contribute to test cleanup failure.
InvokeTest(TContext, object?)
Invokes the test method and returns the amount of time spent executing.
protected virtual ValueTask<TimeSpan> InvokeTest(TContext ctxt, object? testClassInstance)
Parameters
ctxtTContextThe context that describes the current test
testClassInstanceobjectThe instance of the test class (may be
nullwhen running a static test method)
Returns
Remarks
This method runs during Running and any exceptions thrown will contribute to test failure.
IsTestClassCreatable(TContext)
Override to determine whether a test class should be created.
protected abstract bool IsTestClassCreatable(TContext ctxt)
Parameters
ctxtTContextThe context that describes the current test
Returns
Remarks
This method runs during Running and any exceptions thrown will contribute to test failure (and test class creation will not take place).
IsTestClassDisposable(TContext, object)
Determine whether a test class instance should be disposed. The pipeline will only call
DisposeTestClassInstance(TContext, object) if this returns true. By default, looks to
see if the class implements IAsyncDisposable or IDisposable.
protected virtual bool IsTestClassDisposable(TContext ctxt, object testClassInstance)
Parameters
ctxtTContextThe context that describes the current test
testClassInstanceobjectThe test class instance
Returns
Remarks
This method runs during Running and any exceptions thrown will contribute to test failure.
OnTestClassConstructionFinished(TContext)
This method will be called when a test class instance has finished being constructed. By default, this sends TestClassConstructionFinished.
protected virtual ValueTask<bool> OnTestClassConstructionFinished(TContext ctxt)
Parameters
ctxtTContextThe invoker context
Returns
Remarks
This method runs during Running and any exceptions thrown will contribute to test failure.
OnTestClassConstructionStarting(TContext)
This method will be called when a test class instance is about to be constructed. By default, this sends TestClassConstructionStarting.
protected virtual ValueTask<bool> OnTestClassConstructionStarting(TContext ctxt)
Parameters
ctxtTContextThe invoker context
Returns
Remarks
This method runs during Running and any exceptions thrown will contribute to test failure (and test class creation will not take place).
OnTestClassDisposeFinished(TContext)
This method will be called when a test class instance has finished being disposed. By default, this sends TestClassDisposeFinished.
protected virtual ValueTask<bool> OnTestClassDisposeFinished(TContext ctxt)
Parameters
ctxtTContextThe invoker context
Returns
Remarks
This method runs during Running and any exceptions thrown will contribute to test failure.
OnTestClassDisposeStarting(TContext)
This method will be called when a test class instance is about to be disposed. By default, this sends TestClassDisposeStarting.
protected virtual ValueTask<bool> OnTestClassDisposeStarting(TContext ctxt)
Parameters
ctxtTContextThe invoker context
Returns
Remarks
This method runs during Running and any exceptions thrown will contribute to test failure.
PostInvoke(TContext)
Override this method to call code just after the test invocation has completed, but before the test class instance has been disposed.
protected virtual void PostInvoke(TContext ctxt)
Parameters
ctxtTContextThe context that describes the current test
PreInvoke(TContext)
Override this method to call code just after the test class instance has been created, but before the test has been invoked.
protected virtual void PreInvoke(TContext ctxt)
Parameters
ctxtTContextThe context that describes the current test
RunTest(TContext)
Override this method to run the test.
protected override ValueTask<TimeSpan> RunTest(TContext ctxt)
Parameters
ctxtTContextThe context that describes the current test