25

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.

enter image description here

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.

Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126
  • This 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` – idjaw Mar 18 '16 at 08:11
  • 2
    ctrl-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). – Erwin Mayer Mar 18 '16 at 08:29
  • which version of pycharm do u have? can u post your code please? so i can test it with my pycharm – qvpham Mar 18 '16 at 08:38
  • As indicated in the question title, I am using PyCharm 5.0.4. I have published a [gist here](https://gist.github.com/mayerwin/21c471f1adaf18728089). – Erwin Mayer Mar 18 '16 at 09:02
  • @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 – idjaw Mar 18 '16 at 14:20
  • @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)? – Erwin Mayer Mar 18 '16 at 18:44
  • @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. – idjaw Mar 19 '16 at 05:20
  • @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). – Erwin Mayer Mar 19 '16 at 10:46
  • 4
    after upgrading to Intellij 2017 I started experiencing this, too. No resolution, yet... – Kirby Mar 22 '17 at 23:22

7 Answers7

14

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:"

enter image description here

John Mee
  • 50,179
  • 34
  • 152
  • 186
  • 3
    Awesome! Just on windows, it worked for me with CTRL+SHIFT+F10. – Karel Macek May 26 '17 at 13:15
  • 3
    This 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) – Leonard Feehan Nov 08 '17 at 10:49
7

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

Maximilian
  • 7,512
  • 3
  • 50
  • 63
  • Thanks, that followed by "Run..." (Alt+Shift+F10) to run the created test works. – 101 Jun 14 '17 at 04:51
  • Yes, and specifically there is a "unit test " option under "python tests" in Edit Configurations – eddys Feb 02 '18 at 10:16
  • I 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. – user48956 Oct 11 '18 at 18:54
4

Under pycharm 2017.2.3:

the key step:

  1. change the default test runner(unittests) to (nosetests or py.test), both ok.
  2. then the IDE can run single test function now.
  3. follow the steps of the below screenshots.

1. change settings:

enter image description here

2. run single test function:

enter image description here

enter image description here

3. run all test functions:

enter image description here

enter image description here

HuanHuang
  • 119
  • 1
  • 5
4

In Pycharm 2018.1: restart, delete the existing run configrations - suddently right-click provides an option to run a single test. :-/

user48956
  • 14,850
  • 19
  • 93
  • 154
3

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.

Richard Løvehjerte
  • 584
  • 1
  • 4
  • 12
  • 1
    I 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). – Erwin Mayer Mar 18 '16 at 08:27
1

Please check whether you have the same test name repeated in two or more locations in the test fixture. I had the same problem and resolving the naming conflicts enabled me to right click on the test name and run it individually.

Sampath
  • 1,144
  • 1
  • 21
  • 38
0

I had this problem with PyCharm 2018.3.

It seemed to be because I had a breakpoint in a strange place (at function declaration, instead of inside the function).

Clearing all the breakpoints seemed to restore the ability to debug individual tests

slashdottir
  • 7,835
  • 7
  • 55
  • 71