operator icon indicating copy to clipboard operation
operator copied to clipboard

`breakpoint()` doesn't work in charm code during unit tests

Open nsklikas opened this issue 1 month ago • 3 comments

Problem

When using Scenario for unit tests, calling breakpoint() in charm code does nothing. It only works in test code or when using import pdb; pdb.set_trace() directly.

Root Cause

The ops Framework overrides sys.breakpointhook with a custom implementation that only activates when JUJU_DEBUG_AT environment variable is set. During unit tests, this variable isn't set, so breakpoint() silently returns without doing anything.

Reproduction

  1. Add breakpoint() to a charm event handler
  2. Write a scenario test that invokes that handler
  3. Run pytest
  4. Breakpoint is never triggered

nsklikas avatar Nov 13 '25 15:11 nsklikas

Hi @nsklikas ! Are you trying to test breakpoint() functionality in your charm, or are you trying to set breakpoints while debugging tests? For the latter, I would have thought using your regular debugger tooling would be more useful than the Ops functionality (which is really meant to help jump into code when running under Juju). For the former, it seems like you'd typically rely on Ops ensuring that it works.

tonyandrewmeyer avatar Nov 14 '25 06:11 tonyandrewmeyer

I am trying to use breakpoint in the charm code when running unit tests (scenario). I am not trying to use ops functionality, but ops is overriding the default python breakpoint behavior.

nsklikas avatar Nov 17 '25 08:11 nsklikas

ref: https://github.com/canonical/operator/pull/440 there's a test in test/test_main.py as well.

dimaqq avatar Nov 18 '25 07:11 dimaqq