【问题标题】:VS MSTest runner locks System.Data.SQLite.dll when running in memory testsVS MSTest 运行器在内存测试中运行时锁定 System.Data.SQLite.dll
【发布时间】:2011-09-08 05:36:40
【问题描述】:

我正在使用 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) 中调试此测试时,清理 失败 并出现错误: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 的句柄。我已经尝试根据another question 的一些建议修改Cleanup 方法,但它仍然不起作用:

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

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

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

【问题讨论】:

  • 好吧,只是为了确保数据库被完全破坏。
  • 奇怪的是错误信息中的路径是bin\Debug,即使你是在Release模式下构建的
  • @Patrick - 抱歉,我的意思是 运行 测试而不是 调试 测试。在 Release/Debug 构建配置中,调试测试后清理将失败。
  • 调用 GC.Collect 两次有效吗? (不太可能但值得一试)stackoverflow.com/questions/3829928/…
  • 我也有同样的问题。 QTAgent32 坚持使用 SQLite.DLL,我 使用 NHibernate。

标签: c# visual-studio-2010 sqlite fluent-nhibernate mstest


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    相关资源
    最近更新 更多