vscode-csharp icon indicating copy to clipboard operation
vscode-csharp copied to clipboard

Implement VSTest support to enable running and debugging .NET Core tests in .csproj projects

Open aochsner opened this issue 9 years ago • 65 comments

I realize I'm on pretty bleeding edge (.NET Core 1.1.0, .NET CLI 1.0.0 preview 4 (csproj), etc) but wanted to raise this and see if I can help w/ anything.

Environment data

.NET Core version

Microsoft .NET Core Shared Framework Host

  Version  : 1.1.0
  Build    : 928f77c4bc3f49d892459992fb6e1d5542cb5e86

dotnet --info output:

.NET Command Line Tools (1.0.0-preview4-004233)

Product Information:
 Version:            1.0.0-preview4-004233
 Commit SHA-1 hash:  8cec61c6f7

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.12
 OS Platform: Darwin
 RID:         osx.10.11-x64
 Base Path:   /usr/local/share/dotnet/sdk/1.0.0-preview4-004233

VS Code version: Version 1.8.1 (1.8.1)

C# Extension version: 1.6.2

Steps to reproduce

Create new xunit project dotnet new -t xunittest

Update references to 1.1.0 in csproj:

<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="**\*.cs" />
    <EmbeddedResource Include="**\*.resx" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NETCore.App" Version="1.1.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20161123-03" />
    <PackageReference Include="xunit" Version="2.2.0-beta4-build3444" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0-beta4-build1194" />
  </ItemGroup>

</Project>

Open in VS Code and navigate to Test.cs. Run Test via Omnisharp

Expected behavior

Test runs

Actual behavior

"Failed to run test because null" error

aochsner avatar Dec 30 '16 16:12 aochsner

This is the only thing holding me back from adopting the preview4 tooling. While "run test" is a less severe of a problem (I can just do that from the command line...) "debug test" has the same problem, and I don't have a good work around for that.

vcsjones avatar Jan 25 '17 17:01 vcsjones

+1

VladimirMakaev avatar Jan 26 '17 13:01 VladimirMakaev

Upon fixing the null reference (which was due to some ugly code that looked specifically for the presence of project.json), it looks like this might be quite seriously broken. Previously, OmniSharp just called dotnet test with --port and --parentProcessId flags to start communicating with the test framework. However, now that the .NET CLI has moved over to MSBuild (and VSTest), it's not clear whether this is possible any longer.

@piotrpMSFT, do you have any thoughts here? I'm guessing OmniSharp will need to commnicate a bit differently with VSTest using the details from https://github.com/microsoft/vstest. Do we just need to use dotnet vstest?

DustinCampbell avatar Jan 27 '17 15:01 DustinCampbell

OK. I'm making some progress with dotnet vstest using the spec at https://github.com/Microsoft/vstest-docs/blob/master/RFCs/0007-Editors-API-Specification.md. :smile:

DustinCampbell avatar Jan 27 '17 17:01 DustinCampbell

Keep in mind xUnit are looking to make their own command eg/dotnet xunit so not sure if that would change how tests are run/debugged in vscode @bradwilson

jchannon avatar Feb 03 '17 09:02 jchannon

I don't think it would. I think we'd still want to go through the VSTest protocol. Otherwise, we'd be writing custom code for every test framework that's supported.

DustinCampbell avatar Feb 03 '17 09:02 DustinCampbell

Exactly, just wanted to make sure that wasn't the case :)

On 3 February 2017 at 09:54, Dustin Campbell [email protected] wrote:

I don't think it would. I think we'd still want to go through the VSTest protocol. Otherwise, we'd be writing custom code for every test framework that's supported.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/OmniSharp/omnisharp-vscode/issues/1100#issuecomment-277207593, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGaphHL9WYhCEcgdr0uyBdZbzgiZtLFks5rYvlPgaJpZM4LYSEd .

jchannon avatar Feb 03 '17 09:02 jchannon

I think we'd still want to go through the VSTest protocol.

I agree, you should. dotnet xunit is about providing an equivalent to xunit.console.exe for .NET SDK applications. It is not intended to be automated by third parties.

bradwilson avatar Feb 03 '17 20:02 bradwilson

Will there be a .NET: Test command added to VS Code, similar to the .NET: Restore Packages command? That would be incredibly useful I feel.

ErikSchierboom avatar Feb 27 '17 12:02 ErikSchierboom

Eventually. This just tracks getting the CodeLens indicators for "run test" and "debug test" working for .csproj.

DustinCampbell avatar Feb 27 '17 21:02 DustinCampbell

Ah, I missed that. That would also be brilliant!

ErikSchierboom avatar Feb 28 '17 07:02 ErikSchierboom

