PyCharm Unit Testing
Unit testing is a fundamental means of ensuring code quality; it verifies whether the behavior of the smallest testable unit matches expectations.
Writing and running unit tests in PyCharm is very convenient; the IDE provides a complete workflow from test creation to execution to result analysis.
PyCharm has built-in support for three mainstream Python testing frameworks: unittest, pytest, and nose.
Write a test example
The following complete example demonstrates how to write unit tests in PyCharm.
We first create a simple Car class, then write test cases for it.
Code under test: car.py
Example
car.py - a simple Car class used to demonstrate unit test writing
class
Car:
"""Simulated car class providing acceleration and braking functions"""
def
init
(
self
,
make
=
"runoob"
,
model
=
"RUNOOB-X1"
)
:
Initialize the car brand and model
self
.
make
=
make
self
.
model
=
model
Initial speed is 0
self
.
speed
=
0
def
accelerate
(
self
,
increment
)
:
"""Acceleration method
Parameters:
increment: speed increment, must be a positive integer
Returns:
Current speed after acceleration
Exceptions:
ValueError: raised when increment is negative or zero
"""
if
increment
<=
0
:
raise
ValueError
(
"Speed increment must be positive"
)
self
.
speed
=
increment
return
self
.
speed
def
brake
(
self
,
decrement
)
:
"""Braking method
Parameters:
decrement: deceleration amount, must be a positive integer
Returns:
Current speed after braking, minimum 0
Exceptions:
ValueError: raised when decrement is negative or zero
"""
if
decrement
<=
0
:
raise
ValueError
(
"Deceleration amount must be positive"
)
self
.
speed
=
max
(
0
,
self
.
speed
- decrement
)
return
self
.
speed
Test code: test_car.py
Example
test_car.py - unit tests for the Car class
Write test cases using the unittest framework
import
unittest
from
car
import
Car
class
TestCar
(
unittest
.
TestCase
)
:
"""Test suite for the Car class"""
def
setUp
(
self
)
:
"""setUp is called before each test method runs, creating a fresh Car instance"""
self
.
car
=
Car
(
)
def
test_initial_speed
(
self
)
:
"""Test whether the initial speed is 0"""
self
.
assertEqual
(
self
.
car
.
speed
,
0
,
"A new car's initial speed should be 0"
)
def
test_accelerate_normal
(
self
)
:
"""Test the normal acceleration scenario"""
Accelerate by 50
result
=
self
.
car
.
accelerate
(
50
)
Verify the return value
self
.
assertEqual
(
result
,
50
,
"Speed after acceleration should be 50"
)
Verify object state
self
.
assertEqual
(
self
.
car
.
speed
,
50
,
"The car.speed attribute should be updated to 50"
)
def
test_accelerate_multiple_times
(
self
)
:
"""Test the cumulative effect of multiple accelerations"""
First acceleration by 30
self
.
car
.
accelerate
(
30
)
Second acceleration by 20
result
=
self
.
car
.
accelerate
(
20
)
Total speed should be 50
self
.
assertEqual
(
result
,
50
,
"Speed should be 50 after two accelerations"
)
def
test_accelerate_with_zero
(
self
)
:
"""Test that an exception should be raised when acceleration increment is 0"""
with
self
.
assertRaises
(
ValueError
,
msg
=
"Acceleration increment of 0 should raise ValueError"
)
:
self
.
car
.
accelerate
(
0
)
def
test_accelerate_negative
(
self
)
:
"""Test that an exception should be raised when acceleration increment is negative"""
with
self
.
assertRaises
(
ValueError
,
msg
=
"Negative acceleration increment should raise ValueError"
)
:
self
.
car
.
accelerate
(
10
)
def
test_brake_normal
(
self
)
:
"""Test normal braking scenario"""
Accelerate to 80 first
self
.
car
.
accelerate
(
80
)
Brake 30
result
=
self
.
car
.
brake
(
30
)
Verify speed
self
.
assertEqual
(
result
,
50
,
"Speed should be 50 after braking"
)
def
test_brake_below_zero
(
self
)
:
"""Test that braking will not let speed drop below 0"""
Accelerate to 20 first
self
.
car
.
accelerate
(
20
)
Brake 50, speed should be clamped to 0
result
=
self
.
car
.
brake
(
50
)
self
.
assertEqual
(
result
,
0
,
"Speed should not go below 0"
)
def
test_brake_with_zero
(
self
)
:
"""Test that an exception should be raised when deceleration amount is 0"""
with
self
.
assertRaises
(
ValueError
,
msg
=
"Deceleration amount of 0 should raise ValueError"
)
:
self
.
car
.
brake
(
0
)
Run tests when the script is executed directly
if
name
==
"main"
:
unittest
.
main
(
)

PyCharm can automatically recognize test classes and test methods; test class names start with Test (a prefix), test_ which is the naming convention for Python tests.
Running tests
After writing the test code, there are multiple ways to run tests.
Method 1: Click the green run icon
PyCharm displays a green run icon in the Gutter area to the left of the test class and each test method.
Clicking the green icon next to the class name runs all tests in that class; clicking the icon next to a method runs only that single test.
You can also right-click the test file and select Run 'test_car' from the menu.
Method 2: Use the keyboard shortcut
Place the cursor inside the test class or test method and press Ctrl + Shift + F10 to run the current test.
View test results
After the test run completes, PyCharm displays the results in a tree view in the Run tool window at the bottom.
Passing tests are marked with a green check, failed tests with a red cross, along with detailed error messages and stack traces.
Clicking a failed test item jumps directly to the offending line of code, making it easy to locate the problem quickly.
Switch test framework
PyCharm uses unittest as the test runner by default, but you can easily switch to another framework.
Configuration steps
- Open Settings (shortcut Ctrl + Alt + S, or Command + , on macOS).
- Navigate to Python.
- In the Integrated Tools section, find the Test dropdown menu.
- Choose the framework you need from Unittests, pytest, or Nosetests.
- Click OK to save the settings.

If you choose pytest as the test runner, make sure you have installed pip install pytest the pytest package.
TDD recommendations
Unit testing is not only for verifying existing code, but is also a core practice of Test-Driven Development (TDD).
The recommended workflow is to write the test cases first, then the implementation code, and finally verify through the tests.
Developing the habit of writing tests will significantly improve the quality of your code.