How to Initialize Sentry SDK with a Mock DSN in Python

Initialize the Sentry SDK in Python with a mock DSN to test error tracking without sending real events, then verify the DSN configuration.

Easy Python 3.9+ Aug 9, 2026 Modern tooling 13 views 0 copies

Requires third-party packages — install first
pip install sentry-sdk

Python code

16 lines
Python 3.9+
import sentry_sdk

# Initialize Sentry SDK with a mock DSN (no real events will be sent)
sentry_sdk.init(
    dsn="https://mock-public@mock-host/mock-project",
    traces_sample_rate=1.0,
    environment="development",
)

# Capture a test message to confirm SDK is configured
sentry_sdk.capture_message("Test message from mock DSN setup")

# Read and print the current DSN to verify configuration
client = sentry_sdk.get_client()
current_dsn = client.get_options().get("dsn")
print(f"Configured DSN: {current_dsn}")

Output

stdout
Configured DSN: https://mock-public@mock-host/mock-project

How it works

The sentry_sdk.init function configures the SDK with a DSN that points to a non-existent mock endpoint, so no real events are transmitted. By setting traces_sample_rate=1.0, all transactions are sampled for local testing, and environment marks the context as development. The capture_message call creates a test event that would be sent to Sentry, but with a mock DSN it is silently dropped. Using get_client().get_options() retrieves the effective configuration, confirming the DSN is set correctly.

Common mistakes

  • Using a fake DSN that still attempts network calls, causing timeouts; mock DSNs should be unreachable to avoid delays
  • Forgetting to add `sentry-sdk` to your `requirements.txt` or virtual environment
  • Assuming `capture_message` raises an error with a mock DSN; it returns an event ID even when no send occurs

Variations

  1. Use `sentry_sdk.init(dsn=None)` to disable Sentry entirely for local runs
  2. Set `send_default_pii=False` and `debug=True` to get more logging during development

Real-world use cases

  • Run unit tests that exercise error-handling code without polluting Sentry with fake exceptions.
  • Frontend developers verify SDK setup in CI by initializing with a mock DSN before production.
  • Debugging local feature branches where real error reporting would clutter the issue tracker.

Sponsored

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Modern tooling

Related tutorials and quizzes for this topic.