CLion 2024.1 Help

Python tests

Generally, CLion runs and debugs Python tests in the same way as other Python applications, by running the run/debug configurations you have created. When doing so, it passes the specified test classes or methods to the test runner.

In many cases, you can initiate a testing session from a context menu. For this purpose, the Run and Debug commands are provided in certain context menus. For example, these commands are available for a test class, directory, or a package in the Project tool window. They are also available for a test class or method you are currently working on in the editor.

If you run a test for which there is no permanent run/debug configuration, a temporary configuration is created. You can then save such a configuration using the Run/debug configuration dialog if you want to reuse it later.

The tests run in the background, so you can execute several tests at the same time.

Each running configuration gets its own tab in the Run tool window (the Test Results tab). One tab can aggregate several tests.

See Run, debug, terminate tests for more details on running tests in CLion.

Run all Python tests in a directory

  1. In the Project tool window, select the directory that contains tests to be executed.

  2. From the context menu, select the corresponding run command.

    If the directory contains tests that belong to the different testing frameworks, select the configuration to be used.

    For example, select Run 'All tests in: <directory name>' Run pytest in <directory name>'.

    Run all tests in a directory
  3. Explore results in the test runner.

    Running all pytest tests from one directory

By default, all tests are executed one by one. You can change this behavior for the pytest testing framework and execute your tests in parallel.

Run Python tests in parallel

Enable test multiprocessing to optimize execution of your pytest tests.

To explicitly specify the number of CPUs for test execution:

  1. Install the pytest-xdist.

  2. Specify pytest as the project testing framework.

  3. Select Edit configurations... from the list of the run/debug configurations on the main toolbar. In the Run/Debug Configurations dialog, expand the Pytest tests group, and select pytest in <directory name>.

    CLion creates this configuration when you run all tests in a directory for the very first time. If you haven't executed the tests yet, click the the Add button icon and specify the run/debug Configuration parameters.

  4. In the Run/Debug Configurations dialog, in the Additional Arguments field specify the number of the CPUs to run the tests: -n <number of CPUs> and save the changes.

    Editing Run/Debug configuration for a group of pytest tests
  5. Now run all the tests in the directory again and inspect the output in the Run tool window. In the shown example, The total execution time is 12s 79ms as compared to 30s 13ms when running the same tests consequentially. The test report provides information about the CPUs used to run the tests and execution time.

    Running tests in parallel

Alternatively, you can specify the number of CPUs to run your tests in the pytest.ini file. For example,

[pytest] addopts = -n3

If you can stop a running tests, all running tests stop immediately. Icons of tests in the Run tool window reflect status of the test (passed, failed, aborted).

Last modified: 16 April 2024