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.
pip install sentry-sdk
Python code
16 linesimport 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
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
- Use `sentry_sdk.init(dsn=None)` to disable Sentry entirely for local runs
- 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
More from Modern tooling
- Build a Recipe Runner Mock in Python easy
- Build a Textual TUI App Skeleton in Python easy
- Configure ruff linter rules in pyproject.toml with Python easy
- Data Conversion Helper Functions in Python easy
- How to Bind and Mock structlog Context in Python medium
- How to Build a Chainable Filter Helper in Python easy
Keep learning
Related tutorials and quizzes for this topic.