Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 411
Tomek Buszewski Avatar asked Mar 23 '20 19:03

Tomek Buszewski


People also ask

Is pytest part of Python?

Pytest is a Python testing framework that originated from the PyPy project. It can be used to write various types of software tests, including unit tests, integration tests, end-to-end tests, and functional tests.

What is pytest module?

PyTest is a testing framework that allows users to write test codes using Python programming language. It helps you to write simple and scalable test cases for databases, APIs, or UI. PyTest is mainly used for writing tests for APIs. It helps to write tests from simple unit tests to complex functional tests.


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

like image 137
Sergey K. Avatar answered Oct 17 '22 15:10

Sergey K.