pythonintellij-ideapytest

How to make pytest cases "runnable" in IntelliJ


I want to write my test using pytest and to be able to run them (individually) in IntelliJ. I have pytest installed, along with (obviously) Python plugin for the IDE.

My test file (tests/test_main.py) looks like that:

from app.main import sum_numbers


def test_sum_numbers():
    assert sum_numbers(1, 2) == 3
    assert sum_numbers(10, 20) == 30
    assert sum_numbers(1, 2, -3) == 0

Running pytest from terminal works perfectly. But IntelliJ seems to treat this as a regular file, there are no "run" icons before the declarations. Also, running the file from the IDE does nothing, as it is not treated as a test file.

What can I do?


Solution

  • Here what you need to do:

    1) Remove all the existing run configurations for your test file.

    2) Make sure that Preferences | Tools | Python Integrated Tools | Default rest runner is set to pytest.

    After that, you should see the run icon next to your test functions and right-clicking the test file should suggest running the pytest. See the docs for more details.