0

When choosing to run all tests, and there is logic within [TestInitialize] method to delete a file, anything after the first completed test receives the following error:

The process cannot access the file 'C:\root\var\MonRequestGeneratorTests.sqlite' because it is being used by another process..

Here is the Initialize method:

[TestInitialize]
public override void Initialize()
{
    string sqliteFilePath = "myPath";
    if (File.Exists(sqliteFilePath)) {
        File.Delete(sqliteFilePath);
    }
}

How do I get around this with tests? This is a SQLite database, and the means of dropping it is deleting a file. I need that file to be deleted prior to each test running.

blgrnboy
  • 4,877
  • 10
  • 43
  • 94

1 Answers1

0

This means that the sqlite file is either open in some other program (like some SQL management software), or that you did not dispose your streams after accessing it during the tests.

Whenever you open a file using a FileStream or something similar, make sure you do so in a 'using' clause so that it is disposed later.

One thing that I would recommend, is instead of deleting the data during initialization, delete it during teardown. Assume that your workspace is clean, and make sure you leave it clean when you're finished. That way, if some test did not dispose of it's FileStream, the offending test will fail, and not the test run after that.

Gilthans
  • 1,656
  • 1
  • 18
  • 23
  • Problem is that Entity Framework is using this, and the DB Context is in a using block. – blgrnboy Jul 28 '16 at 21:50
  • This seems to be another issue with Entity Framework: http://stackoverflow.com/questions/16979635/dbcontext-doesnt-release-sqlite-database – Gilthans Jul 28 '16 at 21:51