In creating new Singer Taps using the Meltano SDK, it's not always clear how to setup testing in VS Code.
What's the best way to get the VS Code test features working?
First Method: Unit testing with VS Code "Tests" pane
This method makes unit tests (pytest tests) easy to run in the VS Code "Tests" pane.
Prereqs:
Python is installed on your machine.
The Python VS Code extension is installed.
You've run poetry install at least once.
You've set the Python interpreter to the Poetry environment via: "Command Palette" > "Python: Select Interpreter" and then select the matching Poetry environment.
Once the above are complete, the "tests" pane should populate with the list of unit tests and you'll have an option in the VS Code GUI to "Run" or "Debug" each test.
Second Method: Integration test by actually invoking the tap
This method actually runs your tap and sends the output to target-json or a similar sample target.
Prereqs:
Add a main to the bottom of your tap.py to make it invocable:
if __name__ == "__main__":
TapGoogleAnalytics.cli()
Install target-jsonl via pipx install target-jsonl or similar.
Replace tap_foobar with the actual name of your tap's library and then paste this into VS Code's launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${workspaceRoot}/tap_foobar/tap.py",
"console": "integratedTerminal",
"args": ["--config", ".secrets/config.json", "|", "target-jsonl"],
"env": { "PYTHONPATH": "${workspaceRoot}"}
}
]
}
Related
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.
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...
Hello I'm working with pipenv which have a differetn path for every developer, but I want to have the same launch setting for all. The problem is that the program is not the python is a custom executable with is under the virtual environment bin folder.
Here is my current launch.json
{
"name": "Python: TEST",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"console": "integratedTerminal",
"program": "${env:HOME}/.local/share/virtualenvs/venv-PbRe8Lzd/bin/<program>",
"args": [
...
],
"cwd": "${workspaceRoot}",
}
And for me works OK becaouse my is under "venv-PbRe8Lzd/bin/" but for the other developers that have different venv folder no. Any Idea on how to do this generic for all?
I think better not to sync .vscode folder at all. But anyway it's possible to put interpreter path into another file ".vscode/settings.json".
Delete "program" from launch.json.
Install the Python extension for Visual Studio Code https://github.com/Microsoft/vscode-python (probably it's already installed)
Press Ctrl+Shift+P
Search "Python: Select Interpreter"
Select pipenv interpreter path
or
Create/modify file ".vscode/settings.json"
{
"python.pythonPath": "${env:HOME}/.local/share/virtualenvs/venv-PbRe8Lzd/bin/python"
}
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',
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",