SSDT Database Unit Test: Cleanup after Test Execution

In my previous research about database unit testing in SQL Server Data Tools I’ve found that data inserted into tables during test had not been deleted automatically. You should create a cleanup action manually.

  • Choose an item Post-test in dropdown list shown below.
Create post-test
  • Click the link Click here to create in the center of Test Designer.
  • Write T-SQL code to delete test data.
Post-test delete

I truncate table Clients and run my test. Test is OK and there’s no test data in Clients table. So I could run my test as many times as needed.

There’s a possibility to include test condition even in post-test phase. Wonderful, I’ll check that test data is deleted successfully.

  • Add simple SELECT command to query rows with test guid.
  • Add test condition to check no data is returned. This can be made with Row Count = 0 condition or Empty ResultSet condition. I choose the second.
The whole post-test action looks like this:
post-test delete with check
Unit test has passed. If you are in doubt about all actions or not were really performed, you can check the output of the test. To see it click the Output link at the bottom of Test Explorer pane.
Test output

Leave a Reply

Your email address will not be published. Required fields are marked *