Skip to content

About

.NET Integration Testing library

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Octopus.IntegrationTesting.SqlServer

This library supports our Isolated Integration Testing in Octopus Server and other products.

It provides functionality for efficiently creating a pool of databases which enables multiple integration tests to run in parallel, providing a clean database to each test.

The package targets net10.0.

Overview

  • The main benefit of the library is that it manages a pool of test databases for you.
  • Each test can be given a clean database from the pool, and the "application" can then be instantiated on top of this clean database.
  • The test runs, and the application code uses the database in any way it pleases
  • When the test completes, the "application" is disposed and the database is returned to the pool, where it is reset.

Simple Example:

Assuming pool is an IsolatedEnvironmentPool<ServiceProvider> created once for the test run (see Reference):

var environment = await pool.Lease(cancellationToken);
try
{
    var orders = environment.Application.GetRequiredService<IOrderService>();
    await orders.PlaceOrder(...);
    // Assert against the application, or directly against environment.Database.ConnectionString
}
finally
{
    // Returns immediately. The reset happens in the background.
    pool.Return(environment);
}

Efficiently managing a pool of databases

In building the library, we experimented a lot with how best to do this; We tested the following strategies for the Octopus Server codebase. For reference, we have roughly 280 tables, with complex relationships between many of them. We use DbUp to manage database migrations in a fairly traditional way. We have accumulated roughly 1,000 database migrations over the years that Octopus Server has been around.

To create a clean database, we tested the following strategies, approximate results on a test machine were as follows:

Strategy Time taken
Run all migrations from scratch 30 seconds
Consolidated single SQL script which creates the entire database in one operation 10 seconds
Restoring a database backup 1 second
Wiping all tables and re-inserting a known clean-state 30 milliseconds

How the library works

You create an IsolatedEnvironmentPool<TApplication> representing a pool of clean databases.

  • You supply a callback (PooledDatabaseOptions.CreateCleanDatabase) which creates your system's database from scratch. In the Octopus Server case, this runs all the database migrations from zero, exactly replicating what our real production systems would do.
  • You also supply it with a SchemaStateIdentifier which is an opaque token that captures the state of your database schema. In the Octopus Server case, all of our database migrations are stored in a single Octopus.Upgraders.dll; We take a SHA256 hash of the file on disk, so any change to any database migration code or supporting logic will be considered a new state.

The library builds on top of this with a multi-stage approach with caching across test runs

  1. First, it runs the "from scratch" callback to create an initial clean database
  2. Then, it creates a SQL backup of the database
  3. It captures the data of all the tables in the clean database into a "reset script"; A single SQL file full of inserts that can be applied onto a completely empty database to put it into a clean state.
  4. It uses the database backup to create N copies of the database, depending on the parallelism level you specify
  5. A test leases an environment from the pool, where it is given a connection string for one of the databases in the pool.
  6. The test runs, using the pooled database, making whatever changes it wants
  7. The test returns the environment to the pool. The in-memory application state is Disposed, all tables are wiped, and the "reset script" re-populates the tables, restoring their initial clean data state.

There are hooks for customising the clean state:

  • If the initial clean database state contains something like an "Installation ID", it can be removed from the captured clean state and replaced by SQL which runs after every reset (AfterResetAdditionalSql), so that each test run gets a new value.
  • You can selectively exclude tables from the clean state capture, e.g. if you have an "audit event" table, you may want each test to always start with an empty table.

To improve the developer experience, these stages can be cached across runs. Caching is opt-in: by default every database in the pool is created from scratch and the reset script is regenerated on every run. See Speeding up later test runs.

  • With BackupStrategy = new DiskBackupStrategy(...) and ResetScriptStore = new FileResetScriptStore(...), database backups and the "reset script" are deliberately left on disk (you specify the location)
  • The next time a test run starts, if the schema state identifier matches the previous backups/reset script, they will be re-used, skipping the time taken to build them up.

Caveat: Background work must not be permitted to run after returning the database to the pool.

For example, Octopus Server contains a message bus which runs in the background. Tests using this pattern can use the message bus, but the bus must not be running by default. If a test opts to start it, it must stop and dispose of it as part of teardown, or the bus could make changes to a pooled database used by a future test.

In practice we have found this is not a huge limitation; Most of the things we need to test in Octopus Server involve simple state-manipulation that does not rely on background work.

Results

By giving each test an entire database and application state to itself, for tests converted to this pattern we found that almost all integration test flake was eliminated.

Test launch times for local developer workflows in Octopus Server reduced from roughly 12 seconds to 2 seconds (in the common case where a developer hasn't altered a database migration).

Over roughly a year, our CI builds replaced approximately 16 parallel machines worth of test-work, with two.

About

.NET Integration Testing library

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages