简体   繁体   中英

Can I force MSTest to use a new process for each test run?

We're using the VS 2010 test runner (MSTest) for automated functional testing. When we run tests from Visual Studio, VS creates a process called QTAgent32.exe, and it runs the tests in that process.

We've found that when we do multiple test runs, MSTest will reuse the same QTAgent32 process - the process ID does not change. This is a problem for us, since the code we're testing is P/Invoking to an unmanaged DLL. The DLL needs to be initialized only once during the lifetime of the process. We have an [AssemblyInitialize] method, but that executes once per test run. If we perform multiple test runs, it will execute more than once in the same process.

Every time we do a test run, MSTest creates a new appdomain; but these appdomains are all in the same process.

So I'm wondering: is there a way to tell the Visual Studio test runner to use a new process every time we run tests? I looked at the ".testsettings" configuration but didn't see anything relevant.

dont know how far you want to go with it, but one solution could be to create your unit test host

http://technet.microsoft.com/fr-fr/query/bb166558

this link shows how to create adapters, also you could then launch a new process for evertest, create a piped communication and tear it down after the test.

I know MS itself uses a different host for running tests under moles

http://research.microsoft.com/en-us/projects/pex/molestutorial.pdf

I was able to get this working after reading Wiktor's comment about FreeLibrary().

I used this class created by Mike Stall, which provides wrappers around LoadLibrary, GetProcAddress, and FreeLibrary. That way, I can load the library once in each test run, call the necessary methods, and then free the library at the end of the test run.

Mike Stall's code uses Marshal.GetDelegateForFunctionPointer , which converts an unmanaged function pointer to a managed delegate type.

I had to replace the [DllImport] extern declarations with declarations for delegate types. So I converted this:

[DllImport("asesignal.dll")]
public static extern bool ASESDK_Initialize(string licenseCode);

to this:

public delegate bool ASESDK_Initialize(string licenseCode);

Mike Stall's code contained examples with generic delegates (Action<T> etc.). But I couldn't get that working, so I created my own delegate types.

I can load the DLL dynamically like this:

_ht = new UnmanagedLibrary(@"c:\windows\system32\asesignal.dll");

To call a function, I do this:

var function = _ht.GetUnmanagedFunction<ASESDK_Initialize>("ASESDK_Initialize");
function(licenseCode);

Thanks Wiktor and np-hard for your help!

VS 2013 and forward now has a setting for this under Test > Test Settings > Keep Test Execution Engine Running. Unchecking this selection will start up a new engine each run.

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