13

本文介绍如何设置 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

我希望这个页面能说明如何做到这一点,但事实并非如此。

4

3 回答 3

7

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

测试方法示例

我最终做的是:

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")
}

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

于 2019-07-09T14:07:21.463 回答
1

所有功劳都归功于@Lance U. Matthews,因为他将此作为对问题答案的评论发布:如何在 VSCode 中调试 MSTest?. 我将其重新发布为正确答案,因为他已经一年没有这样做了。

以下答案已在 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 一次,您应该到达您的断点之一。

于 2021-12-09T01:45:02.973 回答
0

也许这更符合您的要求: 如何在 VSCode 中调试 MSTest?

(也检查评论)。因此,通过使用 "VSTEST_HOST_DEBUG": "1" 您应该能够附加一个调试过程,类似于在 Java 中可以做到这一点。

于 2020-08-20T08:26:35.000 回答