DatabaseTestSetManager 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package DatabaseTestSetManager --version 1.0.3
                    
NuGet\Install-Package DatabaseTestSetManager -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DatabaseTestSetManager" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DatabaseTestSetManager" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="DatabaseTestSetManager" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DatabaseTestSetManager --version 1.0.3
                    
#r "nuget: DatabaseTestSetManager, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DatabaseTestSetManager@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DatabaseTestSetManager&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=DatabaseTestSetManager&version=1.0.3
                    
Install as a Cake Tool

DatabaseTestSetManager

Painlessly and quickly bring your test database into the correct state before each unit test.

Currently supports a combination of MSTest, SQL Server and Entity Framework Core.

How do I use it?

  1. Add the DatabaseTestSetManager nuget package to your unittest project.
  2. Add one or more .sql scripts as embedded resources to your unittest project. These should remove any existing data from your test database and insert the data to be used for your unittests.
  3. Create a unittest class and have it derive from DatabaseTestBase<TDbContext>, where TDbContext is your EF Core DbContext:
public class ProductRepositoryTest: DatabaseTestBase<AcmeDbContext>
{
	public override string ConnectionString => "Server=(LocalDB)\\MSSQLLocalDB;Initial Catalog=AcmeUnitTestDB;Integrated security=True;TrustServerCertificate=True";

	public override void OnDefineTestSets()
	{
		TestSetManager.DefineTestSet("Default", setup =>
			 setup.FromAllEmbeddedSqlScripts(inAssemblyThatDefines: this));
	}

	// Test methods go here...
}

That's all there is to it to have your unittest database be initialized with the same data before each unittest.

That is, if you define a unittest like this:

	[TestMethod]
	public async Task GetProducts_ReturnsData()
	{
		ProductRepository productRepository = new ProductRepository(this.DbContext);

		//Act
		List<Entities.Product> products = await productRepository.GetProducts();

		//Assert
		Assert.IsTrue(products.Any());
	}

The DatabaseTestSetManager ensures that the sql scripts are run against your unittest database before the first unittest.

After that, by default, every unittest is run inside a SQL Server transaction that is rolled back at the end. This ensures that no changes done by your code under test are actually persisted to the database, so that the database is back in its initial state, ready for the next unittest.

You can control this behaviour by adding a DatabaseTestSet attribute to your unittest method (or class or assembly), specifying the name of the TestSet to use and how any changes should be cleaned up. The default values used are:

[DatabaseTestSet("Default", CleanUpChanges = DatabaseCleanUpChanges.ByRollback)]
public async Task GetProducts_ReturnsData()
{
		...

For DatabaseCleanUpChanges, three values are supported:

  1. ByRollback - The unittest is wrapped inside a SQL transaction, and at the end this transaction is rolled back so that any changes are reverted as well. Is very fast and usually the best choice, unless the tested code manages its own SQL transactions - then use ByReinitialize instead.
  2. ByReinitialize - Executes the SQL scripts again to reinitialize the database. Usually slower than ByRollback but doesn't interfere with any SQL transactions from the code under test.
  3. None - The database is left as-is; use this only for tests that don't modify the database.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.4 114 2/19/2025
1.1.3 109 2/19/2025
1.1.2 111 2/19/2025
1.1.0 436 2/4/2025
1.0.4 136 1/23/2025
1.0.3 109 1/20/2025
1.0.2 92 1/20/2025
1.0.0 120 1/17/2025