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

前端 未结 12 1308
闹比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:01

    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

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

    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.

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

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

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

    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.

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

    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.

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

    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 讨论(0)
提交回复
热议问题