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

前端 未结 12 1309
闹比i
闹比i 2020-12-07 09:31

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 t

相关标签:
12条回答
  • 2020-12-07 10:11

    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.

    0 讨论(0)
  • 2020-12-07 10:12

    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
    0 讨论(0)
  • 2020-12-07 10:12

    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.

    0 讨论(0)
  • 2020-12-07 10:14

    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.

    0 讨论(0)
  • 2020-12-07 10:15

    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

    0 讨论(0)
  • 2020-12-07 10:15

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

    1.Tools

    2.Python Integrated Tools

    3.Default test runner

    0 讨论(0)
提交回复
热议问题