Disable Pytest in PyCharm

2020-08-09 09:20发布

If I have a file beginning with "test_", PyCharm tries to run this with PyTest. I want to run it normally (as a regular Python script). How can I do this?

Edit with Solution: As A. Romeau pointed out, there are ways to do this:

  1. For a given test file that you want to run normally:

    • Run the file... note that it runs under py.test or some other testing framework.
    • Click on the drop-down box on the top-right that says something like "py.test in test_something.py".
    • Click "Edit Configurations". This will open a window with "Python" and "Python tests" as options on the left. You'll notice you're in the "Python tests" section.
    • Copy the file path in the "Target:" field.
    • Click the red "-" in the top left, which will remove that test configuration.
    • Now, click the green "+", select "Python", and paste the file path you copied in the "Script:" field.
    • Press "OK". Your test file will now run as an ordinary script.
  2. Edit your setup.cfg file in pytest (with some option that I don't know right now) so that it doesn't "discover" your test (I have not tried this).

Neither solution is good. 1 is inconvenient because you need to go through a big mutli-click and process for each file you might want to run like this. 2 is inconvenient because it interferes with you running pytest normally, from console.

JetBrains: Please for the love of God just add an option to disable automatic unittesting. It is very annoying because the output of pytest/UnitTest is not nearly as helpful as the output of the regular interpreter when trying to track down bugs in tests (no links to the failing line, can't easily run given function alone, etc).

A PyCharm "issue" has been opened here.

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-08-09 09:41

You can either create a run configuration for that specific file, under Run/Edit Configurations, or you can change the test discovery settings for py.test, so files starting with test_* are not considered test files by default.

More information here:

http://pytest.org/2.2.4/example/pythoncollection.html

查看更多
冷血范
3楼-- · 2020-08-09 09:46

Disable 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 Unittests.
  3. Click OK to save the settings.

enter image description here

查看更多
登录 后发表回答