【问题标题】:How to debug dotnet test in VS Code?如何在 VS Code 中调试 dotnet 测试?
【发布时间】:2019-10-10 21:50:54
【问题描述】:

This article 描述了设置 VS Code 设置以将调试目标指向单元测试项目的构建输出。因此,我将我的设置如下:

{
    "explorer.confirmDragAndDrop": false,
    "git.allowForcePush": true,
    "git.autofetch": true,
    "window.zoomLevel": 0,
    "csharp.unitTestDebuggingOptions": {
        "sourceFileMap": {
            "C:\\git\\MsTester\\bin\\Debug\\netcoreapp2.1": "C:\\git\\MsTester\\bin\\Debug\\netcoreapp2.1"
        }
    },
    "files.autoSave": "afterDelay",
    "files.exclude": {
        "**/bin": true,
        "**/node_modules": true,
        "**/obj": true
    },
    "csharpfixformat.style.spaces.insideEmptyBraces": false,
    "csharpfixformat.style.braces.allowInlines": false,
    "csharpfixformat.style.spaces.beforeParenthesis": false,
    "csharpfixformat.style.spaces.afterParenthesis": false,
    "csharp.format.enable": false,
    "extensions.ignoreRecommendations": true
}

但是,我不确定如何设置 launch.json 以启动 dotnet test 以便它可以附加调试器。

这是我目前得到的:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "MsTester",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/MsTester/bin/Debug/netcoreapp2.1/MsTester.dll",
            "windows": {
                "args": [
                    "--filter",
                    "TestCategory=lbshell",
                    "--logger",
                    "trx",
                    "--results-directory",
                    ".\\TestResults",
                    "--settings",
                    ".\\Features\\runsettings.xml"
                ],
            },
            "cwd": "${workspaceFolder}/MsTester",
            "console": "internalConsole",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
        },
    ]
}

是否有一个选项告诉 VS Code 它需要执行 dotnet test 而不是 dotnet run

我希望this page 会指出如何做到这一点,但它没有。

【问题讨论】:

    标签: unit-testing vscode-settings vscode-debugger dotnet-test


    【解决方案1】:

    所有功劳归功于@Lance U. Matthews,因为他将此作为对问题答案的评论发布:How does one debug an MSTest in VSCode?。我将此作为正确答案重新发布,因为他已经一年没有这样做了。

    以下答案已在 VSCode v1.62.3 和 dotnet tools v5.0.403 上测试。

    假设您有一个结构如下的 .NET 解决方案:

    mysolution.sln
    tests\
      |---> tests.csproj
      |---> UnitTest1.cs
    .vscode\
      |---> launch.json
      |---> tasks.json
    

    将以下内容复制到tasks.json 文件中:

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            { 
                "label": ".NET Core Test with debugger", 
                "type": "process", 
                "isBackground": true, 
                "command": "dotnet", 
                "args": [ "test" ], 
                "options": 
                    { 
                        "cwd": "${workspaceFolder}/tests", 
                        "env": 
                        { 
                            "VSTEST_HOST_DEBUG": "1" 
                        }, 
                    }, 
                "group": "test", "presentation": 
                    { 
                        "echo": true,
                        "reveal": "always",
                        "focus": false,
                        "panel": "shared"
                    },
                "problemMatcher": [] 
            },
        ]
    }
    

    并将其放入launch.json 文件中:

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

    现在在UnitTest1.cs 文件中的任意位置设置断点并运行.NET Core Test with debugger 任务(终端->运行任务...)。 测试执行命令行工具应该会启动并且在你的终端底部应该是这样的: 您的 Process Id 肯定会有所不同,但这是预期的行为。

    现在你需要切换到“运行和调试”选项卡(ctrl+shift+d)并选择“.NET Core Attach”(它应该在 VSCode 的左上角)。 提供之前出现在终端中的进程 ID: 现在单击 F5 一次,您应该会到达一个断点。

    【讨论】:

      【解决方案2】:

      也许这更符合您的要求: How does one debug an MSTest in VSCode?

      (也请查看评论)。 因此,通过使用 "VSTEST_HOST_DEBUG": "1" 您应该能够附加一个调试进程,类似于在 Java 中的做法。

      【讨论】:

        【解决方案3】:

        这可能无法完全解决您传递参数的问题,但可能会解决将运行设置参数传递给正在调试的代码的问题。使用这种方法,我能够在运行调试器时将运行设置的内容传递给我的代码(即当我单击方法上方的“调试测试”时):

        我最终做的是:

        首先,我编写了一些代码,如果密钥在设置文件中不可用,则将从设置文件(通过TestContext)或用户环境中读取,即:

        
        [ClassInitialize]
        public static void TestClassInitialize(TestContext context)
        {
            v = Settings.GetSettingValueFromContextOrEnvironment("v", context);
        }
        
        

        这里是GetSettingValueFromContextOrEnvironment的实现:

        /// <summary>
        /// Attempts to read a setting value from the context (populated through runsettings). If the
        /// key is not present in the context, the value will be read from the user environment variable
        /// instead.
        /// 
        /// This allows running the unit test code in VSCode debugger that currently doesn't seem to allow
        /// passing runsettings file to the DLL.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static String GetSettingValueFromContextOrEnvironment(string name, TestContext context){
            if (context.Properties.ContainsKey(name)){
                return context.Properties[name].ToString();
            } else {
                String envVar = Environment.GetEnvironmentVariable(name, System.EnvironmentVariableTarget.User);
        
                if (envVar == null){
                    throw new Exception(String.Format("Environment variable '{0}' was not available neither in the context file nor in the environment.", name));
                } else {
                    return envVar;
                }
            }
        }
        

        然后我编写了一个 Powershell,它将读取我的设置文件以解析参数并将它们设置为用户环境变量,如下所示:

        <# This script updates user environment variables with parameters stored in *.runsettings file that is provided to it as the only argument on the command line.
        
        Example usage:
            .\set_settings_env.ps1 local.runsettings
        #>
        
        param (
            [Parameter(Mandatory=$true)][string]$settings_file
        )
        
        
        [xml]$xml = Get-Content $settings_file
        foreach( $parameter in $xml.RunSettings.TestRunParameters.Parameter) 
        {
            write-host("Setting environment variable: " + $parameter.name)
            [Environment]::SetEnvironmentVariable($parameter.name, $parameter.value, "User")
        }
        

        所以现在我既可以使用特定的运行设置文件从命令行运行代码,也可以通过运行脚本来设置我的环境变量来进行调试。

        【讨论】:

        • 您是否在 Visual Studio Code 中使用 F5 启动程序?
        • 不,我使用出现在我的单元测试名称上方的“运行测试”选项(请参阅我的答案中的屏幕截图)
        • 所以,不是 VS Code?​​span>
        • @MattW,它在 VS 代码中。这是我在 mstest 项目的 VS Code 中看到的一个选项,这是一个更好的屏幕截图:imgur.com/a/rg8xx9e
        猜你喜欢
        • 2021-05-12
        • 2017-10-02
        • 2019-12-29
        • 2021-09-03
        • 1970-01-01
        • 1970-01-01
        • 2019-04-29
        • 2020-12-16
        相关资源
        最近更新 更多