VS MSTest runner blocks System.Data.SQLite.dll when working in memory tests

I use 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 need to do anything. } } } 

After running this test, I will try to run Clean whole solution. The results are as follows:

  • With this test (CTRL + R, CTRL + T), the cleanup may succeed as expected.
  • When debugging this test in (CTRL + R, T), the cleaning 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. 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 fine, remove the dll. When I try, I will be prompted that QTAgent32.exe is currently using the DLL. To verify this, I used Process Explorer. For some reason, the ms test runner stores a DLL handle. I tried modifying 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 was able to reproduce this on three different machines. Any known method of solving this problem would be very helpful.

Update . I cleared the linguistic confusion. The actual solution configuration may be in Debug / Relase. However, running tests and debugging tests causes a difference in error messages.

+6
source share
1 answer

I constantly faced a similar problem (SQLite.dll was blocked by the Visual Studio tester, although in Debug mode it was mainly using MbUnit tests) and found that using TestDriven.net to run my tests solved the problem for me. I never looked at the MSTest runner again after that, sorry.

0
source

Source: https://habr.com/ru/post/889752/


All Articles