shortcut to run current Python unit test in VSCode - python

The question is regarding Visual Studio Code (VSCode from here) and python VSCode extension that finds and runs py.test tests.
Is it possible to assign some shortcut to run current (under cursor) test method and/or test class?
I really like the ability to run single test straight from VSCode, but my workflow is not optimal since it is necessary to click it and just use some shortcut.
This image shows the buttons that appear when tests are found and can be run. I would like to know if it is possible to just use some custom shortcut instead of clicking this buttons.

Try Test Explorer UI and Python Test Explorer for Visual Studio Code.
The command list:
However, run-test-at-cursor doesn't work for me. I use run-file. Moreover, maybe you should join workbench.action.files.save, test-explorer.reload, and test-explorer.run-test-at-cursor with a macro extension.

Go to File > Preferences > Keyboard Shortcuts
In the searchbox, type python
All Python commands will be displayed below.
Select the command you want to add a key binding and click on the '+' button. Enter your preferred key combination in the new window.

Starting with VSCode 1.59 and the new Testing API (?) there are now these two new commands:
Run Test at Cursor
Debug Test at Cursor

Assuming the unit test you want to repeatedly run is selected in the Test sidebar (which you can do just by clicking on it), I've found a keyboard-driven option that doesn't require an extension to repeat running it. From anywhere in VS Code, type:
shift+cmd+i, which for me is bound to the command workbench.view.extension.test.
(Optional) Note that annoyingly, you sometimes have to press shift+cmd+i a second time to select the unit test in question (this seems like a bug to me - this happens with all Side Bar views)
Tab-Tab-Space (i.e. Tab, then Tab, then Space). The two tabs select the little bug symbol on your test and the Space kicks off another debug run of the selected test.
Here's how the screen should look after the two Tab presses:
and then the Space "clicks" the selected debug button.
With these 2-3 steps you can repeat this unit test ad infinitum.
HTH

In my case, a lot of test functions already have shortcuts:
I also added a shortcut to get to the test bench with the keyboard shortcuts utility.

Run/Debug/Interactive console in VSCode Editor ==>
File -> Preferences -> Extensions -> Robot > Code Lens: Enable/Disable
File -> Preferences -> Extensions -> Robot: Variables -> Edit in settings.json
need to add "ENV":"DEV"
Restart VS Code editor

Related

In VS Code Test Explorer, How to turn off the "play" icon in gutter?

