206

I want to start writing unit tests for my Python code, and the py.test framework sounds like a better bet than Python's bundled unittest. So I added a "tests" directory to my project, and added test_sample.py to it. Now I want to configure PyCharm to run all the tests in my "tests" directory.

PyCharm allegedly supports py.test in its test runner. You're supposed to be able to create a run/debug configuration to run your tests, and PyCharm allegedly has a "create configuration" dialog box specifically for py.test. But that's the complete extent of their documentation on the subject, and I can't find this alleged dialog box anywhere.

If I right-click the directory in the Project tool window, I'm supposed to see a "Create <name>" menu item, but the only menu item starting with "Create" is "Create Run Configuration". Okay, maybe the documentation is just wrong, and "Create Run Configuration" does sound promising. Unfortunately, the only two items in its submenu are "Unittests in C:\mypath..." and "Doctests in C:\mypath...", neither of which applies -- I'm using neither unittest nor doctest. There is no menu item for py.test.

If I open my test_sample.py and right-click in the editor window, I do get the promised "Create <name>" menu items: there's "Create 'Unittests in test_sa...'...", followed by "Run 'Unittests in test_sa...'" and "Debug 'Unittests in test_sa...'". So again, it's all specific to the unittest framework; nothing for py.test.

If I do try the menu items that say "unittest", I get a dialog box with options for "Name", "Type", a "Tests" group box with "Folder" and "Pattern" and "Script" and "Class" and "Function", etc. This sounds exactly like what's documented as the dialog to add a configuration for Python Unit Test, and not like the "Name" and "Test to run" and "Keywords" options that are supposed to show up in the configuration for py.test dialog. There's nothing inside the dialog to switch which test framework I'm adding.

I'm using PyCharm 1.5.2 on Windows with Python 3.1.3 and pytest 2.0.3. I can successfully run py.test on my tests from the command line, so it's not something simple like pytest not being installed properly.

How do I configure PyCharm to run my py.test tests?

15 Answers 15

301

Please go to File| Settings | Tools | Python Integrated Tools and change the default test runner to py.test. Then you'll get the py.test option to create tests instead of the unittest one.

9
  • 1
    Hah! Don't know how I missed that. It only half-works, though: if I right-click on the folder in the Project tool window, it gives me an option to create doctests, not py.tests. (I'll write up a bug report.) But it does work if I right-click in the editor and then change "Script:" to the directory name.
    – Joe White
    Commented Jun 18, 2011 at 20:59
  • 3
    Also remember mark your source directory as a "sources root": jetbrains.com/pycharm/webhelp/content-root.html
    – Inti
    Commented Mar 25, 2014 at 11:57
  • 1
    Is there a way to set this for the whole project (or all projects) rather than per-module?
    – OrangeDog
    Commented Apr 5, 2016 at 10:07
  • 2
    It appears that File | Settings | Tools | Python Integrated Tools is only available in the Project Settings rather than the Default Settings in PyCharm 5.
    – killthrush
    Commented Jun 13, 2017 at 13:12
  • 3
    Once it's configured, how do you actually run the tests?
    – chris
    Commented Mar 27, 2018 at 16:02
77

PyCharm 2017.3

  1. Preference -> Tools -> Python integrated Tools - Choose py.test as Default test runner.
  2. If you use Django Preference -> Languages&Frameworks -> Django - Set tick on Do not use Django Test runner
  3. Clear all previously existing test configurations from Run/Debug configuration, otherwise tests will be run with those older configurations.
  4. To set some default additional arguments update py.test default configuration. Run/Debug Configuration -> Defaults -> Python tests -> py.test -> Additional Arguments
