gpt4 book ai didi

unit-testing - 如何在 VS Code 中调试 dotnet 测试?

转载 作者:行者123 更新时间:2023-12-04 04:24:09 25 4
gpt4 key购买 nike

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将指示如何做到这一点,但它没有。

最佳答案

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

Test Method Example

我最终做的是这样的:

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


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

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

关于unit-testing - 如何在 VS Code 中调试 dotnet 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56290166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com