MSTest Unit tests (VS 2012) versus SQL Server, intermittent primary key collisions

I am dealing with a very strange problem. Initially, I thought it was a problem with cleaning up the test data ... but after completely refactoring the code, cleaning up the test data and still observing the same behavior ... I'm at a loss.

I have 245 unit test methods in different classes. Each class has its own unique test data, and I initialize these objects, and then in each test method the data is usually inserted into the database and then processed using tests. Each test class has a ClassCleanup method that clears all test data from the database and that ClassCleanup also runs in TestInitialize to make sure everything is cleared before any other test method runs.

When I "Run Everything" using VS 2012 Test Explorer, 22 tests fail. All of them fail with some changes to the restrictions of the primary key. Meaning, when they initialize the data for these tests, the data was not cleared of the previous test method in this class. If I re-run all the tests, I get the same tests, failures every time. This is pretty reproducible. No matter how many times I run all the tests. The same 27 tests fail with primary key violations.

However, it is strange that if I re-run ONLY those failed tests, only 9 tests will work. This is also reproducible, that is, no matter how many times I run only these 27 previously failed tests, 14 will fail, and the rest will fail. This continues when I run only failed tests until I get to the point where none of the tests have completed. It should also be noted that if I run each test class individually, everything passes.

I know what that looks like.

"You obviously are not clearing your test data." If so, then I should see that the same tests fail every time I run, no matter what. These 27 tests will not work EVERY run, and not just when I run the rest.

" , ". . . , ( , ), ( ThreadId), .

" ". , . SQL Profiler, . , , .

" ". , , , -, ( Pooling = false ), . .

" - ". , , (, , SQL 2012). SQL Server.

" mstest ". . .

- - , . , - , , , , , .

+5
2

, , (, / db ..).

, , .

  • . . , , 2-3 .

  • Sql RPC /, sql Batch /, Sql /, SP /, TM: * , SQLTransaction, DTCTransaction . .

  • . , , .

  • . , .

  • . , , . .

  • , . . sql . sql , ? ? ? ( transactionId , , ). BEGIN TRAN/COMMIT TRAN/ROLLBACK TRAN, transactionId .

  • . , . , "RESTORE DATABASE.... WITH NORECOVERY". "RESTORE LOG.. WTIH STOPAT, RECOVERY" .

  • , , , . , ? , . , .

  • . , . , PK. , ? , .

    • , , . COMMIT, , . STOPAT . , , .
    • , , . , , . , , , , , ..
  • , . , db, , . .

2012 Management Studio , . !

+2

, , - , transactionscope :

public void SetUp()
{
 _transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew);
}

. .

0

All Articles