Sql – Is this file lock issue due to me copying the DB file for every MSTest test? (using DeploymentItem annotation)

mstestsqliteunit testingvisual-studio-2008

BACKGROUND:

  • I've running unit tests in VS2008 using MSTest.
  • My project has a SqlLite database.
  • I have found that I needed to arrange for the default database file to be copied to the MSTest area for the test to be able to find it. I am using the following annotation about the test code to arrange this:
    [DeploymentItem("database.db3")]

ISSUE:

  • When I run the "all tests" at once via VS2008 I get the below-mentioned error on one of the tests. When I run this test alone it passes fine.
  • The error I am getting is:
    "System.Data.SQLite.SQLiteException: The database file is locked database is locked"
  • Again, when I run the test that throughs this issue alone it works fine & passes

QUESTION:

Any ideas/suggestions regarding how to address this issue? Is it due to the way I'm manually copying the database file for each test (i.e. each test I have one of the above-mentioned annotations)

Example of full test:

/// <summary>
///A test for process
///</summary>
[TestMethod()]
[DeploymentItem("database.db3")]
public void processTest()
{
    Coordinator target = new Coordinator();
    target.MyConfig.clear_database();

    target.process();
}

Thanks

Best Answer

Sounds like the first unit test to run leaves the DB file open. Be sure to close the DB file.

Try using a [TestInitialize()] method in you test fixture to copy/create and then open the DB file while using a [TestCleanup()] method to close and then delete the DB file.

Related Topic