Debugging Go tests in Visual Studio Code - unit-testing

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

Related

How do I reach a containerized django web app running in visual studio code debug mode?

After some path changes due to being a git submodule, I was able to configure a launch.json and tasks.json to get my application running in debug mode. Here are the tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "signmeasures:latest",
"dockerfile": "${workspaceFolder}/signmeasures-frontend/Dockerfile",
"context": "${workspaceFolder}",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": [
"docker-build"
],
"python": {
"args": [
"runserver",
"0.0.0.0:8000",
"--nothreading",
"--noreload"
],
"file": "signmeasures-frontend/signmeasures/manage.py"
}
}
]
}
and here is the launch.json
{
"configurations": [
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}/signmeasures-frontend",
"remoteRoot": "/app"
}
],
"projectType": "django",
"port": 5678,
"host": "localhost"
},
}
]
}
I'm not too familiar with setting up the debug container or docker in general, I'm still trying to learn. When I hit the debug button on the debug tab, it builds a container and then in the debug console I see this
but when I try to go to localhost:8000 it does not load the web app. It shows this:
I'm not sure what I need to change to be able to reach it and set breakpoints to debug this application. What should I do to be able to reach it and debug?

How to configure cargo test with custom arguments in VSCode

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

Configure vscode to run tests with AWS profile

I want to run tests against my AWS environment using a run configuration in vscode.
The tests need access to the AWS account to retrieve information like the name of a bucket, a user pool id ..
I have my credentials configured in ~/.aws/credentials under [dev-user] like:
[dev-user]
aws_access_key_id=*************
aws_secret_access_key=***************************************
I can run this from the command line with export AWS_PROFILE=dev-user and then npm run test.
How do I create a launch config in vscode that does the same? Currently it is not working. I've tried the following: adding env variables or a prelaunch task. But neither of them work:
Env vars:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"name": "vscode-jest-tests",
"request": "launch",
"args": ["--runInBand"],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"internalConsoleOptions": "openOnSessionStart",
"disableOptimisticBPs": true,
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"env": { "AWS_PROFILE": "dev-user"}
}
]
}
Pre launch task:
launch.json
{
"version": "2.0.0",
"configurations": [
{
"type": "node",
"name": "vscode-jest-tests",
"request": "launch",
"args": ["--runInBand"],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"preLaunchTask": "setProfile",
"internalConsoleOptions": "openOnSessionStart",
"disableOptimisticBPs": true,
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "setProfile",
"command": "export AWS_PROFILE=dev-user",
"args": ["test"],
"type": "shell"
}
]
}
None of the above gives me access to the AWS account from my tests.
How do I need to configure launch.json and / or tasks.json to get access to the AWS account in my tests?
It turns out that adding "env": { "AWS_PROFILE": "dev-user"} to your configuration in launch.json does work.
So this is correct:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"name": "vscode-jest-tests",
"request": "launch",
"args": ["--runInBand"],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"internalConsoleOptions": "openOnSessionStart",
"disableOptimisticBPs": true,
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"env": { "AWS_PROFILE": "dev-user"}
}
]
}
I don't know if it will help anyone, but for me I had a key and password that should be sent in the request, I downloaded aws plugin and it didn't work, searching a lot I managed to solve it, you will have to post vscode and edit the launch file, the vscode has "env" property for variables .... I did it like this:
....
env": {
"AWS_DEFAULT_REGION":"xxxxxx",
"AWS_ACCESS_KEY_ID":"xxxxxxxxxxxx",
"AWS_SECRET_ACCESS_KEY":"xxxxxxxxxxxxxxxxxx"
}
...

how do you launch oclif tests with debugging using vscode?

What does the launch.json look like to run oclif project tests in debug mode? I can't seem to get it so that I set breakpoints and step in.
This launch.json config works for me:
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"runtimeExecutable": "${workspaceFolder}/node_modules/ts-mocha/bin/ts-mocha",
"args": [
"${workspaceFolder}/test/**/*.ts"
],
"protocol": "inspector"
},
I benefitted from the response provided here:
https://github.com/oclif/oclif/issues/135#issuecomment-403622999
I've tested it with typescript, and program works great for debugging.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/bin/run",
"args": [
"--name",
"Callum",
"--force",
"argument"
]
}
]
}

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