How to configure cargo test with custom arguments in VSCode - unit-testing

I have some tests which create/read/write/delete a file and I always use the same file name in each of them, therefore I need to run them sequentially to avoid simultaneous operations on the same file. In the command line I can just do it like this
cargo test -- --test-threads=1
however in the VSCode Run/Debug menu it doesn't seem to work. I'm using the autogenerated configuration with Rust Analyzer, plus these extra arguments for the sequential run.
This is my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'my-project'",
"cargo": {
"args": [
"build",
"--bin=my-project",
"--package=my-project"
],
"filter": {
"name": "my-project",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'my-project'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin my-project",
"--package my-project",
"--", // here I've added the arguments
"--test-threads=1", // here I've added the arguments
],
"filter": {
"name": "my-project",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
And this is the output I get when running the second command (the one which calls cargo test):
Running `cargo test --no-run --bin my-project --package my-project --message-format=json -- --test-threads=1`...
error: Found argument '--bin my-project' which wasn't expected, or isn't valid in this context
Did you mean '--bin'?
If you tried to supply `--bin my-project` as a value rather than a flag, use `-- --bin my-project`
USAGE:
cargo.exe test --no-run --bin [<NAME>]
For more information try --help

VSCode uses a two steps process:
it calls cargo test --no-run to compile the test executable and
it calls the test executable directly.
You should put --test-threads=1 in the last args array:
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'my-project'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=my-project",
"--package=my-project",
],
"filter": {
"name": "my-project",
"kind": "bin"
}
},
"args": [ "--test-threads=1" ],
"cwd": "${workspaceFolder}"
}

Related

how can I use vscode on windows to remote debug launch a linux based project writen by c++ and built by makefile?

