IntelliJ IDEA
 
Get IntelliJ IDEA
You are viewing the documentation for an earlier version of IntelliJ IDEA.

Pytest

Last modified: 11 February 2024

IntelliJ IDEA supports pytest, a fully functional testing framework.

The following features are available:

  • The dedicated test runner.

  • Code completion for test subject and pytest fixtures.

  • Code navigation.

  • Detailed failing assert reports.

  • Support for Python 2.7 and Python 3.5 and later.

  • Multiprocessing test execution.

By default, the suggested default test runner is unittest. So, to utilize pytest, you need to make it the default test runner first.

Once the pytest package is installed, IntelliJ IDEA detects it and makes it the default project test runner. At any time you can change a test runner in the project settings.

Now, that pytest is set as the default testing framework, you can create a small test for the Car.py sample. Let's create a pytest test to check the brake function.

Note that IntelliJ IDEA recognizes the test subject and offers completion for the Car class' instance.

Autocompletion for the test subject

Although Go To Test Subject and Go To Test commands of the context menu are not supported for pytest, you can navigate to the tested code in car.py by using the Go To Declaration command.

With pytest fixtures you can create small test units that can be reused across the testing module. All you need is to mark a reusable unit with @pytest.fixture.

You can enable sharing fixture instances across tests using the scope parameter of the fixture function. For more information about pytest fixtures, refer to pytest fixtures documentation.

You might want to run your tests on the predefined set of data. IntelliJ IDEA supports test parametrization implemented in pytest through @pytest.mark.parametrize.

If you use the Ultimate edition of IntelliJ IDEA, in addition to the mentioned capabilities, you can use Behavior-Driven Development (BDD) through pytest_bdd. This is particularly helpful when you need to quickly record your test using the Gherkin language and utilize beneficial features of pytest, such as fixture.