VS Code Pytest/Unittest debugger doesn't stop on breakpoints - unit-testing

I wrote unittest-based tests with pytest. according to this document:
https://docs.pytest.org/en/7.1.x/how-to/unittest.html
then I tried to run the tests by Python testing in Visual Studio Code according to this document:
https://code.visualstudio.com/docs/python/testing
but when I select the Debug Test icon next to that test in the Test Explorer. VS Code starts the debugger but it doesn't pause at the breakpoints. how can I make it to stop at the breakpoints?

According to what has been explained here, when we use "--cov" option for pytest, the VSCode doesn't stop at breakpoints. So the solution is to disable cov while debugging according to this in your VSCode launch.json add the followings:
// your other settings are here
{
"name": "Debug Unit Test",
"type": "python",
"request": "launch",
"justMyCode": true,
"program": "${file}",
"purpose": ["debug-test"],
"console": "integratedTerminal",
"env": {
"PYTEST_ADDOPTS": "--no-cov"
},
}

Related

How do I stop vs code to stop debugging internal cpp modules?

VScode screenshot
Vscode cpp debugger is taking me in to internal c++ modules like you can see in this picture
I just want it to debug my files
You will need to create new file launch.json first.
Now add
{
"name": "C++ Launch (Windows)",
"type": "cppvsdbg",
"justMyCode": true,
"request": "launch",
"program": "C:\\app1\\Debug\\app1.exe",
"symbolSearchPath": "C:\\Symbols;C:\\SymbolDir2",
"externalConsole": true,
"logging": {
"moduleLoad": false,
"trace": true
},
"visualizerFile": "${workspaceFolder}/my.natvis",
"showDisplayString": true
}
Check this first: https://code.visualstudio.com/docs/cpp/launch-json-reference
If above thing doesn't work, try using "Step Over" option when you hit internal C++ functions.
Check this: https://code.visualstudio.com/docs/editor/debugging#_debug-actions

The configuration option popup for debugging a c++ project in Visual Studio Code does not appear

so I want to debug my .cpp program file but when I click on the Run and Debug button and proceed to select my debugging environment (C++ (GDB/LLDB)), the popup to select the configuration option does not even appear at all and the debugging just doesn't start.
This is before I click the environment popup:
and this is after:
Any help I can get for this problem is greatly appreciated as I can't seem to find any solutions at all on the internet, thank you very much!
p.s I've already tried uninstalling VS Code completely off my laptop and resetted all the settings and it didn't work.
Are you sure you installed the C/C++ Studio Code extension? If you don't get the popup, try writing the json files manually.
Create a .vscode folder in your working directory. In there create a file launch.json in which you declare how you run the debugger
{
"configurations": [
{
"name": "Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}\\test.exe",
"stopAtEntry": true,
"cwd": "${workspaceRoot}",
"preLaunchTask": "Build Debug",
"miDebuggerPath": "c:\\mingw64\\bin\\gdb.exe"
}
]
}
This runs the gdb debugger that came with MinGW. You need to provide the path to your debugger at miDebuggerPath.
This debugs an executable test.exe that needs to be created in debug mode. For this you have the preLaunchTask option. In .vscode create a file tasks.json in which you describe the debug task as
{
"tasks": [
{
"type": "cppbuild",
"label": "Build Debug",
"command": "g++",
"args": [
"${workspaceRoot}\\test.cpp",
"-g",
"-o",
"${workspaceRoot}\\test.exe"
],
}
],
"version": "2.0.0"
}
This uses the gcc or g++ compiler that also comes with MinGW to compile a single source file test.cpp into the test.exe binary. You can select the debug launch configuration in the lower left corner of Studio Code and click Run.

How to read input when debugging in C++ in Visual Studio Code?

I'm using VSCode for debugging my CPP program in MacOSX.
I've 2 programs.
Program1
int main(){
string a;
a = "a";
a += 'b';
cout<<a<<endl;
return 0;
}
Program2
int main(){
string a;
cin>>a;
a += 'b'
cout<<a;
return 0;
}
In program1 I'm directly assigning the string a and when I debug the program in VSCode by first compiling it in terminal using :
g++ -g filename.cpp
and then selecting the Starting Debugging option in the Debugging menu. I'm able to see the state of the string a variable by moving forward in breakpoints.
The VARIABLES section shows the state of different variables and the CALL STACK show the stack frame.
But, for program2, when I go past the breakpoint of the cin>>a;, the contents of VARIABLES and of CALL STACK get cleared up.
Here are the contents of the launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
How can I get user-input and move forward to debug my code?
As stated in Here
if you enable "externalConsole":true in the launch.json then you will get a pop up console window that you can type in.
To debug with inputs, you can edit the arg section as shown below:
"program": "${workspaceFolder}/main",
"args": ["<", "input_file.in"]
The example above should be the same as: ./main < input_file.in
I hope this helps anyone who comes around here:
by (1) setting "externalConsole" to true and (2) checking (enabling) "Run In Terminal" in Code-Runner Extension configuration, you can plug-in your input to your code by typing the input on the external console, that would pop up when you run your code.
Install extension CodeLLDB
Add new configuration CodeLLDB: Launch
Set program property as "program": "${workspaceFolder}/${fileBasenameNoExtension}"
(optional) Rebuild code
Chose created Launch config in VS Debug tab. And start it!
Profit!
Video manual
simply:-
step1. click on small gear-icon of debugger window.
step2. make "true" to this ["externalConsole": false,] in launch.json file.
step3. and just restart your debugger.
I also encountered the same problem, my solution was to replace cygwin's gdb and g ++ with mingw64's.
In my case this was a two-step process.
Enable externalConsole: true, as described in other responses.
Let VS code control the terminal.
If the code you are debugging requires user input, set external Console to true. after entering input, avoid clicking "x" to close the external Console. Instead, click "-" to minimise the window. Then keep hitting f10 or f11 to continue debugging.

