How to configure PyCharm to run py.test tests?

I want to start writing unit tests for my Python code, and py.test is a better bet than Python in unittest . So I added the "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 supposedly supports py.test in its test runner. You must create a run / debug configuration to run your tests, and PyCharm supposedly has a โ€œcreate configurationโ€ specifically for py.test . But what is their full documentation on this subject, and I cannot find this prospective dialog box anywhere.

If I right-click the directory in the Project tool window, I assumed to see "Create <name>"; but the only menu item starting with "Create" is "Create Startup Configuration". Well, maybe the documentation is just wrong, and "Create Run Configuration" sounds promising. Unfortunately, there are only two items in his submenu - "Unittests in C: \ mypath ..." and "Doctests in C: \ mypath ...", none of which apply - I do not use either unittest or doctest. There is no menu item for py.test.

If I open my test_sample.py and right-click in the editor window, I will get the promised "Create <name>"; menu items: "Create" Unittests in test_sa ... "...", then "Run" Unittests in test_sa ... "and" Debug "Unittests in test_sa ...". So again, all this relates to a unitary structure; nothing for py.test.

If I try menu items that say "unittest", I get a dialog box with the options "Name", "Type", the "Tests" field with "Folder" and "Template" and "Script" and "Class" and "Function" etc. This sounds exactly the same as in the dialog box to add the configuration for the Python Unit Test ., And not like the Name and Test to Run and Keywords parameters that should be displayed in the configuration for py.test . There is nothing in the dialog box to switch the check frames that I add.

I am using PyCharm 1.5.2 for Windows with Python 3.1.3 and pytest 2.0.3. I can successfully run py.test in my tests from the command line, so it's not as simple as pytest, which is not installed properly.

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

+78
python pycharm
Jun 18 '11 at 15:37
source share
5 answers

Please go to File | Settings | Tools | Python Integrated Tools and change the default test runner to py.test. You will then get the py.test parameter to create tests instead of unittest.

+125
Jun 18 '11 at 16:23
source share

I think you need to use the "Run / Debug Configuration" element in the toolbar. Click "Edit Configurations" (or, conversely, use the menu item "Run-> Edit Configurations"). In the "By default" section in the left pane there is the "py.test" element, which I think you need.

I also found that the manual does not match the user interface. I hope I understood the problem correctly and it helps.

+10
Jun 18 '11 at 15:58
source share

This is poorly documented to be sure. After you add the new default configuration, you will be taken to the launch area of โ€‹โ€‹the "/ Applications / PyCharm CE.app/Contents/helpers/pycharm/pytestrunner.py" script. It is not documented and has its own ideas for command line arguments.

You can:

  • Try playing around, cancel the script, and see if you can somehow get py.test to accept the arguments. It might work; this is not the first half hour for me.
  • Just run "py.test * .py" from the console.

Oddly enough, it will be difficult for you to find any discussion, since JetBrains does an excellent job of bombarding Google algorithms with its own pages.

+4
Apr 30 '16 at 19:15
source share

find this topic when I got into the same question and found a solution pycharm version: 2017.1.2 go to "Settings" โ†’ "Tools" โ†’ "Python Integrated Tools" and install the standard test runner from the right panel in the form of py.test solve my the problem

+1
Jun 15 '17 at 15:42 on
source share

With Conda's special python setup, which included installing pip for py.test plus using the Specs add-on (option --spec) (for Rspec, such as a good test addition language), I had to do

1. Change the default py.test value to include the = --spec parameter, which means using the plugin: https://github.com/pchomik/pytest-spec

2. Create a new test configuration using py.test. Change its python interpreter to use ~ / anaconda / envs / of your choice of translators, like py27 for my names.

3.Remove the unittests test configuration.

4. Now the default test configuration is py.test with my beautiful Rspec style findings. I like it! Thanks everyone!

ps Jetbrains document for run / debug configurations is here: https://www.jetbrains.com/help/pycharm/2016.1/run-debug-configuration-py-test.html?search=py.test

0
Aug 23 '16 at 16:28
source share



All Articles