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.

9
  • 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, 2016 at 8: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). Mar 18, 2016 at 8: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, 2016 at 8:38
  • As indicated in the question title, I am using PyCharm 5.0.4. I have published a gist here. Mar 18, 2016 at 9:02
  • 4
    after upgrading to Intellij 2017 I started experiencing this, too. No resolution, yet...
    – Kirby
    Mar 22, 2017 at 23:22

7 Answers 7

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

2
  • 3
    Awesome! Just on windows, it worked for me with CTRL+SHIFT+F10. May 26, 2017 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) Nov 8, 2017 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

3
  • Thanks, that followed by "Run..." (Alt+Shift+F10) to run the created test works.
    – 101
    Jun 14, 2017 at 4:51
  • Yes, and specifically there is a "unit test " option under "python tests" in Edit Configurations
    – eddys
    Feb 2, 2018 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, 2018 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

1
  • Thank you, but how come the default test runner doesn't support multiple test methods?
    – tonysepia
    Jul 3, 2018 at 15:46
4

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

0
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.

1
  • 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). Mar 18, 2016 at 8: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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.