Startup
A project may declare one IPreceptStartup class. It registers services and runs setup for the
whole run:
public sealed class Startup : IPreceptStartup
{
public void ConfigureServices(IServiceCollection services, PreceptSettings settings)
{
services.AddPreceptApi();
services.AddPreceptWeb();
services.AddPreceptData(cs => new NpgsqlConnection(cs));
}
public Task BeforeRunAsync(IServiceProvider services) => SeedReferenceDataAsync();
}
ConfigureServices is required. Write it with an empty body if the suite registers nothing; an
explicit declaration is what stops a mistyped signature from compiling. BeforeRunAsync and
AfterRunAsync are optional.
When each method runs#
BeforeRunAsync runs once, just before the first test. AfterRunAsync runs once, after the last
test has finished. Neither runs when the platform only lists tests. dotnet run -- --list-tests,
and a test explorer refreshing its tree after a build, open a session and discover the suite without
running anything, and a hook that seeds a database or signs a browser in should not run for a tree
refresh.
ConfigureServices does run on discovery, because the container it builds is what discovery reads
settings from. Keep it to registrations, and put work that touches an environment in
BeforeRunAsync.
If BeforeRunAsync throws, no test runs, not even a class's own [BeforeSuite]. Every test the run
selected is reported as failed with the hook's exception, so the cause reaches the console, the TRX,
a test explorer and every reporter, rather than only a log. AfterRunAsync still runs, because a
setup that failed halfway is the one most in need of its teardown.
Module settings#
Each AddPrecept… call binds that module's own section of precept.json. It also takes a callback
for anything the file cannot say, such as a base address the startup only learns at run time, or a
secret read from a vault:
services.AddPreceptApi(api => api.BaseUrl = stub.BaseUrl);
services.AddPreceptWeb(web => web.Headless = false);
The callback runs when the settings are first resolved, not inside ConfigureServices, so it may
close over something this method has only just created.
The settings parameter carries the run's own settings: parallelism, retries, the environment, the
filter. It does not carry a module's. Read one of
those with settings.GetSection<T>() if a decision here depends on it.
Anything registered here can be resolved from PreceptTestContext.Current.Services.
The run's container is disposed once AfterRunAsync has returned. A singleton that implements
IDisposable or IAsyncDisposable, such as a container host, test data created for the whole run,
or a client holding connections, gets its teardown without the project arranging it.