How to integrate VSCode with pytest ('test discovery fails')? - visual-studio-code

I'm unable to integrate my project's unit tests into VSCode. Test discovery fails because source files are not recognized by pytest.
(Just for clarification, this is a question about VSCode, not about pytest. I'm here because VSCode links its question section to SOF. Tests work fine if I run them manually.)
Tooling: pyenv, pipenv, pytest.
Project layout:
/src -> source code
/tests/unit -> test code
.vscode/settings.json:
{
"python.envFile": "${workspaceFolder}/.env",
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false
[...]
}
.env:
PYTHONPATH=./src
(Note: I don't think .env matters here, as per this comment, under Use of the PYTHONPATH variable)
Test discovery fails with:
tests/unit/test_revoke_default_sg.py:7: in <module>
from revokedefaultsg.app import RevokeDefaultSg, UnknownEventException
E ModuleNotFoundError: No module named 'revokedefaultsg'
=========================== short test summary info ============================
ERROR tests/unit/test_revoke_default_sg.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.08s ===============================
This is caused by ./src not being part of the python path, so tests cannot import source code.
I can fix this on CLI/makefile level by adding to PYTHONPATH:
PYTHONPATH=./src pytest
This works as expected.
What's the correct setup for VSCode?
Am I supposed to create a launch configuration for testing? I've started to look into this, but couldn't get it to work nicely with pipenv.

(OP here)
I think the key to integrate into VSCode creating a specific launch configuration for pytest (before, I only had the default launch configuration that's been created with the Python extension).
I now have two configurations (.vscode/launch.json):
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Run Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": {
"PYTHONPATH": "./src"
}
},
{
"name": "Python: pytest",
"type": "python",
"request": "launch",
"module": "pytest",
"cwd": "${workspaceRoot}",
"env": {
"PYTHONPATH": "./src"
},
"envFile": "${workspaceRoot}/.env",
"console": "integratedTerminal",
}
]
}
(project on GitHub)
Running this configuration after launching VSCode discovers tests correctly, without raising errors. However, I have not tried it out in different projects yet.
Also, maybe I didn't find the right section of the VSCode documentation, but this feels heavily under-documented. I ended up searching GitHub for gists with launch configs and copied together whatever seemed vaguely helpful...

Related

VS Code / Metals: How to debug Scala sbt project using remote Debugger?

I know the question has been asked before in some variation here, but I simply can not figure out how to debug my scala sbt project in VS Code (WSL). Can someone provide some detailed instruction on how to do that ? What does it mean to debug using (java) remote Debugger ?
When I run Metals doctor, it says that it can not even detect the build tool. From the terminal however I am able to run/compile the code just fine (F.e. saying sbt "testOnly *IngestionTaskTest").
I addded the following launch.json file after entering >Debug: Select and Start Debugging in command Pallette:
"version": "0.2.0",
"configurations": [
{
"type": "scala",
"request": "launch",
"name": "somename",
"mainClass": "somepackage.SparkMain",
"args": [],
"jvmOptions": []
}
But I receive Class: sompackage.SparkMain not found
Ok I found out how to do it. These are the exact steps:
1.) Configure launch.json manually. First go to command pallette and say >Debug: Select and Start Debugging in command Pallette
2.) Then edit launch.json to look like this:
"version": "0.2.0",
"configurations": [
{
"type": "java",
"request": "attach",
"name": "gqd debugger",
"hostName": "localhost",
"port": "8000",
}
]
}
4.) Enable sbt debugger in build.sbt by adding the following line: Test / javaOptions += "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000"
5.) In the bash terminal say sbt Test/testOnly theTest
6.) Wait a few seconds until project & settings are loaded. Then press putton F5
(Tests refers to my unit tests). Hope it helps.

Starlark debugger for Bazel in Visual Studio Code

