Bootstrapping a database for a test session #1902
-
Hopefully this hasn't been asked before. Tried to search, but may not be articulating it in the same terms... Problem StatementI need to implement database-dependent tests with the following requirements:
Current ChallengeThe main challenge is implementing this in a way that:
QuestionsWhat's the recommended approach to implement session-wide setup that only executes conditionally? Other InfoIts a multi-tenant database, so I can scope the test to a specific/random tenant, to keep tests from stomping on each other. Therefore I don't need a fresh db for every test, just one for the entire session. What I've tried
Thanks so much! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Here is my third attempt, which I think works well enough and may be enough of a solution, if no one has a better approach... What I've tried (Part 3)
[NotInParallel]
internal abstract class DatabaseTest
{
private static int _invocationCount = 0;
[Before(Class)]
public static async Task Setup()
{
if (Interlocked.Increment(ref _invocationCount) > 1) return;
await DropCreate();
DatabaseMigrationRunnner.RunMigrations();
}
} |
Beta Was this translation helpful? Give feedback.
-
Use a
Alternative: Implement an Both of these solutions should, in theory, only run if you are running tests inside of classes that have one these attributes. Though they are assembly-wide, they are not constructed unless used. |
Beta Was this translation helpful? Give feedback.
Use a
[ClassDataSource<>(Shared = PerAssembly)]
and inject a fixture that sets up the db. The fixture should also implementIAsyncInitializer
andIAsyncDisposable
. You can see an example here:InitializeAsync()
; you can sed data in this method as well)Alternative: Implement an
IClassConstructor
, store a…