SQL Server Unit Test Project using Visual Studio

Copper Contributor

Hello All,

I have created SQL Unit Test project using the built-in Visual Studio Test project. I am able to create test cases for all scenarios using Pre-Test, Test and Post-Test conditions. Also, I am able to create the Database in run-time using DACPAC.


All these are fine till now. The problem I am facing is, I am not able to drop the database post execution of all my test cases. I have tried the following approaches where it failed to drop the database:

1. Custom scripts to forcefully drop the database in the finally block of the last test case

2. Order the test cases and in the Post-Test script of the last test case, added the drop script


Is there a way that this can be achieved? All I need in short is - Drop the database at the end of my test execution of the last test case.


Please can you help me with this or point me in the right direction?



Aravind S

2 Replies

Hi @AravindS2190 -- What error are you receiving when attempt to drop the database?

Hello @bake13 - I am not receiving any errors. Whereas I am looking for options how to do.

But I have finally found a way to do it. 


I have used [AssemblyCleanup] attribute to drop my database post completion of all the tests. This [AssemblyCleanup] will be executed once per assembly and this is what I was looking for.


public static void MyCleanup()
    //Your test code goes here
      // Read from config file
      // Drop your database


Now with my SQL Unit Test, I can create database in any local server using DACPAC, Unit test them and finally drop them as well.