Skip to content Skip to sidebar Skip to footer

Nunit Integration Tests On Web Api - How To Create/destroy Integration Test Db

I'm implementing NUnit Integration Tests of our controller REST endpoints in a .NET Web API 2 project. We use an Entity Framework code-first from database approach to create our co

Solution 1:

Probably not the answer you are looking for but I have had success recently using the sql server docker image with docker compose.

You can fire up a database instance and delete the data volumes when the the image shuts down. Using the —rm switch on the docker run command will do that automatically for you.

If you are using dot net core you can setup another container to run your entity framework migrations and tests.

If you are using dotnet framework you maybe be able to run windows docker images however they tend to be a bit slower to startup.

This approach would work best if you launched everything from a powershell script. Launching the infrastructure from the code as you are looking to do could be tricky and perhaps more complex than it needs to be.

Solution 2:

Going line-by-line through the type of sql commands you'll need for these operations is just going to be painful. You would benefit much better to just develop a stored procedure that does the tear-down/build-up steps. You appear to already have a start in that as I see you writing code around the statements. Then your integration test code would just need to call this procedure and wait for the setup to complete. Remember, You don't have to do everything in code.

Post a Comment for "Nunit Integration Tests On Web Api - How To Create/destroy Integration Test Db"