在内存测试中运行时,VS MSTest运行器会锁定System.Data.SQLite.dll

The*_*Sky 7 c# sqlite mstest visual-studio-2010 fluent-nhibernate

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

[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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后以此为例:

[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.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 运行此测试(CTRL + R,CTRL + T)时,clean可以按预期成功.
  • 在(CTRL + R,T)中调试此测试时,清除失败并显示错误: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.

我的第一个想法是好的,删除DLL.当我尝试时,我被提示QTAgent32.exe当前正在使用DLL.我使用Process Explorer来验证这一点.由于某种原因,ms测试运行器正在处理DLL.我尝试Cleanup另一个问题的一些建议来修改metehod ,但它仍然无效:

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

我已经能够在3台不同的机器上重现这一点.任何知道解决这个问题的方法都将不胜感激.

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

Pat*_*ald 0

我不断遇到类似的问题(SQLite.dll 被 Visual Studio 测试运行程序锁定,尽管主要在调试模式下并使用 MbUnit 测试),并发现使用 TestDriven.net 运行我的测试解决了我的问题。抱歉,此后我再也没有进一步研究过 MSTest 运行程序。