PyCharm
 
Get PyCharm

Pytest

Last modified: 26 September 2024

PyCharm 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, PyCharm 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 class. Let's create a pytest test to check the brake function.

Note that PyCharm 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 Ctrl0B 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. PyCharm supports test parametrization implemented in pytest through @pytest.mark.parametrize.