Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ python:
- "3.7"
- "3.8"
install:
- pip install .[testing]
- pip install -e .[testing]
script:
- pytest --cov=pytest_httpx --cov-fail-under=100
- pytest --cov=pytest_httpx --cov-fail-under=100 --runpytest=subprocess
deploy:
provider: pypi
username: __token__
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.4.0] - 2020-06-05
### Changed
- `httpx_mock` [`pytest`](https://docs.pytest.org/en/latest/) fixture does not need to be explicitly imported anymore (many thanks to [`Thomas LÉVEIL`](https://github.com/thomasleveil)).

## [0.3.0] - 2020-05-24
### Changed
- Requires [`httpx`](https://www.python-httpx.org)==0.13.*
Expand Down Expand Up @@ -69,7 +73,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- First release, should be considered as unstable for now as design might change.

[Unreleased]: https://github.com/Colin-b/pytest_httpx/compare/v0.3.0...HEAD
[Unreleased]: https://github.com/Colin-b/pytest_httpx/compare/v0.4.0...HEAD
[0.4.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.3.0...v0.4.0
[0.3.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.2.1...v0.3.0
[0.2.1]: https://github.com/Colin-b/pytest_httpx/compare/v0.2.0...v0.2.1
[0.2.0]: https://github.com/Colin-b/pytest_httpx/compare/v0.1.0...v0.2.0
Expand Down
6 changes: 1 addition & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,9 @@ Before creating an issue please make sure that it was not already reported.
* To add the [pre-commit](https://pre-commit.com) hook, after the installation run: **pre-commit install**
6) Add at least one [`pytest`](http://doc.pytest.org/en/latest/index.html) test case.
* Unless it is an internal refactoring request or a documentation update.
7) Increment [version number](https://semver.org) and add related [changelog entry](https://keepachangelog.com/en/1.0.0/).
7) Add related [changelog entry](https://keepachangelog.com/en/1.0.0/).
* Unless it is a documentation update.

##### Changelog entry

Once the changelog entry is added, please don't forget to also add the link to the proper tag at the end of the changelog.

#### Enter pull request

1) Go to the *Pull requests* tab and click on the *New pull request* button.
Expand Down
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ Once installed, `httpx_mock` [`pytest`](https://docs.pytest.org/en/latest/) fixt

You can register responses for both sync and async [`HTTPX`](https://www.python-httpx.org) requests.

`httpx_mock` fixture is available within `pytest_httpx`.

```python
import pytest
import httpx
from pytest_httpx import httpx_mock


def test_something(httpx_mock):
Expand Down Expand Up @@ -73,7 +71,7 @@ Matching is performed on the full URL, query parameters included.

```python
import httpx
from pytest_httpx import httpx_mock, HTTPXMock
from pytest_httpx import HTTPXMock


def test_url(httpx_mock: HTTPXMock):
Expand All @@ -94,7 +92,7 @@ Matching is performed on equality.

```python
import httpx
from pytest_httpx import httpx_mock, HTTPXMock
from pytest_httpx import HTTPXMock


def test_post(httpx_mock: HTTPXMock):
Expand Down Expand Up @@ -141,7 +139,7 @@ Matching is performed on equality for each provided header.

```python
import httpx
from pytest_httpx import httpx_mock, HTTPXMock
from pytest_httpx import HTTPXMock


def test_headers_matching(httpx_mock: HTTPXMock):
Expand All @@ -159,7 +157,7 @@ Matching is performed on equality.

```python
import httpx
from pytest_httpx import httpx_mock, HTTPXMock
from pytest_httpx import HTTPXMock


def test_content_matching(httpx_mock: HTTPXMock):
Expand All @@ -175,7 +173,7 @@ Use `json` parameter to add a JSON response using python values.

```python
import httpx
from pytest_httpx import httpx_mock, HTTPXMock
from pytest_httpx import HTTPXMock


def test_json(httpx_mock: HTTPXMock):
Expand All @@ -192,7 +190,7 @@ Use `data` parameter to reply with a custom body by providing bytes or UTF-8 enc

```python
import httpx
from pytest_httpx import httpx_mock, HTTPXMock
from pytest_httpx import HTTPXMock


def test_str_body(httpx_mock: HTTPXMock):
Expand All @@ -218,7 +216,7 @@ You can specify `boundary` parameter to specify the multipart boundary to use.

```python
import httpx
from pytest_httpx import httpx_mock, HTTPXMock
from pytest_httpx import HTTPXMock


def test_multipart_body(httpx_mock: HTTPXMock):
Expand All @@ -245,7 +243,7 @@ Use `status_code` parameter to specify the HTTP status code of the response.

```python
import httpx
from pytest_httpx import httpx_mock, HTTPXMock
from pytest_httpx import HTTPXMock


def test_status_code(httpx_mock: HTTPXMock):
Expand All @@ -262,7 +260,7 @@ Use `headers` parameter to specify the extra headers of the response.

```python
import httpx
from pytest_httpx import httpx_mock, HTTPXMock
from pytest_httpx import HTTPXMock


def test_headers(httpx_mock: HTTPXMock):
Expand All @@ -279,7 +277,7 @@ Use `http_version` parameter to specify the HTTP protocol version of the respons

```python
import httpx
from pytest_httpx import httpx_mock, HTTPXMock
from pytest_httpx import HTTPXMock


def test_http_version(httpx_mock: HTTPXMock):
Expand All @@ -306,7 +304,7 @@ Callback should return a httpcore response (as a tuple), you can use `pytest_htt

```python
import httpx
from pytest_httpx import httpx_mock, HTTPXMock, to_response
from pytest_httpx import HTTPXMock, to_response


def test_dynamic_response(httpx_mock: HTTPXMock):
Expand All @@ -332,7 +330,7 @@ This can be useful if you want to assert that your code handles HTTPX exceptions
```python
import httpx
import pytest
from pytest_httpx import httpx_mock, HTTPXMock
from pytest_httpx import HTTPXMock


def test_exception_raising(httpx_mock: HTTPXMock):
Expand Down Expand Up @@ -385,7 +383,7 @@ Matching is performed on equality.

```python
import httpx
from pytest_httpx import httpx_mock, HTTPXMock
from pytest_httpx import HTTPXMock


def test_many_requests(httpx_mock: HTTPXMock):
Expand Down
22 changes: 21 additions & 1 deletion pytest_httpx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
import httpx
import pytest

from pytest_httpx._httpx_mock import HTTPXMock, to_response, _PytestSyncTransport, _PytestAsyncTransport
from pytest_httpx.version import __version__
from pytest_httpx._httpx_mock import httpx_mock, HTTPXMock, to_response


@pytest.fixture
def httpx_mock(monkeypatch) -> HTTPXMock:
mock = HTTPXMock()
# Mock synchronous requests
monkeypatch.setattr(
httpx.Client, "transport_for_url", lambda self, url: _PytestSyncTransport(mock),
)
# Mock asynchronous requests
monkeypatch.setattr(
httpx.AsyncClient,
"transport_for_url",
lambda self, url: _PytestAsyncTransport(mock),
)
yield mock
mock.assert_and_reset()
17 changes: 0 additions & 17 deletions pytest_httpx/_httpx_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,23 +290,6 @@ async def request(
return self.mock._handle_request(*args, **kwargs)


@pytest.fixture
def httpx_mock(monkeypatch) -> HTTPXMock:
mock = HTTPXMock()
# Mock synchronous requests
monkeypatch.setattr(
httpx.Client, "transport_for_url", lambda self, url: _PytestSyncTransport(mock),
)
# Mock asynchronous requests
monkeypatch.setattr(
httpx.AsyncClient,
"transport_for_url",
lambda self, url: _PytestAsyncTransport(mock),
)
yield mock
mock.assert_and_reset()


def to_response(
status_code: int = 200,
http_version: str = "HTTP/1.1",
Expand Down
2 changes: 1 addition & 1 deletion pytest_httpx/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# Major should be incremented in case there is a breaking change. (eg: 2.5.8 -> 3.0.0)
# Minor should be incremented in case there is an enhancement. (eg: 2.5.8 -> 2.6.0)
# Patch should be incremented in case there is a bug fix. (eg: 2.5.8 -> 2.5.9)
__version__ = "0.3.0"
__version__ = "0.4.0"
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
],
keywords=["pytest", "testing", "mock", "httpx"],
packages=find_packages(exclude=["tests*"]),
entry_points={'pytest11': ['pytest_httpx = pytest_httpx']},
install_requires=["httpx==0.13.*", "pytest>=5.4.*,<6.*"],
extras_require={
"testing": [
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# see https://docs.pytest.org/en/documentation-restructure/how-to/writing_plugins.html#testing-plugins
pytest_plugins = ["pytester"]
Loading