55<a href =" https://travis-ci.com/Colin-b/pytest_httpx " ><img alt =" Build status " src =" https://api.travis-ci.com/Colin-b/pytest_httpx.svg?branch=master " ></a >
66<a href =" https://travis-ci.com/Colin-b/pytest_httpx " ><img alt =" Coverage " src =" https://img.shields.io/badge/coverage-100%25-brightgreen " ></a >
77<a href =" https://github.com/psf/black " ><img alt =" Code style: black " src =" https://img.shields.io/badge/code%20style-black-000000.svg " ></a >
8- <a href =" https://travis-ci.com/Colin-b/pytest_httpx " ><img alt =" Number of tests " src =" https://img.shields.io/badge/tests-79 passed-blue " ></a >
8+ <a href =" https://travis-ci.com/Colin-b/pytest_httpx " ><img alt =" Number of tests " src =" https://img.shields.io/badge/tests-121 passed-blue " ></a >
99<a href =" https://pypi.org/project/pytest-httpx/ " ><img alt =" Number of downloads " src =" https://img.shields.io/pypi/dm/pytest_httpx " ></a >
1010</p >
1111
@@ -25,6 +25,9 @@ Once installed, `httpx_mock` [`pytest`](https://docs.pytest.org/en/latest/) fixt
2525- [ Add dynamic responses] ( #dynamic-responses )
2626- [ Raising exceptions] ( #raising-exceptions )
2727- [ Check requests] ( #check-sent-requests )
28+ - [ Migrating] ( #migrating-to-pytest-httpx )
29+ - [ responses] ( #from-responses )
30+ - [ aioresponses] ( #from-aioresponses )
2831
2932## Add responses
3033
@@ -393,6 +396,21 @@ def test_exception_raising(httpx_mock: HTTPXMock):
393396
394397```
395398
399+ Note that default behavior is to send an ` httpx.TimeoutException ` in case no response can be found. You can then test this kind of exception this way:
400+
401+ ``` python
402+ import httpx
403+ import pytest
404+ from pytest_httpx import HTTPXMock
405+
406+
407+ def test_timeout (httpx_mock : HTTPXMock):
408+ with httpx.Client() as client:
409+ with pytest.raises(httpx.TimeoutException):
410+ client.get(" http://test_url" )
411+
412+ ```
413+
396414### How callback is selected
397415
398416In case more than one callback match request, the first one not yet executed (according to the registration order) will be executed.
@@ -482,3 +500,109 @@ Matching is performed on equality for each provided header.
482500Use ` match_content ` parameter to specify the full HTTP body executing the callback.
483501
484502Matching is performed on equality.
503+
504+ ## Migrating to pytest-httpx
505+
506+ Here is how to migrate from well-known testing libraries to ` pytest-httpx ` .
507+
508+ ### From responses
509+
510+ | Feature | responses | pytest-httpx |
511+ | :--------| :----------| :-------------|
512+ | Add a response | ` responses.add() ` | ` httpx_mock.add_response() ` |
513+ | Add a callback | ` responses.add_callback() ` | ` httpx_mock.add_callback() ` |
514+ | Retrieve requests | ` responses.calls ` | ` httpx_mock.get_requests() ` |
515+
516+ #### Add a response or a callback
517+
518+ Undocumented parameters means that they are unchanged between ` responses ` and ` pytest-httpx ` .
519+ Below is a list of parameters that will require a change in your code.
520+
521+ | Parameter | responses | pytest-httpx |
522+ | :--------| :----------| :-------------|
523+ | method | ` method=responses.GET ` | ` method="GET" ` |
524+ | body (as bytes) | ` body=b"sample" ` | ` data=b"sample" ` |
525+ | body (as str) | ` body="sample" ` | ` data="sample" ` |
526+ | status code | ` status=201 ` | ` status_code=201 ` |
527+ | headers | ` adding_headers={"name": "value"} ` | ` headers={"name": "value"} ` |
528+ | content-type header | ` content_type="application/custom" ` | ` headers={"content-type": "application/custom"} ` |
529+ | Match the full query | ` match_querystring=True ` | The full query is always matched when providing the ` url ` parameter. |
530+
531+ Sample adding a response with ` responses ` :
532+ ``` python
533+ from responses import RequestsMock
534+
535+ def test_response (responses : RequestsMock):
536+ responses.add(
537+ method = responses.GET ,
538+ url = " http://test_url" ,
539+ body = b " This is the response content" ,
540+ status = 400 ,
541+ )
542+
543+ ```
544+
545+ Sample adding the same response with ` pytest-httpx ` :
546+ ``` python
547+ from pytest_httpx import HTTPXMock
548+
549+ def test_response (httpx_mock : HTTPXMock):
550+ httpx_mock.add_response(
551+ method = " GET" ,
552+ url = " http://test_url" ,
553+ data = b " This is the response content" ,
554+ status_code = 400 ,
555+ )
556+
557+ ```
558+
559+ ### From aioresponses
560+
561+ | Feature | aioresponses | pytest-httpx |
562+ | :--------| :----------| :-------------|
563+ | Add a response | ` aioresponses.method() ` | ` httpx_mock.add_response(method="METHOD") ` |
564+ | Add a callback | ` aioresponses.method() ` | ` httpx_mock.add_callback(method="METHOD") ` |
565+
566+ #### Add a response or a callback
567+
568+ Undocumented parameters means that they are unchanged between ` responses ` and ` pytest-httpx ` .
569+ Below is a list of parameters that will require a change in your code.
570+
571+ | Parameter | responses | pytest-httpx |
572+ | :--------| :----------| :-------------|
573+ | body (as bytes) | ` body=b"sample" ` | ` data=b"sample" ` |
574+ | body (as str) | ` body="sample" ` | ` data="sample" ` |
575+ | body (as JSON) | ` payload=["sample"] ` | ` json=["sample"] ` |
576+ | status code | ` status=201 ` | ` status_code=201 ` |
577+
578+ Sample adding a response with ` aioresponses ` :
579+ ``` python
580+ from aioresponses import aioresponses
581+
582+
583+ @pytest.fixture
584+ def mock_aioresponse ():
585+ with aioresponses() as m:
586+ yield m
587+
588+
589+ def test_response (mock_aioresponse ):
590+ mock_aioresponse.get(
591+ url = " http://test_url" ,
592+ body = b " This is the response content" ,
593+ status = 400 ,
594+ )
595+
596+ ```
597+
598+ Sample adding the same response with ` pytest-httpx ` :
599+ ``` python
600+ def test_response (httpx_mock ):
601+ httpx_mock.add_response(
602+ method = " GET" ,
603+ url = " http://test_url" ,
604+ data = b " This is the response content" ,
605+ status_code = 400 ,
606+ )
607+
608+ ```
0 commit comments