i am new to Visual studio Code. I followed this tutorial to set up a Bazel build configuration in Visual studio code (I use Windows 10).
I created a simple task.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build Example (Debug)",
"type": "shell",
"command": "bazel build //main:hello-world -c dbg",
"windows": {
"command": "bazel build //main:hello-world --experimental_enable_runfiles -c dbg"
},
"osx": {
"command": "bazel build //main:hello-world -c dbg --spawn_strategy=standalone",
},
"group": {
"kind": "build",
"isDefault": true
},
}
]
}
and launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Example",
"type": "cppvsdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"preLaunchTask": "Build Example (Debug)",
"cwd": "${workspaceFolder}/bazel-out/x64_windows-dbg/bin/example.exe.runfiles/__main__/",
"program": "${workspaceFolder}/bazel-out/x64_windows-dbg/bin/main/hello-world.exe",
"externalConsole": false,
"windows": {
"type": "cppdbg",
"type": "cppvsdbg",
"cwd": "${workspaceFolder}/bazel-out/x64_windows-dbg/bin",
"program": "${workspaceFolder}/bazel-out/x64_windows-dbg/bin/main/hello-world.exe",
},
},
}
]
}
In this way with run-> start debugging, I am able to debug and stop to breakpoins within the .cpp code of my project.
However, I read here, that is also possible to use the Starlark debugger to debug the .bzl files and Starlark rules.
According to the instructions in the same page I should be able to do this "by right-clicking a build target in the Bazel Build Targets view and selecting "Build Target with Starlark Debugger"". Unfortunately i can't see this option in my Bazel Build Targets view windows:
The Bazel Build Targets view is empty. and if i right click i can't see the "Build Target with Starlark Debugger" option. According to this link i should be able to see my targets listed below Bazel Build Targets view. I guess i am missing something in the configuration of the project or maybe some starlack extension?
Thanks for any help.
Bazel build targets does not work for me on windows either. When I run it, the extension outputs some error about bazel query. I haven't been on windows recently enough to remember the exact message, but I believe it is something along the lines of what is documented in this open issue.
Looks like there is a pull request open to solve it but no one has reviewed it yet. Best bet might be to weigh in over on either one of those after checking your extension output error log to see if it matches what is documented in there. Alternatively, you can check out the Clion with Bazel plugin, I have not tried that on windows yet though.

VSCode Junit output not displaying

I'm running Ubuntu 18.04 and just installed VSCode 1.35.1. I installed the Java packages, created a project from maven, and then tried to run the unit tests. The tests run, but none of the print messages are showing anywhere.
My launch.json looks like this,
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug (Launch) - Current File",
"request": "launch",
"mainClass": "${file}",
"console": "integratedTerminal"
},
{
"type": "java",
"name": "Debug (Launch)-App<ttt>",
"request": "launch",
"mainClass": "exercises.App",
"projectName": "ttt",
"console": "integratedTerminal"
}
]
}
When I run with F5 or Ctrl-F5, it always runs the main App. When I go to my test file and click on the Run|Debug links, the tests run and generate reports, but I don't see any of the print messages I put in.
What do I need to setup differently?
The Test Output is in the Java Test Runner Output.
You can highlight it with the "Java: Show Test Output" command in the F1 menu
With VSCode 1.64 (Jan. 2022), you won't have to look for that view anymore when launching a debug session.
It allows for VS Code to automatically switch to test explorer when users click 'run tests'.
See issue 140596 / issue 135999, solved by commit a47b55c
You now have the options:
NeverOpen = 'neverOpen',
OpenOnTestStart = 'openOnTestStart',
OpenOnTestFailure = 'openOnTestFailure',

Can not change debug environment automatically on visual studio code with config