In VS Code, with the Python, Text Explorer, and Python Test Explorer extensions, I am able to unit test great with pytest.
But two of the "gutter" icons interfere with each other, and I cannot figure out how to remove one of them.
Background
There are two different windows for Test Explorer with this setup - one for the base Test Explorer, and one for the Python Test Explorer extension. They work basically the same but I find the organization of the tests better in the Python one, so I use that.
In the test code itself, there are various code lenses applied, and other decorators, that allow you to interact with the tests by running, debugging, opening that test in the test explorer window, etc.
These decorators include an icon that shows up in the gutter, which allows you to run the test, then changes afterward to show success or failure. If you right-click on that icon you get multiple test options, as shown below. There is also a secondary icon showing the status behind the triangle "play" icon in the image below:
The settings for test explorer allow removing the secondary icon in the background (the faint green circle with the checkmark behind the bright triangle "play" icon), with a setting called "Test Explorer: Gutter Decoration". Changing that removes this secondary icon:
Gutter Decorator True:
Gutter Decorator False:
Question
So, my question is - how do I remove the Test Explorer "play" icon from the gutter?
This icon appears to be the main icon connected to the main Test Explorer UI, rather than the Python one (the commands will go to the main UI window and run it's test runs, rather than the Python window). It gets in the way of the status gutter icon behind it that shows the status of the Python test runs and is unnecessary in my setup.
I recently had my PC wiped and I used to have this turned off somehow, but I can't find the setting now. Thanks!
Please try to delete Test Explorer extension. This extension is no longer updated.
It turns out the Test Explorer extension is no longer being updated, per MingJie-MSFT's answer, as it appears the extension was integrated into the main VS Code codebase.
This led me to search the settings again, and I found that most of the settings from the extension (search for "Test Explorer") are now duplicated in another section called "Testing".
So, it turns out the play icon was showing up from the new integrated test explorer, not from the extension. So I was able to shut it off in the new setting:
However, per MingJie-MSFT's answer, the Test Explorer extension is no longer being maintained, so unless the Python specific Test Explorer extension UI was integrated into VS Code as well, at some point this alternate UI (which I prefer some of the design decisions) will likely no longer be available.

python extension for vs code

I have a problem with python autocomplete in visual studio code. The only way I can trigger the autocomplete function is with the Tab or Enter key. Brackets and dot does not have any effect (or better it just hides the suggestion box and puts the bracket in place on the unfinished word e.g. -> typing pr in the editor would display the suggestion box with print as the first choice, if i then press bracket i end up with pr() instead of print())
I presume there should be some settings so autocomplete would act "normally" but I can't find it.
This is set by the shortcut key. Open the Default Keyboard Shortcuts (File > Preferences > Keyboard Shortcuts) and search for "acceptSelectedSuggestion". You will see that there are only two settings by default, the Tab and Enter keys.
If you want to add other buttons to trigger typing intellisense, right-click on one of the settings and select Add Keybinding,
then press the button you want to set, and press Enter to save.
I think you don't need to use the python autocomplete extension. You can just use the Python extension.
Tab or enter is required to actually make a selection. Otherwise, you could have custom function printStuff, and typing pr( would not necessarily pick the right one.
From what I can tell, PyCharm works the exact same way, so unclear what "acts normal" means in this context.

VS Code unit test checkmarks in the editor do not show up

I am new to VS Code and I run unittest in VS Code for Python.
The checkmarks are for each method is shown only inside the beaker button but not inside the code editor.
I follow the steps below but nothing shows up inside the text editor.
To test your classes use the keyboard shortcut "Ctrl + shift + P" for windows or "cmd+shift+P" on Mac to bring up the command Palette.
Enter "Python: Configure Tests" and select unittest. You will then be prompted to select the directory of where your tests are located and finally you need to
choose "test_*.py" option because your file names should have the form test_.py
This will allow you to individually test methods without having to wait for all of them to be tested.
To test all methods and to view coverage do the following.
python will run your test file and tell you how many passed

How to run only block of code in PyCharm like in Spyder?

In Spyder I can run only a part of code without running everything. I know that in PyCharm I can click right mouse button and "Execute Selection in Console", but it will be new execution without values and variables which declared before this part of code.
So, very often I need to run only few last lines of my code, with parametres and options that I already have.
you can try to use the interactive interpreter while in debug mode.
I find it to be very useful when trying to run code snippets in the program.
view the screenshot below.
You can use the "Run cell" functionality and the cell will be executed in the Python Console (the same way it does when right clicking "Execute Selection in Console"). To enable that I am aware of two options :
In settings (Ctrl+Alt+S) install the "PyCharm cell mode" plugin.
Then use ## to create code sections.
https://plugins.jetbrains.com/plugin/7858-pycharm-cell-mode
Create a new project in scientific mode (only available in PyCharm professional)
You can create code cells with #%%.
https://www.jetbrains.com/help/pycharm/matplotlib-support.html
In both cases, it creates cells that you can execute with the green "play" button like shown below :

Is there a Python shortcut to circumvent need for `print()`

In R 3 * 2 typed on the editor can be executed in the console as [1] 6 by having the cursor on the line where the code is typed; clicking on Run if using RStudio, or through Ctrl + Enter. Very convenient.
New to Python, I am coming to realize the if I want to see 6, I may need to type print(3 * 2), unless I type the expression directly on the Python console. Or, is there a shortcut?
Incidentally, I am using Pycharm as IDE.
In the Pycharm charm editor go to
Settings > Keymap > Other
And change the kep map for "Execute selection in console". Double click it and select "Add keyboard shortcut"
I think the default is set the Alt+Shift+E. I was also from an R background before Pycharm and was used to the shortcut of Ctrl+R to run selected code. I think Ctrl+R might be something in Pycharm because I decided a long while back to map mine to Alt+R.
Once this is done, you can highlight a section and use your new shortcut to run it in a console. You can also just have a cursor on the line and using the shortcut will run the line and move to the next.
You need IPython. In particular, this ability to select a section of a saved file and re-execute it with a click or keypress is the hallmark of the Jupyter interface (previously known as "IPython Notebook").

Categories

Resources