DatabaseTestSetManager 1.0.3
See the version list below for details.
dotnet add package DatabaseTestSetManager --version 1.0.3
NuGet\Install-Package DatabaseTestSetManager -Version 1.0.3
<PackageReference Include="DatabaseTestSetManager" Version="1.0.3" />
<PackageVersion Include="DatabaseTestSetManager" Version="1.0.3" />
<PackageReference Include="DatabaseTestSetManager" />
paket add DatabaseTestSetManager --version 1.0.3
#r "nuget: DatabaseTestSetManager, 1.0.3"
#:package DatabaseTestSetManager@1.0.3
#addin nuget:?package=DatabaseTestSetManager&version=1.0.3
#tool nuget:?package=DatabaseTestSetManager&version=1.0.3
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?
- Add the DatabaseTestSetManager nuget package to your unittest project.
- 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.
- Create a unittest class and have it derive from DatabaseTestBase<TDbContext>, where
TDbContext
is your EF CoreDbContext
:
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:
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.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.None
- The database is left as-is; use this only for tests that don't modify the database.
Product | Versions 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. |
-
net8.0
- Microsoft.EntityFrameworkCore.SqlServer (>= 9.0.1)
- MSTest.TestFramework (>= 3.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.