Auto run tests in visual studio code - visual-studio-code

How do I run the tests automatically in visual studio code. My project has testing setup, and the settings JSON/UI has settings related to auto running tests, but I don't see an option for the test to actually run automatically. I have found topics around this issue, but those seem to be out of date, or related to a (different) plugin.
settings

In complement to the answer provided by #DanCiborowski, I would use the extension RunItOn instead of the RunOnSave because it is possible to run internal VSCode commands. Like so, the output from pytest is correctly integrated in the VSCode IDE.
Once RunItOn is installed, you can configure it with the settings:
"runItOn": {
"commands": [
{
"match": "\\.py$",
"isAsync": true,
"isShellCommand": false,
"cmd": "testing.runAll"
},
],
},
I've prepared a step-by-step tutorial here:
https://cordmaur.medium.com/how-to-autorun-python-tests-in-vscode-step-by-step-cfb24c6fc4be

Since VSCode is a text editor rather than a fully-fledged IDE, tests are not a native feature. Testing frameworks also vary drastically between different programming languages or environments. I'd suggest looking into command-line tools such jest for JavaScript which you can run via VSCode's integrated terminal.
Another approach is to create a custom task to run your test. Find an example for jest here.

The Python support for VSCode is an extension, which does not fully support "all" the testing features. The "Testing > Auto Run" settings are currently missing. This issue is being tracked here.
https://github.com/microsoft/vscode-python/issues/19046
Until this feature is properly implemented, the best way to achieve this uses extensions such as "Run on Save", the most popular item in the marketplace.
https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave
"emeraldwalk.runonsave": {
"commands": [
{
"match": "**.py",
"isAsync": true,
"cmd": "pytest"
}
]
}
(disclosure, I work for MSFT and was investigating this issue prior to finding the question while doing my own research on alternatives)

Related

vscode debugger does not launch debug console

