C# VS MSTest runner在运行内存测试时锁定System.Data.SQLite.dll
我正在使用Fluent NHibernate使用SQLite 1.0.66.0运行内存数据库测试(MS测试):C# VS MSTest runner在运行内存测试时锁定System.Data.SQLite.dll,c#,visual-studio-2010,sqlite,fluent-nhibernate,mstest,C#,Visual Studio 2010,Sqlite,Fluent Nhibernate,Mstest,我正在使用Fluent NHibernate使用SQLite 1.0.66.0运行内存数据库测试(MS测试): [TestClass] public abstract class InMemoryDatabaseTest { private NHibernate.Cfg.Configuration configuration; private ISessionFactory sessionFactory; [TestInitialize] public void
[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.
}
}
}
运行此测试后,我尝试清理整个解决方案。结果如下:
- 当运行此测试(CTRL+R、CTRL+T)时,清理能够按预期成功
- 在(CTRL+R,T)中调试此测试时,清除失败,错误为:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3607,9):警告MSB3061:无法删除文件“PathToProject\bin\Debug\System.Data.SQLite.DLL”。对路径“PathToProject\bin\Debug\System.Data.SQLite.DLL”的访问被拒绝。
QTAgent32.exe
当前正在使用DLL。我使用Process Explorer验证了这一点。出于某种原因,ms测试运行程序正在DLL上保留一个句柄。我尝试了修改清理
方法,并从中获得了一些建议,但仍然不起作用:
[TestCleanup]
public void Cleanup()
{
new SchemaExport(this.configuration).Drop(false, true);
sessionFactory.Close();
sessionFactory.Dispose();
SQLiteConnection.ClearAllPools();
GC.Collect();
}
我已经能够在3台不同的机器上复制这个。任何已知的解决此问题的方法都将不胜感激
更新:我已经消除了一些语言上的混乱。实际的解决方案配置可以处于调试/松弛状态。但是,运行测试和调试测试会导致错误消息的不同。我一直都有类似的问题(SQLite.dll被Visual Studio测试运行程序锁定,尽管主要处于调试模式并使用MbUnit测试),并且发现使用TestDriven.net运行测试为我解决了问题。在此之后,我再也没有进一步查看过MSTest runner,很抱歉。好吧,只是为了确保数据库被完全销毁。奇怪的是,即使在发行版中构建,错误消息中的路径仍然是bin\Debugmode@Patrick-对不起,我的意思是运行测试而不是调试测试。在这两种版本/调试构建配置中,在调试测试之后,清除将失败。调用GC.Collect两次是否有效?(不太可能,但值得一试)我也有同样的问题。QTAgent32保留着SQLite.DLL,我没有使用NHibernate。