Home

SQL Server Unit Test Project using Visual Studio

%3CLINGO-SUB%20id%3D%22lingo-sub-1412161%22%20slang%3D%22en-US%22%3ESQL%20Server%20Unit%20Test%20Project%20using%20Visual%20Studio%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1412161%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20All%2C%3C%2FP%3E%3CP%3EI%20have%20created%20SQL%20Unit%20Test%20project%20using%20the%20built-in%20Visual%20Studio%20Test%20project.%20I%20am%20able%20to%20create%20test%20cases%20for%20all%20scenarios%20using%20Pre-Test%2C%20Test%20and%20Post-Test%20conditions.%20Also%2C%20I%20am%20able%20to%20create%20the%20Database%20in%20run-time%20using%20DACPAC.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAll%20these%20are%20fine%20till%20now.%20The%20problem%20I%20am%20facing%20is%2C%20I%20am%20not%20able%20to%20drop%20the%20database%20post%20execution%20of%20all%20my%20test%20cases.%20I%20have%20tried%20the%20following%20approaches%20where%20it%20failed%20to%20drop%20the%20database%3A%3C%2FP%3E%3CP%3E1.%20Custom%20scripts%20to%20forcefully%20drop%20the%20database%20in%20the%20finally%20block%20of%20the%20last%20test%20case%3C%2FP%3E%3CP%3E2.%20Order%20the%20test%20cases%20and%20in%20the%20Post-Test%20script%20of%20the%20last%20test%20case%2C%20added%20the%20drop%20script%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20a%20way%20that%20this%20can%20be%20achieved%3F%20All%20I%20need%20in%20short%20is%20-%20Drop%20the%20database%20at%20the%20end%20of%20my%20test%20execution%20of%20the%20last%20test%20case.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20can%20you%20help%20me%20with%20this%20or%20point%20me%20in%20the%20right%20direction%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3EAravind%20S%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1412161%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQL%202014%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESQL%20Server%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESQL%20Unit%20Test%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eunit%20test%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
New 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?

 

Thanks

Aravind S

2 Replies
Highlighted

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

Highlighted

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.

 

[AssemblyCleanup]
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. 

 

Thanks