Back to all reviewers

Minimize mocks verify behavior

getsentry/sentry
Based on 7 comments
Python

Write tests that verify actual system behavior rather than implementation details by minimizing mock usage and focusing on real interactions. Excessive mocking can lead to brittle tests that pass even when core functionality is broken.

Testing Python

Reviewer Prompt

Write tests that verify actual system behavior rather than implementation details by minimizing mock usage and focusing on real interactions. Excessive mocking can lead to brittle tests that pass even when core functionality is broken.

Key guidelines:

  1. Prefer real implementations over mocks when practical
  2. When mocking is necessary, mock at system boundaries rather than internal components
  3. Use built-in test helpers (e.g. @override_options) instead of mocking configuration
  4. Break complex test scenarios into separate focused test methods

Example - Instead of:

@patch("sentry.api.endpoints.seer_rpc.integration_service.get_organization_integrations")
@patch("sentry.api.endpoints.seer_rpc.options.get")
def test_complex_scenario(self, mock_options, mock_integrations):
    mock_integrations.return_value = []
    mock_options.return_value = []
    # Test implementation

Prefer:

@override_options({"feature.flag": True})
def test_complex_scenario(self):
    # Create real integration
    integration = self.create_integration(
        organization=self.organization,
        provider="github"
    )
    # Test actual behavior

This approach produces more reliable tests that better verify system correctness and are easier to maintain.

7
Comments Analyzed
Python
Primary Language
Testing
Category

Source Discussions