Run a specific unit test function inside PyCharm IDE 5.0.4
Solution 1
Check the default test framework of the project...
You're perhaps used to 'unittest' being the default. Its enables me to put the cursor on the test definition and hit "SHIFT-CTRL-R" to run that one test.
The default seems to have changed to 'py.test' which has different behaviour and keyboard shortcuts. I'm on OSX so ymmv.
On Linux:
File -> Settings -> Tools -> Python Integrated Tools -> Testing / "Default Test Runner"
On OSX:
Preferences -> Tools -> Python Integrated Tools -> "Default test runner:"
Solution 2
With recent versions of PyCharm the availability of the 'right click' option seems intermittent.
One replacement is to go to Edit Configurations...
and type the name of the class and method yourself. That's worked well for me, even if not quite as convenient
Solution 3
Under pycharm 2017.2.3:
the key step:
- change the
default test runner(unittests)
to (nosetests
orpy.test
), both ok. - then the IDE can run single
test function
now. - follow the steps of the below screenshots.
1. change settings:
2. run single test function:
3. run all test functions:
Solution 4
In Pycharm 2018.1: restart, delete the existing run configrations - suddently right-click provides an option to run a single test. :-/
Solution 5
Have you tried right clicking the test in the actual class? It should be possible to run the single test from there. I'd suggest a re-install if this is not available.
Related videos on Youtube
Comments
-
Erwin Mayer over 1 year
I am trying to use PyCharm for unit testing (with
unittest
), and am able to make it work: the test runner nicely shows the list of test cases and nested test functions.However, once the tests have been discovered, I cannot find any way to (re)run a specific test function: the only button available will run the whole list of tests, and right clicking on a single test function doesn't show any meaningful action for this purpose.
As you can imagine, it can take a long time unnecessarily when the purpose is to debug a single test.
How to achieve this? It is possible in Visual Studio for example, and seems like a basic feature so I assume I must be missing something.
-
idjaw about 8 yearsThis is definitely possible. I always run individual tests. The key-board shortcut for it actually is to have your cursor on the method name of the test and do
ctrl-shift-r
-
Erwin Mayer about 8 yearsctrl-shift-r opens the "Replace" dialog, do you know the command name? Also, are you clicking on the method inside the class, or in the test runner window? I cannot really click on the method in the source code, as it is inherited by a derived class (TestDerived) but implemented in a base class (TestAbstract, not visible in the test runner).
-
qvpham about 8 yearswhich version of pycharm do u have? can u post your code please? so i can test it with my pycharm
-
Erwin Mayer about 8 yearsAs indicated in the question title, I am using PyCharm 5.0.4. I have published a gist here.
-
idjaw about 8 years@ErwinMayer I don't remember ever changing my key bindings for that, and ctrl-shift-r runs the particular test I have my cursor selected on. Also, as mentioned, right clicking on the test also works. I looked at my keybindings and it is under rerun-tests
-
Erwin Mayer about 8 years@idjaw I can see this shortcut, in my Keymap ("Default), it is set as Alt+Shift+R. However when I press it nothing happens. Are you clicking first on a single test method (e.g. test_fail_bad) inside the test runner window (not the source code)?
-
idjaw about 8 years@ErwinMayer If I am in the Project pane I can run by specific test file. If I am in my code and set my cursor to the specific method and run that specific test method.
-
Erwin Mayer about 8 years@idjaw OK, so this wouldn't unfortunately work for my test methods as they are in an abstract base class (see the gist in the link above), so the derived class has no method to click upon. Also I guess your method will run a new profile and doesn't preserve the test runner window shown in my screenshot (which would be very convenient when you want to switch from test to test without browsing through the code, as in Visual Studio).
-
Kirby about 7 yearsafter upgrading to Intellij 2017 I started experiencing this, too. No resolution, yet...
-
-
Erwin Mayer about 8 yearsI cannot do that as the test function is declared and implemented in a base class inherited by my TestDerived class (so while the test is properly discovered, there is no 1-to-1 mapping with the code).
-
Karel Macek almost 7 yearsAwesome! Just on windows, it worked for me with CTRL+SHIFT+F10.
-
101 almost 7 yearsThanks, that followed by "Run..." (Alt+Shift+F10) to run the created test works.
-
Leonard Feehan over 6 yearsThis worked for me. I used to be able to right click on a specific method and run or debug just that. Not i have to manually put the cursor inside that method and CTRL+SHIFT+F10 (pycharm pro 2017.1.5 ubuntu 14.04)
-
eddys about 6 yearsYes, and specifically there is a "unit test " option under "python tests" in Edit Configurations
-
tonysepia almost 6 yearsThank you, but how come the default test runner doesn't support multiple test methods?
-
user48956 over 5 yearsI also have this issue. Any word on a fix -- is 2018 now and the unit test detection is still broken in the current release. At $500 a seat, I would hope they'd give it some love.