Run a specific unit test function inside PyCharm IDE 5.0.4

11,606

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

enter image description here

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:

  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

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.

Share:
11,606

Related videos on Youtube

Erwin Mayer
Author by

Erwin Mayer

I am happy! SOreadytohelp

Updated on September 28, 2022

Comments

  • Erwin Mayer
    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.

    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.

    • idjaw
      idjaw about 8 years
      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
    • Erwin Mayer
      Erwin Mayer about 8 years
      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).
    • qvpham
      qvpham about 8 years
      which version of pycharm do u have? can u post your code please? so i can test it with my pycharm
    • Erwin Mayer
      Erwin Mayer about 8 years
      As indicated in the question title, I am using PyCharm 5.0.4. I have published a gist here.
    • idjaw
      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
      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
      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
      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
      Kirby about 7 years
      after upgrading to Intellij 2017 I started experiencing this, too. No resolution, yet...
  • Erwin Mayer
    Erwin Mayer about 8 years
    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).
  • Karel Macek
    Karel Macek almost 7 years
    Awesome! Just on windows, it worked for me with CTRL+SHIFT+F10.
  • 101
    101 almost 7 years
    Thanks, that followed by "Run..." (Alt+Shift+F10) to run the created test works.
  • Leonard Feehan
    Leonard Feehan over 6 years
    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)
  • eddys
    eddys about 6 years
    Yes, and specifically there is a "unit test " option under "python tests" in Edit Configurations
  • tonysepia
    tonysepia almost 6 years
    Thank you, but how come the default test runner doesn't support multiple test methods?
  • user48956
    user48956 over 5 years
    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.