6
  • 3
    In PyCharm 2017.3.2, even after I set py.test as the default, when I right-click on the project in the Projects panel and select New => Python File, the Kind: dropdown offers only: Python file, Unit test, Python stub, not Pytest. I have looked in all of the locations mentioned in the threads above. What am I missing?
    – Tom Baker
    Commented Jan 14, 2018 at 8:42
  • 1
    In the latest PyCharm, "Defaults" is called "Templates"
    – AgDude
    Commented Jul 30, 2019 at 15:05
  • 2
    Step 3 is huge. I was unaware that these configs would automatically be created without me doing anything and they are in a different sub header than the pytest ones so it is not immediately obvious that they are prioritized. Commented Apr 1, 2021 at 20:41
  • 2
    For me point #2 solved the issue. Everything else was set in place (and even some tests are being run by default by pytest, others not), until I've ticked that checkbox.
    – Doki41
    Commented Sep 20, 2021 at 10:17
  • 1
    Indeed, had to "Clear all previously existing test configurations from Run/Debug configuration"
    – Mykeul
    Commented Oct 21, 2022 at 10:47
21

I think you need to use the Run/Debug Configuration item on the toolbar. Click it and 'Edit Configurations' (or alternatively use the menu item Run->Edit Configurations). In the 'Defaults' section in the left pane there is a 'py.test' item which I think is what you want.

I also found that the manual didn't match up to the UI for this. Hope I've understood the problem correctly and that helps.

4
  • 4
    The "Defaults > py.test" page didn't actually give me any way to create a configuration. But if I went to the Edit Configurations dialog, clicked the "+" toolbar button, selected "py.test", and set "Working directory" to the directory containing my tests, that did create a configuration that would run my tests. So +1 for pointing me to a helpful part of the program. I'll give the accepted answer to @yole, though, since I think changing the default to py.test is a better solution -- it should let me run just the current file, and things like that.
    – Joe White
    Commented Jun 18, 2011 at 21:05
  • @JoeWhite Please do add it as answer as it was helpful Commented Nov 21, 2017 at 10:16
  • On the Edit Configuration Window, after choosing my pytest configuration, I also had to choose Target to be Script ath and point it to the root of my tests folder
    – RAbraham
    Commented Jun 25, 2018 at 22:28
  • This is much better way as it opens up other feature as well like passing command-line-arguments etc. Commented Feb 13, 2020 at 6:58
21

Here is how I made it work with pytest 3.7.2 (installed via pip) and pycharms 2017.3:

  1. Go to edit configurations

  1. Add a new run config and select py.test

  1. In the run config details, you need to set target=python and the unnamed field below to tests. It looks like this is the name of your test folder. Not too sure tough. I also recommend the -s argument so that if you debug your tests, the console will behave properly. Without the argument pytest captures the output and makes the debug console buggy.

  1. My tests folder looks like that. This is just below the root of my project (my_project/tests).

  1. My foobar_test.py file: (no imports needed):
def test_foobar():
    print("hello pytest")
    assert True
  1. Run it with the normal run command

4
  • Is there any possibility to run only test from current file? I only found out that I need to change filename in config settings each time I open other file
    – algot
    Commented Dec 3, 2019 at 22:51
  • @algot I don't know that on the top of my head. Have a look at this: jetbrains.com/help/pycharm/performing-tests.html and this: jetbrains.com/pycharm/guide/tips/run-single-test Commented Dec 15, 2019 at 15:00
  • In PyCharm 2020.1.2 Pro, the Target buttons in the screenshot from step 3 are named: "Module name", "Script path", "Costum". Choosing "Module name" works, with everything else being equal to what's shown.
    – bad_coder
    Commented Aug 10, 2020 at 9:21
  • This one seems perfect, with the visuals and all! Commented Jun 22 at 8:45
14

In pycharm 2019.2, you can simply do this to run all tests:

  1. Run > Edit Configurations > Add pytest
  2. Set options as shown in following screenshot screenshot of configuration
  3. Click on Debug (or run pytest using e.g. hotkeys Shift+Alt+F9)

For a higher integration of pytest into pycharm, see https://www.jetbrains.com/help/pycharm/pytest.html

