How to make pytest cases "runnable" in IntelliJ

Viewed 2312

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?

2 Answers

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.

Open the source code, right-click and hit run using the option run "filename.py".

IntelliJ will make the configuration for you and then you can click the run button after the configuration is made by the IDE.

Same thing for debug configuration.

Also, the IDE gives you the run button on individual tests after this.

Related