VS MSTest运行程序在内存测试中运行时锁定System.Data.SQLite.dll



我使用Fluent NHibernate使用SQLite 1.0.66.0:运行内存数据库测试(MS测试)

[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;
    }
}

然后以这个为例:

[TestClass]
public class MessagesControllerTests : InMemoryDatabaseTest
{
    [TestMethod]
    public void SQLite_should_have_all_handles_released()
    {
        using (var session = this.CreateSession())
        {
            // Don't even need to do anything.
        }
    }
}

运行此测试后,我尝试Clean整个解决方案。结果如下:

  • 当运行此测试(CTRL+R,CTRL+T)时,清理能够按预期成功
  • 在(CTRL+R,T)中调试此测试时,clean失败,返回错误:C:WindowsMicrosoft.NETFrameworkv4.0.30319Microsoft.Common.targets(3607,9): warning MSB3061: Unable to delete file "PathToProjectbinDebugSystem.Data.SQLite.DLL". Access to the path 'PathToProjectbinDebugSystem.Data.SQLite.DLL' is denied.

我的第一个想法是可以的,删除DLL。当我尝试时,系统会提示我QTAgent32.exe当前正在使用DLL。我使用Process Explorer对此进行了验证。出于某种原因,ms测试运行程序保留了DLL的句柄。我试着用另一个问题的一些建议来修改Cleanup方法,但它仍然不起作用:

[TestCleanup]
public void Cleanup()
{
    new SchemaExport(this.configuration).Drop(false, true);
    sessionFactory.Close();
    sessionFactory.Dispose();
    SQLiteConnection.ClearAllPools();
    GC.Collect();
}

我已经能够在3台不同的机器上复制这个。任何已知的解决这个问题的方法都将不胜感激。

更新:我已经清理了一些语言混乱。实际的解决方案配置可以是Debug/Rease。但是,运行测试与调试测试会导致错误消息的差异。

我一直遇到类似的问题(SQLite.dll被Visual Studio测试运行程序锁定,尽管主要处于调试模式并使用MbUnit测试),并发现使用TestDriven.net运行测试为我解决了问题。在此之后,我再也没有深入研究MSTest运行程序,对不起。