VS MSTest runner locks System.Data.SQLite.dll when running in memory tests
I'm using Fluent NHibernate to run in-memory database tests (MS Test) using SQLite 1.0.66.0:
[TestClass]
public abstract class InMemoryDatabaseTest
{
private NHibernate.Cfg.Configuration configuration;
private ISessionFactory sessionFactory;
[TestInitialize]
public void Initialize()
{
// All "CreateConfiguration" does is load FNh mappings.
this.configuration = new NhConfigurationBuilder()
.CreateConfiguration()
.Database(() => SQLiteConfiguration.Standard.InMemory())
.BuildConfiguration();
this.sessionFactory = this.configuration.BuildSessionFactory();
}
[TestCleanup]
public void Cleanup()
{
new SchemaExport(this.configuration).Drop(false, true);
sessionFactory.Dispose();
}
protected ISession CreateSession()
{
var session = this.sessionFactory.OpenSession();
// Re-create the database every time a new session is created.
new SchemaExport(this.configuration)
.Execute(script: false, export: true, justDrop: false, connection: session.Connection, exportOutput: null);
session.BeginTransaction();
return session;
}
}
And then using this as an example:
[TestClass]
public class MessagesControllerTests : InMemoryDatabaseTest
{
[TestMethod]
public void SQLite_should_have_all_handles_released()
{
using (var session = this.CreateSession())
{
// Don't even n开发者_StackOverflow社区eed to do anything.
}
}
}
After running this test, I try to Clean
the whole solution. The results are as follows:
- When running this test (CTRL + R, CTRL + T), the clean is able to succeed as expected.
- When debugging this test in (CTRL + R, T), the clean fails with the error:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3607,9): warning MSB3061: Unable to delete file "PathToProject\bin\Debug\System.Data.SQLite.DLL". Access to the path 'PathToProject\bin\Debug\System.Data.SQLite.DLL' is denied.
My first thought was ok, delete the DLL. When I try to, I'm prompted that QTAgent32.exe
is currently using the DLL. I used Process Explorer to verify this. For some reason the ms test runner is keeping a handle on the DLL. I've tried modifying the Cleanup
metehod with some suggestions from another question, but it still didn't work:
[TestCleanup]
public void Cleanup()
{
new SchemaExport(this.configuration).Drop(false, true);
sessionFactory.Close();
sessionFactory.Dispose();
SQLiteConnection.ClearAllPools();
GC.Collect();
}
I've been able to reproduce this on 3 different machines. Any know method to solve this issue would be greatly appreciated.
Update: I've cleaned up some linguistic confusion. The actual solution configuration can be in Debug/Relase. However, running the tests versus debugging the tests causes the difference in error messages.
I was continually having a similar problem (SQLite.dll being locked by the Visual Studio test runner, although in Debug mode mostly and using MbUnit tests) and found that using TestDriven.net to run my tests solved the problem for me. I never looked any further into the MSTest runner after this, sorry.
精彩评论