Breakpoints working in lldb but not VS Code

I have the following setup in my launch.json file to debug native code compiled from c++ using clang. I am able to use lldb on the command line and debug it, including setting breakpoints. But when I try setting a breakpoint using Visual Studio Code, I get the error message:
Unable to open 'foo.cc': no provider for .///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////tests/foo/foo.cc.
{
"name": "(lldb) test",
"type": "cppdbg",
"request": "launch",
"program": "./prog-test",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"externalConsole": false,
"MIMode": "lldb"
}
On the lldb prompt, I simply run breakpoint set -f foo.cc -n 18 and it works.
I am guessing this is something about the path to the file in VS Code. But changing the working directory around did not help. It is currently set to the directory from which lldb on the commandline works.
Any ideas would be appreciated!

VSCode: Debugger for C++ does not launch nor attach properly

I am using:
Ubuntu 16.04 (I know it is not supported officially)
Latest VSCode version
Latest "C/C++ for VSCode" version
My project is a C++ project and the structure looks like this:
/home/lvier/mainProject/fooProject (source code)
/home/lvier/mainProject/build/fooProject (binaries)
I am working in the sub-project "/home/lvier/mainProject/fooProject" and in "/home/lvier/mainProject/build/fooProject" there are many sub programs (lets say "foo", "foo_sub1", "foo_sub2" ...).
My goal:
I want to start the program "foo" (which starts all other foo_sub-programs) and then, I want to debug a certain sub program (let's say "foo_sub1") by attaching to it. I am also fine with starting and debugging in the same time as long as I can debug the sub-program "foo_sub1". The main project itself does not contain any executables.
Some months ago, debugging was working with "attach". For me it is not working anymore (because of VSCode updates and/or C/C++ extension updates). Here are my problems:
Assume that "foo" is running.
When using the "C++ Attach"-config and setting "request": "attach" (which is getting highlighted as "not an accepted value"), it will ask for the property "processId" if it is not set. If I set "processId", the error "Attach not supported" pops up.
If I use the "C++ Attach"-config with "request": "launch" (in the beginning this was autogenerated by the C/C++-extension), then the program finds the process id, tries to attach but then aborts with the message "Unable to start debugging. Commands are only accepted when the process is stopped." - what a surprise.
Assume that "foo" is not running yet.
When using the "C++ Launch"-config, the program starts but no UI elements appear. Instead, it starts with a new terminal popping up which says "warning gdb failed to set controlling terminal operation not permitted" for a brief moment and in the internal console of VSCode, it states that it stops at a certain line of code (a breakpoint not defined by me) and prints
"Stopped due to shared library event (no libraries added or removed)
Loaded '/lib64/ld-linux-x86-64.so.2'. Symbols loaded.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1"."
From there I can't continue further and I have to manually terminate the program.
When I use GDB without VSCode, i.e. just by native terminal, my program is starting properly but with VSCode, there seem to be some issues currently.
This is my current, autogenerated config where I only edited the "cwd" and "program" paths (assume that the environment variable "${env.build_foo}" is set to "/home/lvier/mainProject/build/fooProject"):
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x64",
"program": "${env.build_foo}/foo",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"linux": {
"MIMode": "gdb"
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb"
}
},
{
"name": "C++ Attach",
"type": "cppdbg",
"targetArchitecture": "x64",
"request": "launch", // <-- "attach" is not allowed (anymore)! :(
"program": "${env.build_foo}/foo_sub1",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"processId": "${command.pickProcess}",
"externalConsole": false,
"linux": {
"MIMode": "gdb"
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb"
}
}
]
}
So far I am really frustrated and still don't want to give up on VSCode (I don't want to switch to another IDE yet). I have searched a lot for other issues and also tried to set some other config-properties but none of it helped.
Does anyone else also have such issues with the debugger or is this a general problem with the extension (... and Ubuntu 16.04)?
I am happy for any help or convenient workaround. Thanks in advance!
Note:
I have also created a thread on the related github page (see https://github.com/Microsoft/vscppsamples/issues/115)
Update 07/26/2016:
It seems like there is a bug with the C/C++ extension (see comments in the github link above). It is still being investigated though.
Here solution:
...Debugging works for me now after I removed the GCC -s flag (strip symbol table and relocation information) from the linker settings...
Got it from last answer of the next link (thanks HorstBaerbel):
https://github.com/Microsoft/vscode-cpptools/issues/115#issuecomment-299334301