ah well, another road blocker with xunit and vscode :( hope the 1.8.0 is coming very soon to fully support csproj and xunit

Jonathan34 avatar Mar 07 '17 16:03 Jonathan34

as a workaround: you can edit your tasks.json to add the test tasks you want, bind a shortcut like this:

[{
    "key": "shift+cmd+t",
    "command": "workbench.action.tasks.runTask",
    "when": "editorTextFocus"
}]

and select the test task. if you want to run all the tests, you can create dependencies using the tasks.json v2.0.0 (cf https://code.visualstudio.com/updates/v1_10#_more-work-on-terminal-runner)

alternatively, switch to vs 2017 :)

Jonathan34 avatar Mar 08 '17 22:03 Jonathan34

Great suggestion!

DustinCampbell avatar Mar 08 '17 22:03 DustinCampbell

@Jonathan34 Could you please clarify more the procedure that you suggested ? Could you start a debugging session for a specific test method using VSCode's tasks.json/launch.json ?

ghost avatar Mar 08 '17 23:03 ghost

I don't believe it would be possible to do that for debugging - from my understanding all the unit test stuff does the real work in child processes, which makes it incompatible with being launched under a debugger in a normal way.

gregg-miskelly avatar Mar 09 '17 00:03 gregg-miskelly

Which means we have to wait for the 1.9 milestone to be released before we can debug tests in csproj build system on Linux and macOS ?

ghost avatar Mar 09 '17 00:03 ghost

Yes, unfortunately for now the only way to debug unit tests is to put it a while (!Debugger.IsAttached) Sleep loop at the start of the test and attach. There is significant work to be done in OmniSharp-Roslyn to make this scenario work the right way and no one has done it yet.

gregg-miskelly avatar Mar 09 '17 01:03 gregg-miskelly

@anass-b this is my tasks.json:

{
    "version": "2.0.0",
    "command": " ",
    "isShellCommand": true,
    "args": [],
    "tasks": [{
            "taskName": "build",
            "command": "dotnet",
            "args": [
                "build"
            ],
            //"isShellCommand": true,
            "options": {
                "cwd": "${workspaceRoot}"
            },
            "isBuildCommand": true,
            "showOutput": "always",
            "problemMatcher": "$msCompile"
        },
        {
            "taskName": "Integration Tests",
            "command": "dotnet",
            "args": [
                "test",
                "IntegrationTests.csproj"
            ],
            "options": {
                "cwd": "${workspaceRoot}/Services/test/IntegrationTests"
            },
            "isTestCommand": true,
            "showOutput": "always",
            "problemMatcher": "$msCompile"
        },
        {
            "taskName": "Unit Tests",
            "command": "dotnet",
            "args": [
                "test",
                "UnitTests.csproj"
            ],
            "options": {
                "cwd": "${workspaceRoot}/Services/test/UnitTests"
            },
            "isTestCommand": true,
            "showOutput": "always",
            "problemMatcher": "$msCompile"
        },
        {
            "taskName": "TestAll",
            "dependsOn": [
                "Unit Tests",
                "Integration Tests"
            ]
        }
    ]
}

then you edit your shortcut to runTask

[{
    "key": "shift+cmd+t",
    "command": "workbench.action.tasks.runTask",
    "when": "editorTextFocus"
}]

and when you use the shortcut: image

the codeLens still does not work, use @gregg-miskelly tricks above to debug or use the old way Console.Write()

Jonathan34 avatar Mar 09 '17 16:03 Jonathan34

Thanks for all the suggestions.

I'm wrapping up a working intermediate solution based on the comments above.

Tested to be working with

  • Unit Test Framework: xUnit
  • VSCode: Insiders 1.11.0
  • dotnet CLI: 1.0.1
  • OS: macOS 10.12 (should work on Linux as well)

VSCode's launch.json file

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

From Terminal

cd /path/to/test/project/
dotnet test --no-build --filter "FullyQualifiedName=MyNamespace.MyTestsClass.MyTestMethod"

In MyNamespace/MyTestsClass.cs file

namespace MyNamespace
{
    public class MyTestsClass
    {
        [Fact]
        public void MyTestMethod()
        {
            while (!Debugger.IsAttached) Thread.Sleep(500);
            MyBuggyMethod(); // Put a breakpoint in this line
        }
    }
}

From VSCode's Debug View

Press the start debugging green arrow, or F5, just make sure the launch config mentioned above is the one that runs. Make sure you select the item that looks like the one mentioned in the screenshot bellow: vscode_debug_attach

After that, VSCode's debugger will break in the line mentioned in the C# code above.

Hope this helps.

ghost avatar Mar 09 '17 18:03 ghost