Skip to content

Allow customizing the default mock response#315

Open
lundberg wants to merge 2 commits into
masterfrom
feature/default-mock
Open

Allow customizing the default mock response#315
lundberg wants to merge 2 commits into
masterfrom
feature/default-mock

Conversation

@lundberg

@lundberg lundberg commented Apr 10, 2026

Copy link
Copy Markdown
Owner

Fixes #227

Swap the regular 200 status default response with e.g. a 404 status when a route not mocked ..

respx_mock = respx.mock(default=httpx.Response(404))
respx_mock.get("https://example.com/known").respond(200)

with respx_mock:
    response = httpx.get("https://example.com/known")  # mocked
    assert response.status_code == 200

    response = httpx.get("https://example.com/unknown")  # not mocked
    assert response.status_code == 404

.. or let unmocked routes result in a network errors ..

respx_mock = respx.mock(default=httpx.NetworkError)
respx_mock.get("https://example.com/known").respond(200)

with respx_mock:
    response = httpx.get("https://example.com/known")  # mocked
    assert response.status_code == 200

    response = httpx.get("https://example.com/unknown")  # not mocked, i.e. will raise

@codecov

codecov Bot commented Apr 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (fc8b43b) to head (10a23c8).

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #315   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines         2941      2984   +43     
  Branches       200       205    +5     
=========================================
+ Hits          2941      2984   +43     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lundberg lundberg added the enhancement New feature or request label Apr 10, 2026
@lundberg lundberg changed the title Allow setting default mock response Allow customizing the default mock response Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider a router option to define default response

1 participant