Class AssemblyFixture
- Namespace
- TinyBDD
- Assembly
- TinyBDD.dll
Base class for assembly-wide setup and teardown that executes once per test assembly.
public abstract class AssemblyFixture
- Inheritance
-
AssemblyFixture
- Inherited Members
Examples
Example: Database fixture for an assembly
using TinyBDD;
[assembly: AssemblySetup(typeof(DatabaseFixture))]
public class DatabaseFixture : AssemblyFixture
{
private TestDatabase? _db;
public TestDatabase Database => _db ?? throw new InvalidOperationException("Database not initialized");
protected override async Task SetupAsync(CancellationToken ct)
{
_db = new TestDatabase();
await _db.StartAsync(ct);
await _db.SeedTestDataAsync(ct);
}
protected override async Task TeardownAsync(CancellationToken ct)
{
if (_db is not null)
await _db.DisposeAsync();
}
}
// In your tests:
public class MyTests : TinyBddXunitBase
{
[Fact]
public async Task CanQueryDatabase()
{
var db = AssemblyFixture.Get<DatabaseFixture>().Database;
// Use the database...
}
}
Remarks
Derive from this class to create fixtures that need to run expensive setup operations once for the entire test assembly (e.g., starting a test database, initializing a DI container, spawning external services, or loading test data).
The lifecycle is:
- SetupAsync(CancellationToken) is called once before any tests run in the assembly.
- All test features and scenarios execute.
- TeardownAsync(CancellationToken) is called once after all tests complete.
Assembly fixtures are framework-agnostic and work with xUnit, NUnit, and MSTest. They are discovered via the AssemblySetupAttribute attribute.
Properties
Reporter
Gets or sets the optional reporter used to emit Gherkin-style assembly setup/teardown logs.
public IBddReporter? Reporter { get; set; }
Property Value
Remarks
If set, the coordinator will write "Assembly Setup" and "Assembly Teardown" sections to the reporter during lifecycle events.
Methods
Get<T>()
Retrieves a registered assembly fixture by type.
public static T Get<T>() where T : AssemblyFixture
Returns
- T
The registered instance of the fixture.
Type Parameters
TThe type of assembly fixture to retrieve.
Remarks
This method provides convenient access to assembly fixtures from within tests. The fixture must be registered via AssemblySetupAttribute and initialized by the coordinator before it can be retrieved.
Exceptions
- InvalidOperationException
Thrown if the fixture has not been registered or initialized.
SetupAsync(CancellationToken)
Override to perform assembly-wide setup operations.
protected virtual Task SetupAsync(CancellationToken ct = default)
Parameters
ctCancellationTokenOptional cancellation token.
Returns
- Task
A task representing the asynchronous setup operation.
Remarks
This method is called once before any tests run in the assembly. Use this to initialize expensive resources that can be shared across all tests.
TeardownAsync(CancellationToken)
Override to perform assembly-wide teardown operations.
protected virtual Task TeardownAsync(CancellationToken ct = default)
Parameters
ctCancellationTokenOptional cancellation token.
Returns
- Task
A task representing the asynchronous teardown operation.
Remarks
This method is called once after all tests complete in the assembly. Use this to clean up expensive resources initialized in SetupAsync(CancellationToken).