1

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

  1. 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?

Thanks

4
  • If SQL Unit Test for SSDT is anything like tSQLt then each unit test is running inside an SQL transaction and the transaction gets rolled back at the end of the test. That being the case you won't be able to drop the database from within a test because there's a connection and open transaction in progress to the database. Commented May 23, 2020 at 0:13
  • Can we use custom scripts then? Any examples of how to use AssemblyCleanUp or ClassCleanUp would help. Commented May 23, 2020 at 14:23
  • You are totallyable to drop the database. Why are you not? Error message? You msut force disconnection of connected users - which is trivial to do. Commented May 23, 2020 at 17:20
  • I am able to drop it now. Mentioned my answer below. Thanks for your suggestion 👍 Commented May 23, 2020 at 18:38

1 Answer 1

1

Use [AssemblyCleanup] attribute to remove Database post your test execution. These will get executed once per Assembly. In this way I was able to drop the database which was created during Unit Test.

[AssemblyCleanup]
public static void MyCleanUp()
{
    //Your clean-up code
    // Read the connection from config file
    //Drop the database
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.