
To create a transaction for a single test method
In this example, you are using an ambient transaction when you use the TransactionScope type. By default, the Execution and Privileged connections will not use the ambient transaction, because the connections were created before the method is executed. The SqlConnection has an EnlistTransaction method, which associates an active connection with a transaction. When an ambient transaction is created, it registers itself as the current transaction, and you can access it through the Current property. In this example, the transaction is rolled back when the ambient transaction is disposed. If you want to commit any changes that were made when you ran the unit test, you must call the Complete method.
To create a transaction for a single test method
Open the Visual Basic or C# file for your unit test.
Wrap the pre-test, test and post-test actions as shown in the following Visual Basic code example:
<TestMethod()> _
Public Sub dbo_InsertTable1Test()
Using ts as New System.Transactions.TransactionScope( System.Transactions.TransactionScopeOption.Required)
ExecutionContext.Connection.EnlistTransaction(Transaction.Current)
PrivilegedContext.Connection.EnlistTransaction(Transaction.Current)
Dim testActions As DatabaseTestActions = Me.dbo_InsertTable1TestData
'Execute the pre-test script
'
System.Diagnostics.Trace.WriteLineIf((Not (testActions.PretestAction) Is Nothing), "Executing pre-test script...")
Dim pretestResults() As ExecutionResult = TestService.Execute(Me.PrivilegedContext, Me.PrivilegedContext, testActions.PretestAction)
'Execute the test script
System.Diagnostics.Trace.WriteLineIf((Not (testActions.TestAction) Is Nothing), "Executing test script...")
Dim testResults() As ExecutionResult = TestService.Execute(ExecutionContext, Me.PrivilegedContext, testActions.TestAction)
'Execute the post-test script
'
System.Diagnostics.Trace.WriteLineIf((Not (testActions.PosttestAction) Is Nothing), "Executing post-test script...")
Dim posttestResults() As ExecutionResult = TestService.Execute(Me.PrivilegedContext, Me.PrivilegedContext, testActions.PosttestAction)
'Because the transaction is not explicitly committed, it
'is rolled back when the ambient transaction is
'disposed.
'To commit the transaction, remove the comment delimiter
'from the following statement:
'ts.Complete()
End Sub
Private dbo_InsertTable1TestData As DatabaseTestActions
Note: |
|---|
If you are using
Visual Basic, you must add Imports System.Transactions (in addition to Imports Microsoft.VisualStudio.TestTools.UnitTesting, Imports Microsoft.VisualStudio.TeamSystem.Data.UnitTesting, and Imports Microsoft.VisualStudio.TeamSystem.Data.UnitTest.Conditions). If you are using Visual C#, you must add using System.Transactions (in addition to the using statements for Microsoft.VisualStudio.TestTools, Microsoft.VisualStudio.TeamSystem.Data.UnitTesting, and Microsoft.VisualStudio.TeamSystem.Data.UnitTesting.Conditions). You must also add a reference to your project to those assemblies.
|