Interface ITestFixture
INTERNAL USE ONLY: Defines the behavior of a Neon Xunit test fixture.
note
All test fixture implementations must inherit from TestFixture to work properly. Do not attempt to create a fixture from scratch that implements this interface.
Namespace: Neon.Xunit
Assembly: Neon.Xunit.dll
Syntax
public interface ITestFixture
Remarks
Xunit test fixtures are designed to provide initialize global state that tests can then reference during their execution. Typical scenarios include:
- Starting a database server and prepopulating it with a schema and data to test database access code.
- Starting a Docker service such that REST endpoints can be tested.
- Initializing a cluster and then configuring it with certificates, routes, services etc. and then performing tests against the actual swarm.
Test fixture lifecycle:
-
First, you'll need create your Xunit test class and have it derive
from Xunit.IClassFixture<TFixture>, where
TFixture
identifies the fixture. -
The Xunit test runner reflects the test assemblies and identifies the
test classes with
[Fact]
test methods to be executed. - For each test class to be executed, the test runner first creates an instance of the test fixture. This is created before one before any of the test classes are instantiated and any test methods are called.
-
The test runner creates a new instance of the test class for each test method to be invoked. The test class constructor must accept a single parameter with type
TFixture
. The test class constructor should call Start(Action) to initialize the fixture, passing an optional Action that does any custom initialization for the test.The Action parameter is generally intended for internal use when implementing custom test fixtures.
Test fixtures are designed to be aware of whether they've been initialized or not such that only the first call to Start(Action) will perform any necessary initialization (including calling the custom action) and any subsequent calls will do nothing.
note
Some test fixtures may define a different different initialization method.
- The test runner will continue instantiating test class instances and calling test methods using the test fixture state setup during the first test.
- Once all of the test methods have been called, the test runner will call the test fixtures Dispose() method so that it can clean up any state.
Properties
IsRunning
Returns true
if the fixture has been started.
Declaration
bool IsRunning { get; }
Property Value
Type | Description |
---|---|
bool |
State
Used by unit test classes to persist arbitrary name/value information across individual unit tests.
Declaration
IDictionary<string, object> State { get; }
Property Value
Type | Description |
---|---|
IDictionary<string, object> |
Methods
OnRestart()
Called when an already started fixture is being restarted. This provides the fixture an opportunity to do some custom initialization.
note
This method is intended only for use by test fixture implementations. Unit tests or test fixtures should never call this directly.
Declaration
void OnRestart()
Reset()
INTERNAL USE ONLY: Resets the fixture state.
Declaration
void Reset()
Start(Action)
Starts the fixture if it hasn't already been started including invoking the optional Action the first time Start(Action) is called for a fixture instance.
Declaration
TestFixtureStatus Start(Action action = null)
Parameters
Type | Name | Description |
---|---|---|
Action | action | The optional custom start action. noteThis is generally intended for use when developing custom test fixtures. |
Returns
Type | Description |
---|---|
TestFixtureStatus | Started if the fixture wasn't previously started and this method call started it or AlreadyRunning if the fixture was already running. |