1
  • 1
    This helped me to fix on PycharmCE(V2020.3) Commented Dec 6, 2020 at 9:52
6

It's poorly documented to be sure. Once you get add a new configuration from defaults, you will be in the realm of running the "/Applications/PyCharm CE.app/Contents/helpers/pycharm/pytestrunner.py" script. It's not documented and has its own ideas of command line arguments.

You can:

  1. Try to play around, reverse the script, and see if you can somehow get py.test to accept arguments. It might work; it didn't in the first half hour for me.
  2. Just run "py.test *.py" from a console.

Oddly, you will find it hard to find any discussion as JetBrains does a good job of bombing Google algorithms with its own pages.

3

find this thread when I hit the same question and found the solution pycharm version:2017.1.2 go to "Preferences" -> "Tools" -> "Python Integrated Tools" and set the default test runner from right side panel as py.test solve my problem

1
  • It will work, but then you have to right click the test script file name in the Project window. If you right click the file editor area of the same test script instead, it still shows the old default tool (Unittests) unfortunately. This is annoying as I can't specify the individual function that I want to test.
    – HZhang
    Commented Aug 2, 2017 at 2:05
2

I'm using 2018.2

I do Run -> Edit Configurations... Then click the + in the upper left of the modal dialog. Select "python tests" -> py.test Then I give it a name like "All test with py.test"

I select Target: module name and put in the module where my tests are (that is 'tests' for me) or the module where all my code is if my tests are mixed in with my code. This was tripping me up.

I set the Python interpreter.

I set the working directory to the project directory.

2

To me help next solution:

  1. Go to settings of config
  2. Add new config
  3. Delete old configs enter image description here
1

Enable Pytest for you project

  1. Open the Settings/Preferences | Tools | Python Integrated Tools settings dialog as described in Choosing Your Testing Framework.
  2. In the Default test runner field select pytest.
  3. Click OK to save the settings.

enter image description here

1

Open preferences windows (Command key + "," on Mac):

Python preferences link

1.Tools

2.Python Integrated Tools

3.Default test runner

python Default test runner

1
  • Please notice your screen shot has Unitests selected while the topic of the question is pytest. This can cause confusion to beginners.
    – bad_coder
    Commented Aug 23, 2020 at 10:14
0

With a special Conda python setup which included the pip install for py.test plus usage of the Specs addin (option --spec) (for Rspec like nice test summary language), I had to do ;

1.Edit the default py.test to include option= --spec , which means use the plugin: https://github.com/pchomik/pytest-spec

2.Create new test configuration, using py.test. Change its python interpreter to use ~/anaconda/envs/ your choice of interpreters, eg py27 for my namings.

3.Delete the 'unittests' test configuration.

4.Now the default test config is py.test with my lovely Rspec style outputs. I love it! Thank you everyone!

p.s. Jetbrains' doc on run/debug configs is here: https://www.jetbrains.com/help/pycharm/2016.1/run-debug-configuration-py-test.html?search=py.test

0

With 2018.3 it appears to automatically detect that I'm using pytest, which is nice, but it still doesn't allow running from the top level of the project. I had to run pytest for each tests directory individually.

However, I found that I could choose one of the configurations and manually edit it to run at the root of the project and that this worked. I have to manually choose it in the Configurations drop-down - can't right click on the root folder in the Project pane. But at least it allows me to run all tests at once.

1
  • 1
    Try Edit configurations -> Edit configuration templates -> pytest -> set working directory to project root
    – softarn
    Commented Jan 11, 2022 at 15:08
0

There is a PyCharm documentation: Run/Debug Configuration: pytest available as of SEP 2020.

enter image description here

0

I came across with a bit another case but the same error (I can't past the error here cuz already fixed it).

Check that you really have pytest installed inside your venv. I have pytest installed globally and it's confused me.

If pip is saying that pytest is already installed - check what kind of pip it is (global pip or pip of venv).

which python should return the path to your venv python. Then python -m pip install pytest

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