I am so frustrated that my vscode debugger does not work on the remote servers suddenly today.
Here is the situation.
When I use the starred interpreter by vscode (the recommended one), it works smoothly. However, when I want to use other interpreters, the ones stored in personal folder on the server, the debugger doesn't launch a debug console and dose not enter the file.
Vscode can find and recognize the interpreters and I can use those interpreters to run scripts but can NOT DEBUG.(on the remote server) However, it works fine on my local machine with multiple python interpreter.
Can anyone give me some help? I have tried uninstall vscode completely on my end but still not work. Thank you so much!!
The Python extension debugger in VSCode requires a minimum Python version to run properly. However, this is not documented at all in the changelog at https://github.com/microsoft/vscode-python/releases, but you have to find out for yourself. For example, at the time of writing, version 2022.12.0 doesn't harmonize with Python 3.6.8.
As a workaround you have 2 options
Upgrade your interpreter to a newer version
Downgrade the Python extension to an older version
For the latter, go to the extensions page and under the Uninstall button you will find Install another version.... This makes it easy to try out what works best for you.
Just in case stumbles upon this question and the accepted answer doesn't work for you:
Try downgrading the Python extension version in VSCode. That fixed the issue for me. I picked a random version from ~2 months ago (when I wasn't having the issue). You might need to try a few versions though.
This workaround might work for you: use the interpreter that works for you but in the launch.json configuration file add a
"python": "/path/to/the/python/you/want",
I share the same experience. It seems that vscode fails to start the remote debugger with the interpreter I want but succeeds with the system interpreter.
For example:
"configurations": [
{
"name": "config_name",
"type": "python",
"python": "/path/to/desired/python",
"request": "launch",
"program": "/path/to/python/script.py",
"console": "integratedTerminal",
},
]

Setting CMake compilation options in VS Code

I recently started using VS Code and I have a question. How do I set the values of the compilation options defined in CMakeList.txt?
For example if the following option is set in the CMakeList.txt:
option(BUILD_WITH_TESTS "Build with tests." OFF)
Then I can set this value when building using for example the CMake GUI:
selecting option value
Does a similar toolkit exist in VS Code? What are the ways to set value BUILD_WITH_TESTS when building a project in VS Code?
At the moment I'm using VS Code with the plugin CMake Tools 1.5.3. But I do not find such functionality as a CMake GUI there.
You have several choices, depending on your exact requirements.
The CMake Tools extensions is a good choice, but it doesn't come with a GUI. If you want to use cmake-gui, you could still use it in your vscode CMake build directory, by executing cmake-gui <path_to_build_folder> in the vscode terminal.
In case you need to run this command often, you could customize your vscode settings, for a better cmake-gui integration, by using the Command Runner extension, which allows to run custom shell commands. After installation add the following to your settings.json:
"command-runner.commands": {
"cmake-gui": "cmake-gui ${workspaceFolder}/build"
}
and, if you like, a key binding to keybindings.json
{
"key": "ctrl+alt+1",
"command": "command-runner.run",
"args": { "command": "cmake-gui" }
}
Personally I would do none of the above methods, but just use ccmake in the terminal.
All of the above describes how to change settings in the current existing build directory (which is usually created by CMake Tools on first launch). However, the settings will get lost if you switch to a different compiler or delete the CMake cache in any other way.
To persist your settings and configure CMake by default with your preferred configuration, you can add your CMake arguments to your vscode workspace settings (in ${workspaceFolder}/.vscode/settings.json), e.g.
{
"cmake.configureArgs": [
"-DBUILD_WITH_TESTS=ON"
]
}

How to configure VS Code to build and debug STM32 projects using cubeMX - Windows 10

I am new to the realm of STM32 programming and have been trying to find a suitable IDE for quite a while now. I know of all the other IDE's like Keil and IAR but the cost of buying them just to learn is far to steep for me at this point in time.
I have started using VS Code for a growing amount of my development work and I though it would be a good IDE to use for STM32 development. I have found many examples online over the past few days on how to configure the IDE to build STM32 projects but they all seem to be missing important information that I need to properly get the project to compile. It is rather frustrating,
I was wondering if there is anyone that can point me to a complete setup guide on how to set up VS code to work with cubeMX and the arm tool-chain, or if you are feeling really kind, send me a sample project that I can use as a base learn from.
Just some background information, I know how to use cubeMX to generate the base project as well as the associated makefile, I also have the latest GNU-Tools-Arm-Embedded installed.
Thank you in advance for your help
Install GNU Arm Embedded toolchain and add its bin folder to your PATH environment variable.
You will also need a make to execute your makefiles so download Make for Windows. Easiest way is to download the binaries and extract it somewhere on your system. Add it (C:\make-3.81-bin\bin) to your PATH as well.
Create an STM32CubeMX project and select Makefile as Toolchain/IDE.
At this point you will be able to build the generated project by simply using make in the project's root folder.
If you open the project in VS Code you can build using its terminal or you can create a VS Code task to execute the make command. You can bind your task to a hotkey as well to spare some time.
To debug, the easiest way is to install Cortex-Debug VS Code extension. Follow the instructions to configure your debug sessions.
A while ago I had the same question, but did not find anything that I really liked. So I created STM32 for VSCode, it is an extension for VSCode which works with STM32CubeMX generated files and sets up building and debugging for you.
There is a library of python scripts that does just this, it has been released recently with excellent documentation and after testing I can say it works as advertised.
VSCode STM32 IDE
The process is quite straight forward:
Export the files using STM32CubeMX
Cpen the VSCode folder and save it as workspace
Copy the scripts "ideScripts" directory to your project folder
Run update.py
Here is a video on how it works:
VSCode STM32 IDE - Getting Started
There is already very good answer by #Bence Kaulics, based on it add my recent findings.
make command somehow did not work for me in VS Code Terminal. To solve this I installed "Makefile Tools" extension from Microsoft.
instruction link does not work, therefore I add steps how to configure debugging for J-Link.
-> Install Cortex-Debug Extention. -> Download and install J-Link Software from Segger. -> Get SVD file if you want to see peripheral registers. -> Edit launch.json file (see code below). -> Set your executable, paths and device.
{
"version": "0.2.0",
"configurations": [
{
"cwd": "${workspaceRoot}",
"executable": "./build/STM32F103RBT6_Test1.elf",
"name": "Debug Microcontroller",
"request": "launch",
"type": "cortex-debug",
"servertype": "jlink",
"serverpath": "C:/Program Files/SEGGER/JLink/JLinkGDBServerCL.exe",
"armToolchainPath": "C:/Program Files (x86)/GNU Arm Embedded Toolchain/10 2021.10/bin",
"device": "STM32F103RB",
"interface": "swd",
//"serialNumber": "", // if Multiple Debuggers attached
"runToMain": true,
"svdFile": "${workspaceRoot}/device/STM32F103xx.svd",
}
]
}

Can VSCode (Visual Studio Code) be used to run elm-make?

I am new to VSCode.
I am new to ELM.
I am perfectly capable of using VIM and command line tools to create an ELM Project, but I want to utilize an IDE. I have chosen VSCode on advice from the internet since it seems to pick up some nice pieces of VIM.
So now I have a few .elm files.
Main.elm
View.elm
Model.elm
I want to run elm-make on Model.elm to make sure it has no errors.
Then I want to run elm-make on Main.elm to create an index.html so I can view my project.
I think this is a pretty simple question for people familiar with how to customize VSCode, but as I stated previously, I am new to VSCode.
Try setting up a task for elm-make:
Create a ./vscode/tasks.json with the contents:
{
"version": "0.1.0",
"tasks": [
{
"taskName": "elm make",
"isBuildCommand": true,
"command": "elm-make",
"args": ["./main.elm"],
"isShellCommand": true
}
]
}
You can then use the build command to run the task, or run the task individually.
You may also want to look into the elm extension: https://marketplace.visualstudio.com/items?itemName=sbrink.elm

Does Visual Studio Code work with PowerShell in the Command Palette?

With Visual Studio Code (not the traditional Visual Studio IDEs), can you run PowerShell in the Command Palette? I ask because I commonly use it in the full IDE.
I have not seen PowerShell mentioned in the documentation, other than for basic syntax highlighting. I have tried it with no success. Is it unsupported, or is it an optional feature I can configure somehow?
Note: to those voting up the PowerGUI answer, it is not correct as it references the wrong edition of Visual Studio for this question. It is helpful if you are using the full IDE, but not the new editor named: Code.
This was the first problem I wanted to solve in VSCode. I did not find a way to
type PowerShell commands but I have found a way to create and run PowerShell tasks.
In the command palette type and choose Tasks: Configure Task Runner. It will
show you a json file configured for the default task runner.
I replaced the content with this
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"-NoProfile",
"Invoke-Build.ps1"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
As a result in the command palette I can choose and run my predefined tasks (Build, Test, and combined Build, Test). I can add other tasks and probably bind them to some hotkeys. This is not exactly what I would like to have in VSCode for PowerShell but for the preview it is at least something.
P.S. This just my first experiment that somewhat worked. It is not perfect, more likely. There are many configuration parameters for this json file that I have not tried yet.
With version 0.10.1 of Visual Studio Code, you can run and debug PowerShell. Here is a sample launch.json that I use:
{
"version": "0.2.0",
"configurations": [
{
"name": "PowerShell",
"type": "PowerShell",
"program": "MyScript.ps1",
"args": "-Verbose"
}
]
}
Unfortunately, I cannot get the args to work (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/24). Another problem I have is that Read-Host does not work with VS Code (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/29). Definitely some rough edges.
Here's how you can configure the Powershell task to execute the currently opened .ps1 file without any Invoke-Build dependency:
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"${file}"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
Note: This is just a slight modification of Roman's answer (My edit to his answer was rejected).
Open the Debug view, in the View Bar select Debug from the View menu or press Ctrl + Shift + D.
In the Launch Configuration dropdown (shown in the following screenshot), select Add Configuration...
The launch.json configuration will open in the editor, type PowerShell and select the debug configurations you want, as shown in the following screenshot.
Save the launch.json file and select the debug configuration you want from the Launch Configuration dropdown:
Now you can debug PowerShell scripts through VSCode.
The Scripting Guys wrote a comprehensive 2 part blog post on this topic, like everything else they write, it's well worth a read if you're new to VSCode and PowerShell.
https://blogs.technet.microsoft.com/heyscriptingguy/2017/02/06/debugging-powershell-script-in-visual-studio-code-part-1/
https://blogs.technet.microsoft.com/heyscriptingguy/2017/02/13/debugging-powershell-script-in-visual-studio-code-part-2/
EDIT: I am aware that this question is several years old, but I came across it before I found any up to date information on how to set up PowerShell debugging in VSCode
As of version 0.10.3 there is now a PowerShell extension written by Microsoft that will enable writing PowerShell with IntelliSense and highlighting in Visual Studio Code
http://blogs.msdn.com/b/powershell/archive/2015/11/17/announcing-windows-powershell-for-visual-studio-code-and-more.aspx
At least in V1.4 (July 2016) this has been made a lot simpler.
The relevant documentation can be found here:
https://code.visualstudio.com/docs/editor/integrated-terminal
Essentially all you're doing is adding an entry to the settings.json file for User Settings.
Adding the following value:
// 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows":"C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe"
IMG: Shows the User Settings settings.json file and the successful Integrated Console showing the PowerShell prompt:
An "Integrated Terminal" is now integrated into Visual Studio Code. This is a PowerShell Terminal in Windows. (Likely BASH in Linux / MacOS). The terminal appears in a panel at the bottom of the code editor, not in the Command Pallete.
Keyboard Shortcut to open Terminal: CTRL + `
Sources:
https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf
https://code.visualstudio.com/docs/getstarted/keybindings
I confirmed in version 1.19.2. Not sure when the feature was first integrated.
There is a much easier way to run PowerShell, no configuration needed:
Install the Code Runner Extension
Open the PowerShell code file in Text Editor, then use shortcut Ctrl+Alt+N, or press F1 and then select/type Run Code, or right click the Text Editor and then click Run Code in context menu, the code will run and the output will be shown in the Output Window.
Besides, you could select part of the PowerShell code and run the code snippet. Very convenient!
PowerGUI tool provides a nice interface to Visual Studio. The goal of this extension is to bring PowerShell development into Visual Studio.
Here is how it looks -
Along with IntelliSense, the PowerGUI Visual Studio Extension provides the following features and more to make it easier to work with PowerShell.
PowerShell file and project types: You can create/edit PowerShell code files and assemble them into projects with more than one file.
PowerShell code snippets: The code snippet feature can be used for PowerShell code.
PowerShell console window: This feature provides the PowerShell console environment within Visual Studio IDE. This allows you to run commands or view output of scripts. Figure B shows the console window opened in the IDE.
PowerShell debugging: This feature is why I installed the extension; it provides a way to debug scripts within Visual Studio. It's a straight-forward way to locate syntax or logical problems in a script.
Syntax highlighting and script analysis: These are more Visual Studio features made available to PowerShell development.
In order to install the PowerGUI Visual Studio Extension, PowerGUI must be installed, with the caveat that the correct version of each product is installed.
Download Here
It also provides Debugging facility which is the best part I like as a developer.
Thanks!