{"id":24782065,"url":"https://github.com/leonbouquiet/databasetestsetmanager","last_synced_at":"2026-02-13T08:40:55.936Z","repository":{"id":272902614,"uuid":"918119367","full_name":"LeonBouquiet/DatabaseTestSetManager","owner":"LeonBouquiet","description":"Painlessly and quickly bring your test database into the correct state before each unit test.","archived":false,"fork":false,"pushed_at":"2025-01-28T15:02:02.000Z","size":30,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-28T15:37:45.023Z","etag":null,"topics":["database","efcore","entity-framework-core","localdb","mstest","sql-server","unittest"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LeonBouquiet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-17T09:37:43.000Z","updated_at":"2025-01-28T15:01:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"baab886e-470c-433e-a0aa-e5fa286eef7f","html_url":"https://github.com/LeonBouquiet/DatabaseTestSetManager","commit_stats":null,"previous_names":["leonbouquiet/databasetestsetmanager"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeonBouquiet%2FDatabaseTestSetManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeonBouquiet%2FDatabaseTestSetManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeonBouquiet%2FDatabaseTestSetManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeonBouquiet%2FDatabaseTestSetManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LeonBouquiet","download_url":"https://codeload.github.com/LeonBouquiet/DatabaseTestSetManager/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":236169081,"owners_count":19106105,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","efcore","entity-framework-core","localdb","mstest","sql-server","unittest"],"created_at":"2025-01-29T11:15:57.114Z","updated_at":"2026-02-13T08:40:55.863Z","avatar_url":"https://github.com/LeonBouquiet.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DatabaseTestSetManager\r\n\r\nPainlessly and quickly bring your test database into the correct state before each unit test.\r\n\r\nCurrently supports a combination of MSTest, SQL Server and Entity Framework Core.\r\n\r\n## How do I use it?\r\n\r\n1. Add the DatabaseTestSetManager nuget package to your unittest project.\r\n2. 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.\r\n3. Create a unittest class and have it derive from DatabaseTestBase\u003cTDbContext\u003e, where `TDbContext` is your EF Core `DbContext`:\r\n\r\n```\r\npublic class ProductRepositoryTest: DatabaseTestBase\u003cAcmeDbContext\u003e\r\n{\r\n\tpublic override string ConnectionString =\u003e \"Server=(LocalDB)\\\\MSSQLLocalDB;Initial Catalog=AcmeUnitTestDB;Integrated security=True;TrustServerCertificate=True\";\r\n\r\n\tpublic override void OnDefineTestSets()\r\n\t{\r\n\t\tTestSetManager.DefineTestSet(\"Default\", setup =\u003e\r\n\t\t\t setup.FromAllEmbeddedSqlScripts(inAssemblyThatDefines: this));\r\n\t}\r\n\r\n\t// Test methods go here...\r\n}\r\n```\r\n\r\nThat's all there is to it to have your unittest database be initialized with the same data before each unittest.\r\n\r\n That is, if you define a unittest like this:\r\n\r\n```\r\n\t[TestMethod]\r\n\tpublic async Task GetProducts_ReturnsData()\r\n\t{\r\n\t\tProductRepository productRepository = new ProductRepository(this.DbContext);\r\n\r\n\t\t//Act\r\n\t\tList\u003cEntities.Product\u003e products = await productRepository.GetProducts();\r\n\r\n\t\t//Assert\r\n\t\tAssert.IsTrue(products.Any());\r\n\t}\r\n```\r\nThe DatabaseTestSetManager ensures that the sql scripts are run against your unittest database before the first unittest. \r\n\r\nAfter 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.\r\n\r\n## Ways to revert database changes\r\n\r\nYou 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:\r\n\r\n\t[DatabaseTestSet(\"Default\", CleanUpChanges = DatabaseCleanUpChanges.ByRollback)]\r\n\tpublic async Task GetProducts_ReturnsData()\r\n\t{\r\n\t\t...\r\n\r\nFor `DatabaseCleanUpChanges`, three values are supported:\r\n1. `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.\r\n2. `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.\r\n3. `None` - The database is left as-is; use this only for tests that don't modify the database.\r\n\r\n## Frequently Asked Questions\r\n\r\n# Can I see a full example of how it should be used?\r\n\r\nSure, check out the [Sample solution on GitHub](https://github.com/LeonBouquiet/DatabaseTestSetManager/tree/main/src/Sample).\r\n\r\n# Does it support parallelization?\r\n\r\nSadly not at this moment, but hopefully I can find some time to look into that.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonbouquiet%2Fdatabasetestsetmanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleonbouquiet%2Fdatabasetestsetmanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleonbouquiet%2Fdatabasetestsetmanager/lists"}