Working with Conditional Breakpoints

Learn how to stop your JetBrains IDE debugger when a certain condition is true.

The Problem

You have a bug in your code, but it's only happening when you've run through a certain number of iterations, or a certain condition is true. You don't want to sit there and click Step Over twenty thousand times, so what can you do?

The Solution

You can use the Condition property on the breakpoint to enter a boolean value and tell the debugger to stop when that condition becomes true.

Place your caret on the line you want to investigate and then invoke "Toggle Breakpoint" ⌘F8 (macOS) / Ctrl+F8 (Windows/Linux). Right-click on the breakpoint and enter a value that evaluates to a boolean and click Done:

python-condition.png

Invoke "Debug" with ⌃D (macOS) / Shift+F9 (Windows/Linux) and note that the debugger now stops when the condition is true:

python-debugger.png

This works in any JetBrains IDE. You can also hover your mouse over the line numbers in the gutter, right-click and select Add Conditional Breakpoint, once again entering a boolean expression in the Condition property and clicking Done:

webstorm-condition.png

Once again, the debugger will stop when your condition evaluates to true:

webstorm-debugger.png

Side note here, if you prefer the debug icon to be adjacent to your line number rather than on top of it, you can right-click in the gutter and select Appearance and then clear Breakpoints Over Line Numbers to configure it.


Related Resources

Add a Breakpoint
Add a Breakpoint
Add breakpoints when you get an exception in your code.
Debugging During Testing
Debugging During Testing
When writing tests, use the PyCharm "visual debugger" to interactively poke around in the context of a problem.
Evaluate Expression
Evaluate Expression
Evaluate an expression during a debugging session to learn more about the problem.