22
votes

Whenever I try to run any of the test from my test suite in Visual Studio 2012 I get

Test Failed - [test method name]

Message: Failed to set up the execution context to run the test

and my test is not even started (i.e. the source of the test failure is pointing at the beginning of the test method:

here--> TEST_METHOD([test method name]) {

}

What does this message mean, what could be the cause for it to appear and what should I do for my test to run correctly?

7
Could you submit this as a bug at connect.microsoft.com/visualstudio?Oleg Sych

7 Answers

23
votes

After some more research I realized that I overlooked the fact that I changed the 'Output Directory' of the project containing the methods under test (which was a .dll) and was not in the same folder as my test project library, hence giving me the message:

Message: Failed to set up the execution context to run the test

So the problem was solved by making sure that the .dll containing the methods under test was in the same folder as my test project library so that at run-time my test project could find the .dll.

18
votes

I had the same issue and the previously mentioned suggestions did not fix it for me. My project uses some third party libraries and the paths to these are set up correctly in the compiler and linker settings of my VS project.

It turns out that the VS test engine was unable to find the libraries, so I added the paths to these libraries to the PATH environment variable. This fixed the issue for me.

Tip: Try running the unit tests from the command line using VSTest.Console.exe. The error messages helped me debug my issue more easily.

10
votes

For those searching for other answers, this turned out to be a problem finding all the DLLs needed. I followed the advice above getting both the test dll and the dll to be test in the same location but still got this error.

My test DLL, out of its native environment, couldn't find its children DLL. You can figure out what is missing by running depends.exe . Adding the locations to these other DLL's to my path resolved the problem and everything now works.

2
votes

I was getting this error, and it was because I was using a debug DLL, and did not have debug c++ runtimes where they could be found. I copied the debug c++ runtimes into the same directory and the issue was resolved.

2
votes

I had this error after including a third party dll (OpenCV) in my project. Adding the dll to path or dropping it in the system32 directory does work, but I have a better solution.

The test is run from a directory called AppX. In my case it is here: .......OneDrive\Documents\Visual Studio\Projects\TutorialOcr\x64\Debug\OcrTesting\AppX

I just dropped the dll in there and the test worked!

1
votes

The .dll created for the tests is run from the folder where it is built to. In my case "x64\UnitTests\Tests.dll". The rest of my application is in "x64\Debug\App.exe" and "x64\Release\App.exe". My application depends on external dlls which are located in the root folder of the project, which is also the "Working Directory" specified for debug launch.

But the Test Explorer test launcher ignores that setting and always launches Tests.dll with working directory "x64\UnitTests", and then fails to find the dlls I depend on. Adding SetCurrentDirectory("..\\.."); in, say, the test class constructor does not fix the problem, because the dll cannot even be loaded into memory if the static dependencies are not found.

I solved it by just setting the "Output Directory" to "$(SolutionDir)" for the UnitTests configuration. This causes the Tests.dll to be created in the root folder.

0
votes

I was also getting this error only in the Release configuration, because I accidentally used a debug lib as one of the library dependencies of my test project. ( same problem as bmann's post )

To find which library was causing the problem, I commented out all the test code and its includes, added one empty test, and removed my dependency libraies one by one until the test worked.