BrandonAGr
BrandonAGr

Reputation: 6017

How to attach debugger to command line mstest

I have a unit test suite using mstest that I can run fine frome inside visual studio, but when my deploy scripts tries to run the tests using a command line call to mstest it will freeze during a test half the time. It is likely a problem in the test, but without being able to reproduce the issue inside a debugger I have been unable to find the issue.

So far I have been unable to attach the mstest process to be able to debug the issue, as when I attach and pause I see nothing in visual studio (no threads listed, no known code). Is there something odd about how it uses appdomains that prevents easily attaching to it? Any other good ways to try and troubleshoot, is it even possible to do the equivalent of Console WriteLine from inside the test so that mstest will display it in the console window its running in?

Upvotes: 11

Views: 6834

Answers (3)

Andrey Burykin
Andrey Burykin

Reputation: 736

Here is my suggestion for vstest.console tool:

  1. Add new test method at the top of test file, so this test will be executed first in vstest.console tool:

    [TestMethod]
    public void DebugAttachToProcessTimeout()
    {
        Console.ReadLine();
    }
    
  2. Start Vstest.console.exe with appropriate assembly as param. Tool will try to check first test and wait for user's input.

  3. Switch to Visual Studio and go to Debug -> Attach to process (CTRL+ALT+P). Then select 'Vstest.console.exe' and click 'Attach'.

  4. Now you can go back to console and press enter. Tool will continue execute tests with attached visual studio.

Upvotes: 1

Rob
Rob

Reputation: 4360

Two options.

  1. In the IDE uncheck Test -> Test Settings -> Keep Test execution engine running.

or

  1. To use the commandline:

Run mstest with the /noisolation switch. It will execute the tests directly rather than spawning a helper process.

Mixed: Automatically attach the VS debugger to the command line mstest.exe:

I configure the Debug tab for my test project with the following:

Start External Program: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\MSTest.exe

Command line arguments: /noisolation /testcontainer:MyProjectName.dll

Upvotes: 5

BrandonAGr
BrandonAGr

Reputation: 6017

After looking at the process tree in Process Explorer, MSTest.exe was launching a child process named QTAgent32_40.exe, I was able to attach to that process and turn off Just my code so that I could debug my tests.

Turns out it was effectively deadlocking inside a mock object I created that was using MethodImplOptions.Synchronized

Upvotes: 4

Related Questions