PyCharm 2022.2 Help

Test

PyCharm works with multiple testing frameworks out of the box, for example, Python unittests, Pytest, Python nosetests, TwistedTrial, and Python doctests..

In the IDE, you can create a test class directly from the source code together with the necessary test methods. You can switch between test classes and source code with a shortcut, run multiple tests, view statistics for each test, and export test results to a file.

PyCharm also features code coverage that allows you to analyze your code and understand which areas of your code are covered by tests and which areas require more testing.

Add testing libraries

PyCharm allows you to add missing libraries as you code: once the IDE detects that you're using some code from the library that is not added to your project yet, it will prompt you to download and install it.

Adding a missing library via quick-fix

You can also add libraries to your project manually. For example, this can be helpful if you need a specific library version or distribution.

Test Sources Root

Before you start creating tests, make sure that the Test Sources Root is configured for your project. The Test Sources Root is a folder that stores your test code. In the Project tool window, this folder is marked with the the Tests root icon icon.

Create a test root for your project

Follow these steps if you're building your project with the native PyCharm builder:

  1. In the Project tool window (Alt+1), create a new directory in which you will store your test code.

  2. Right-click the new directory and select Mark Directory As | Test Sources Root.

    The tests folder should be marked with the the Tests root icon icon.

    Creating the test sources root
Last modified: 11 August 2022