• Stars
    star
    2,624
  • Rank 17,449 (Top 0.4 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Intelligent database cleaner for integration tests

Respawn

CI NuGet NuGet MyGet (dev)

Respawn is a small utility to help in resetting test databases to a clean state. Instead of deleting data at the end of a test or rolling back a transaction, Respawn resets the database back to a clean, empty state by intelligently deleting data from tables.

To use, create a Respawner and initialize with tables you want to skip, or schemas you want to keep/ignore:

var respawner = await Respawner.CreateAsync(connection, new RespawnerOptions
{
    TablesToIgnore = new Table[]
    {
        "sysdiagrams",
        "tblUser",
        "tblObjectType",
        new Table("MyOtherSchema", "MyOtherTable")
    },
    SchemasToExclude = new []
    {
        "RoundhousE"
    }
});

Or if you want to use a different database:

var respawner = await Respawner.CreateAsync(connection, new RespawnerOptions
{
    SchemasToInclude = new []
    {
        "public"
    },
    DbAdapter = DbAdapter.Postgres
});

In your tests, in the fixture setup, reset to a clean state:

await respawner.ResetAsync("MyConnectionStringName");

or if you're using a database besides SQL Server, pass an open DbConnection:

using (var conn = new NpgsqlConnection("ConnectionString"))
{
    await conn.OpenAsync();

    await respawner.ResetAsync(conn);
}

How does it work?

Respawn examines the SQL metadata intelligently to build a deterministic order of tables to delete based on foreign key relationships between tables. It navigates these relationships to build a DELETE script starting with the tables with no relationships and moving inwards until all tables are accounted for.

Once this in-order list of tables is created in the CreateAsync factory, the Respawner object keeps this list of tables privately so that the list of tables and the order is only calculated once.

In your tests, you Reset your database before each test run. If there are any tables/schemas that you don't want to be cleared out, include these in the configuration of your RespawnerOptions.

In benchmarks, a deterministic deletion of tables is faster than truncation, since truncation requires disabling or deleting foreign key constraints. Deletion results in easier test debugging/maintenance, as transaction rollbacks/post-test deletion still rely on that mechanism at the beginning of each test. If data comes in from another source, your test might fail. Respawning to a clean state assures you have a known starting point before each test.

Installing Respawn

You should install Respawn with NuGet:

Install-Package Respawn

Or via the .NET Core CLI:

dotnet add package Respawn

This command from Package Manager Console will download and install Respawn.

Local development

To install and run local dependencies needed for tests, (PostgreSQL and MySQL) install Docker for Windows and from the command line at the solution root run:

docker-compose up -d

This will pull down the latest container images and run them. You can then run the local build/tests.

More Repositories

1

MediatR

Simple, unambitious mediator implementation in .NET
C#
10,980
star
2

ContosoUniversityDotNetCore-Pages

With Razor Pages
C#
1,291
star
3

ContosoUniversityCore

JavaScript
589
star
4

presentations

Presentations I give
HTML
431
star
5

ContosoUniversityDotNetCore

JavaScript
424
star
6

ContosoUniversity

Contoso University sample re-done the way I would build it
JavaScript
394
star
7

MediatR.Extensions.Microsoft.DependencyInjection

MediatR extensions for Microsoft.Extensions.DependencyInjection
C#
327
star
8

bulk-writer

Provides guidance for fast ETL jobs, an IDataReader implementation for SqlBulkCopy (or the MySql or Oracle equivalents) that wraps an IEnumerable, and libraries for mapping entites to table columns.
C#
240
star
9

EntityFramework.Filters

Filters implementation for Entity Framework
C#
192
star
10

AdventureWorksCosmos

C#
96
star
11

nsb-diagnostics-poc

C#
70
star
12

SharpSSH

Fork of SharpSSH - SharpSSH is a pure .NET implementation of the SSH2 client protocol suite. It provides an API for communication with SSH servers and can be integrated into any .NET application.
C#
64
star
13

AzureAdExample

C#
50
star
14

MongoDB.Driver.Core.Extensions.DiagnosticSources

C#
42
star
15

NServiceBus.MessageRouting

Implementations of EIP message routing patterns
C#
41
star
16

QuartzNServiceBusSample

C#
39
star
17

MongoDB.Driver.Core.Extensions.OpenTelemetry

PowerShell
34
star
18

NServiceBus.Extensions.IntegrationTesting

C#
29
star
19

MicroservicesMessagingDemo

JavaScript
26
star
20

ddd-theory-and-practice-workshop-exercises

20
star
21

NServiceBus.Extensions.Diagnostics

C#
16
star
22

NServiceBus.Extensions.Diagnostics.OpenTelemetry

PowerShell
14
star
23

contosouniversitydocker

JavaScript
13
star
24

aspnetwebstack

C#
6
star
25

NServiceBusClassExample

C#
6
star
26

AutoMapper.EFCore

Port of https://github.com/AutoMapper/AutoMapper.EF6 for EF core.
C#
6
star
27

ndcsydney2017-cicd

JavaScript
5
star
28

contosouniversitydocker-linux

JavaScript
5
star
29

AzureMessagingServices

C#
5
star
30

blogexamples

Example code from posts on http://jimmybogard.lostechies.com
JavaScript
5
star
31

DotNetMigration

JavaScript
5
star
32

Oredev2016Workshop

C#
4
star
33

contosouniversity-rest

JavaScript
3
star
34

ContinuousDeliveryExample

JavaScript
3
star
35

rtd-tutorial

Python
2
star
36

NSBPerf

C#
2
star
37

AutoMapperSample

Sample Application for showing issue with AutoMapper
C#
1
star
38

NServiceBusProfiling

C#
1
star
39

sinatra-toto

Ruby
1
star
40

GenericVarianceContainerTests

C#
1
star
41

NServiceBusClassExamples

C#
1
star
42

kcdc2017-cicd

JavaScript
1
star