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会指示如何执行此操作,但事实并非如此。
答案 0 :(得分:0)
这可能不能解决您完全传递参数的问题,但可以解决将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")
}
所以现在我既可以通过命令行使用特定的runsettings文件运行代码,也可以通过运行脚本设置环境变量来进行调试。
答案 1 :(得分:0)
也许这是您正在寻找的更多东西: How does one debug an MSTest in VSCode?
(也请检查注释)。 因此,通过使用“ VSTEST_HOST_DEBUG”:“ 1”,您应该能够附加一个调试过程,类似于在Java中的调试过程。