Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Entity Framework Integration Tests - Cannot Drop Db, because it is currently in use
Dwaldo
#1 Posted : Tuesday, May 21, 2013 3:40:53 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/21/2013(UTC)
Posts: 1
Location: United States of America

I've about driven myself nuts with this problem, issue was I have so much faith in NCrunch, I looked everywhere else first :) I've been using NCrunch for integration Test with EF, with some success, on occasion I'd get a failed test, and just hit the (Reset) button and all was golden.

Then I started a new project that was slightly more complex and has a few more operations hitting the Db. I use MsTest and an [AssemblyInitialize] attribute, and in that initializer I make a call to EF's code first initializer DropCreateDatabaseAlways. Hence in the first thing that runs when the test assembly loads, I'm loading my Db with a fresh set of data.

This is where the weirdness starts. NCrunch for the first two or classes or so didn't give me much problem but when I added a third test class, I started getting the following error. Cannot drop database "FatigueDb" because it is currently in use. All my test start failing, and it appears that each class is somehow calling into the assembly initializer, as each class has the same error in the test runner window... At first I thought I had a problem with my EF DbInitializer.

This is my third day on the problem, and I finally came round to the conclusion that its an NCrunch issue. I did some experimenting, turned off NCrunch engine and ran test using the built-in VS MsTest runner. All tests pass, I run again, they pass, I turn on NCrunch engine, all test in NCrunch fail, and MsTest runner test fail, I turn off NCrunch and MsTest runner makes them pass.

I end up on this form, I learn about [ExclusivelyUses("Database")] attribute, and I try that, I try the serialize attribute, I get lots smarter about NCrunch settings, I turn off parallel test execution. Finally I read a post on here about a guy that has a similar issue, he says ignore all test then un-ignore and they'll pass. I do that, and they do pass, until I add another test or make a change, then it's back to the same error.

So, not sure what to make of this issue, the attributes and settings don't seem to be showing me much love. For now I'm going to move my integration test strictly over to MsTests, any hints would be nice? I'm simply using EF, EF DbInitializer, with LocalDb stored in my User\ folder. Pretty slick setup for integration tests, runs quick except for the described problem when using NCrunch.
Remco
#2 Posted : Tuesday, May 21, 2013 10:40:50 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,988

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Hi Dwaldo,

Thanks for posting! I'm glad you've been enjoying NCrunch and I'm sure we can solve this problem. There was a user that posted with a very similar issue a while back - you may want to have a read: http://forum.ncrunch.net/yaf_postst527_StructureMap-issue-AssemblyInitialize.aspx.

Essentially, NCrunch behaves slightly differently to other test runners regarding its continued use of test runner processes. The post above suggests a way to handle this in your test code.

Another option is to label each of your tests with the IsolatedAttribute, although I would only recommend this as a last resort as you'll notice a big decrease in test performance if you do this.

For tests that behave in this way and rely on the same database, you'll certainly need to have parallel execution disabled. If your system has sufficient resources, you may want to look into rigging up the tests so that they build different test databases with names that match the process ID of the test processes - in this way, you can have separation between the test runs allowing them to work in parallel.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.037 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download