Background:
I am using vscode on windows to remote debug a linux based server project writen by c++ and built by makefile;
the Binary executable files made by the makefile could be launched by shell or scripts:
./executable_files projects.cfg, and the projects.cfg is a file to tell the project some parameters like port, time_out etc.
task.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "clean",
"type": "shell",
"command": "make",
"args": [
"clean"
],
"problemMatcher": [],
"group": "build"
},
{
"label": "make",
"type": "shell",
"command": "make",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/executable_files",
"args": [
"-c",
"${workspaceFolder}/projects.cfg"
],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
I have already set a breakpoints at the entry of the main function
Problems:
when I hit the F5 to debug this projects, it can not even go into the main function, and the debug function do not work to debug by step
enter image description here
Help:
so how can I fix this probelm?

Build and Run current active file in visual studio code for C++

I am new to cpp programming, and I am using Visual Studio Code as my IDE/editor. I created a custom build task which builds the current active file (file open in editor that I am working on). Below is my taks.json.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-10 build active file",
"command": "/usr/local/bin/g++-10",
"args": [
//"-g", // include to add debugging support
"-O2",
"${file}",
"-o",
//"${fileDirname}/${fileBasenameNoExtension}"
"build/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/local/bin/g++-10"
},
{
"type": "cppbuild",
"label": "C/C++: g++-10 run active file",
"command": "${workspaceFolder}/build/${fileBasenameNoExtension}",
"args": [],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "none",
"detail": "compiler: /usr/local/bin/g++-10"
}
]
}
and the launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++-10 - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "build/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
// "preLaunchTask": "C/C++: g++-10 build active file"
}
]
}
I am able to run the build task to generate cpp object file. But how do I run the file just built from within IDE. I essentially just want to emulate build and run for quickly checking programs. I tried creating a new task (the second item in tasks.json's "tasks"), but when run it does not works. I tried finding online but to no avail.
Currently I build the file using this tasks and then switch to terminal and run the file. This workflow is ok, but had this been achieved, I would have been able to do build and run as supported in common IDEs. Is there a way to achieve this?
Thanks!
Attaching screen snips for F5 debugging issue.
To read from standard input using a debugging session, standard input must be redirected from a text file.
To redirect standard input, you need a command like this
program_name < input.txt
So you have to add these commands to you launch
"args": [
"<",
"input.txt"
],
In launch.json change
"externalConsole": false,
to
"externalConsole": true,

Cannot debug C++ in visual studio code on Mac. Launch.json does not execute program in terminal

I am new to Visual Studio Code. I am trying to debug a simple C++ code.
I edited my launch.json to be able to debug the app like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/Calculator",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
When I start debugging, the terminal is opened in the correct folder but the program is not executed. So the Visual Code does not stop in the breakpoints I want to check in the program.
In my task.json I have the following code:
{
"version": "2.0.0",
"tasks": [
{
"label": "Echo vars",
"command": "echo",
"args": [
"${env:USERNAME}",
"workspaceFolder = ${workspaceFolder}"
],
"type": "shell",
"problemMatcher": []
},
{
"label": "build",
"type": "shell",
"command": "g++ -g Calculator.cpp -o Calculator",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
}
Can anybody help me on this?
Thanks in advance
As mentioned in the comments: if your Calculator.cpp is not compiled with debug symbols ie. g++ called without -g flag, you will not be able to debug it.
Hence, add to your launch.json a prelaunchTask entry which will make sure your build task which is compiling your source with debug symbols is always executed prior to launching the debugger.
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "build"
edit your launch.json to add debugger log output to your project:
"logging": { "engineLogging": true, "trace": false, "traceResponse": false }

VsCode while running c++ program

This is my launch.json file of VsCode :
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x86",
"program": "${workspaceRoot}\\${fileBasename}.exe",
"miDebuggerPath":"C:\\mingw-w64\\bin\\gdb.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"externalConsole": true,
"preLaunchTask": "g++"  
}
]
}
And this one if my tasks.json file :
{
"version": "2.0.0",
"tasks": [
{
"label": "<TASK_NAME>",
"type": "shell",
"command": "make",
// use options.cwd property if the Makefile is not in the project root ${workspaceRoot} dir
"options": {
"cwd": "${workspaceRoot}/<DIR_WITH_MAKEFILE>"
},
// start the build without prompting for task selection, use "group": "build" otherwise
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
// arg passing example: in this case is executed make QUIET=0
"args": ["QUIET=0"],
// Use the standard less compilation problem matcher.
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["absolute"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]}
They are worked before and today not. When I run task terminal shows this:
and also it is building :
and then when i debug (F5) program it is shows this :
Please help me how I run c++ program. Thanks for answers !
You have configured a pre launch task called g++ but have no task of that name. You need to change the name of your build task to g++.
You just need to change the value of label to match the name of the task from tasks.json

Debugging Go tests in Visual Studio Code

On my Windows machine, I have Visual Studio Code installed. To run tests manually, I go in console to projects folder and enter
go test main_test.go
It works perfectly.
But I have a situation in which I need to debug my test to understand what's going on.
For this I open launch.json and add a configuration
{
"name": "Tests",
"type": "go",
"request": "launch",
"mode": "test",
"remotePath": "",
"port": 2346,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"env": {},
"args": [
"main_test.go"
],
"showLog": true
}
After I press F5 I have
2017/03/29 13:28:11 server.go:73: Using API v1
2017/03/29 13:28:11 debugger.go:68: launching process with args: [./debug.test main_test.go main_go]
not an executable file
Process exiting with code: 1
Any ideas why this error occurs and what executable it's looking for?
To launch debugger for test I added one more configuration for launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Code",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"env": {},
"args": [],
"showLog": true
},
{
"name": "Test Current File",
"type": "go",
"request": "launch",
"mode": "test",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${file}",
"env": {},
"args": [],
"showLog": true
}
]
}
Also this configuration does not support tags. All tags in test files have to be disabled
// +build unit
...
For the mode, you can select auto which would choose either debug or test depending on active editor window.
All options for mode are auto, debug, test, exec, replay, core.
The resulting launch.json would look like:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${file}"
}
]
}
In my case it was not working because I named my file main_tests.go instead of main_test.go