For example, my root workstation directory is /home/chain/Project. And I have two separate projects which is python and website. My launch.json goes:
{
"version": "0.2.0",
"configurations":
[
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${workspaceRoot}/python_project_source/test.py",
"cwd": "${workspaceRoot}/python_project_source",
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
},
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "${workspaceRoot}/website/test.html",
"webRoot": "${workspaceRoot}/website"
}
]
}
As expected when I select test.py and press 'F5' it can switch to python debug environment, and when I select test.html the Chrome will be opened.
The fact is, VS code only remember the environment I debugged last time rather than change it automatically. So the only thing I do now is adding some comments to one environment (/* */) when I need to compile the other.:(
Is there something wrong in my launch.json?
Automatically switching the debug environment based on the file (or file-type) is not a current feature of Visual Studio Code, I believe.
You'll have to manually switch the launch configuration depending on the type of debugging task you want to perform.
And, of course, you could consider writing a feature request: https://github.com/Microsoft/vscode

Can Visual Studio Code be configured to launch electron

Since Visual Studio Code was created using Electron, I'm guessing that launch.json might be configured to properly launch an app using Electron. But I've not figured out how to do it yet.
Also since Electron is based on io.js, itself based on Node.js, I'm thinking maybe... it can be done, but haven't found the magic yet.
Tried something along these lines... snippet from launch.json:
"configurations": [
{
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Launch Electron",
// Type of configuration. Possible values: "node", "mono".
"type": "node",
// Workspace relative or absolute path to the program.
"program": "Y:\\dev\\electron\\electron.exe",
// Automatically stop program after launch.
"stopOnEntry": false,
// Command line arguments passed to the program.
"args": ["CrawlSpace_Electron\\"],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": ".",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": null,
// Environment variables passed to the program.
"env": { }
},
It does start Electron, but fails (window vanishes too fast to see exactly why).
Any thoughts?
If you specify electron.exe as the runtimeExecutable (as previously suggested) you can pass the main.js file as the program and it will work. Electron allows you to specify the directory OR the main.js file since that is pretty much what the package.json points to. Using the configuration below in my launch.json file, pressing F5 both launched Electron with my app and connected the debugger to the main process (eventually)...
{
"name": "Launch Electron",
"type": "node",
"program": "${workspaceRoot}/app/main.js", // ensure this is path to main.js file
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
// as you have noted, this is also important:
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
My main.js file is in the app folder I normally would pass to Electron.
Yes, it could. Not only could VSCode launch Electron, it could also debug it.
Using node you can debug Electron's Main process, but with Debugger for Chrome you can also debug Electron's Renderer process. I wrote a blog post on this topic: http://code.matsu.io/1.
The current highest upvoted answer is a bit outdated.
You should use electron instead of electron-prebuilt. See http://electron.atom.io/blog/2016/08/16/npm-install-electron
You should use node_modules/.bin/electron to launch electron
On Windows it's electron.cmd, not electron.exe.
Here are two pre-configured projects: https://github.com/octref/vscode-electron-debug.
One configured to run electron/electron-quick-start
One modified from electron/electron-quick-start to use ES6 & Babel & Webpack.
Here is the launch.json for the first project. To run the target "Debug Renderer Process", you need to install Debugger for Chrome. But "Debug Main Process" works fine on vanilla VSCode.
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Main Process",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
// Use the following for Windows
// "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd",
"program": "${workspaceRoot}/main.js"
},
{
"name": "Debug Renderer Process",
"type": "chrome",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
// Use the following for Windows
// "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd",
"runtimeArgs": [
"${workspaceRoot}/main.js",
"--remote-debugging-port=9222"
],
"webRoot": "${workspaceRoot}"
}
]
}
In theory the following should work:
Specify the electron.exe as the "runtimeExecutable" (since it replaces the node runtime). The electron program ("CrawlSpace_Electron\") becomes the "program". VSCode automatically passes a "--debug-brk" or "--debug" to electron.exe.
In practice VSCode does not yet support this setup because the preview version of VSCode tries to verify that the "program" attribute is a file that exists on disk. But for electron the "program" must be a directory.
I have created a bug on our side and will make sure it’s fixed with the next release.
I know this is just 1 link but it's the answer everyone needs...
https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes
Here are the attributes documented for launch.json. Unsure if the list is currently complete, but it should at least help...
On OSX the path to electron is
"runtimeExecutable": "${workspaceRoot}/node_modules/electron-prebuilt/dist/Electron.app/Contents/MacOS/Electron",