简体   繁体   中英

Visual Studio Unit Test locking dll

Simple unit test for abstract class with p/invoked methods is leaving the unmanaged dll locked when tests are executed. Oddly enough the lock does not happen when debugging the test, only when test is run.

Similar problem here reported on Visual Studio 2012 and I am using a similar workaround, a bit less hacky but nonetheless not a good solution.

Workaround

Added a "teardown" method to be executed once tests terminate to manually free the unmanaged library.

[DllImport("kernel32", SetLastError=true)]
static extern bool FreeLibrary(IntPtr hModule);

[AssemblyCleanup]
public static void Teardown()
{
    foreach (ProcessModule mod in Process.GetCurrentProcess().Modules)
    {
        if (mod.ModuleName == "example.dll")
        {
            FreeLibrary(mod.BaseAddress);
        }
    }
}

My solution for running unit tests in vs:

  • Run an instance of the console as administrator and enter this script:

    taskkill /F /IM vstest* /T

  • before running a test switch to the console and execute the line above to kill any process related to the test framework.

I also manually build the test project before running a test because if I forget to kill the processes, vs will be stuck in build mode forever.

Unfortunately all this does not solve my main issue which is that I need to kill vs every 20 minutes while testing or debugging because it gets slow to the point of being unusable. ):

For fairness I must add that I run the tests from a Resharper test session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM