As I wrote in one of my previous posts (see here) SSDT leaves test data in tables after it executes INSERT command. One of the methods to delete these data is to use a special script in post-test action. The second method is to use transaction to run the unit test.
Obviously we can use explicit BEGIN TRANSACTION and ROLLBACK commands to execute test script. But it needs some effort to change the testing script. SSDT offers another graceful solution as described in MSDN. We can declare the transaction in C# code (that’s .cs file for unit test).
To execute only one test from the test class in a transaction we should do the following:
- Right-click the unit test in Solution Explorer and select View Code.
- Find the declaration of your particular test
- Place the code from inside the function body into the following block:
using (TransactionScope ts = new TransactionScope(System.Transactions.TransactionScopeOption.Required))
.... inside-code of the function
- Declare TransactionScope variable in a test class.
- Create a transaction in the Init() method.
- Dispose of the transaction in the Cleanup() method.