Pytest plugin for pook
Reason this release was yanked:
Add explicit beta version numbering
Project description
pytest-pook
A pytest plugin for pook
.
Installation
pip install pytest-pook
Pytest automatically finds and configures the plugin.
Usage
@pytest.mark.pook
Tests that rely on pook can be marked with @pytest.mark.pook
. The mark wraps the test in pook.use()
in a way compatible with pytest, that does not interfere with other marks. It also asserts that any declared pook mocks were actually matched. This prevents the easy mistake of declareing a mock intenteded to be matched, but that never matches. Normally this requires manually asserting pook.isdone()
at the end of a test, but this is too easy to forget and is tedious anyhow.
Within the body of the test, use the global pook import to interact with pook for things like creating new mocks.
Use the mark's default behaviour as follows:
import pytest
import requests
import pook
@pytest.mark.pook
def test_network_call():
pook.get("https://example.com").reply(200).body("Hello from pook")
res = requests.get("https://example.com")
assert res.text == "Hello from pook"
Delay pook activation
If your test requires real network calls to happen before pook is enabled, pass start_active=False
to the mark to delay pook activation until pook.on
is called:
import pytest
import requests
import pook
@pytest.mark.pook(start_active=False)
def test_network_call():
fixture = requests.get("localhost:8080/my-fixture").json()
(pook.post("https://example.com")
.json(fixture)
.reply(200)
.body("Hello from pook"))
pook.on()
res = requests.get("https://example.com", data=fixture)
assert res.text == "Hello from pook"
With this approach, you still get the benefits of automatic cleanup and checks, but can still make outbound network calls before pook starts capturing them later in your test.
Dangling or unused pending mocks
By default the plugin raises an exception if it detects if pending mocks exist after the test is finished. This helps catch the easy-to-miss edge case where a pook was intended to match a request but didn't match any. This is commonly caused either by the tested code not using a compatible HTTP library or by misconfigured mocks.
To disable this check, pass allow_pending_mocks=True
to the mark. In this case, it is recommended to manually confirm the mocks are in the expected state at the end of the test.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest_pook-0.1.0.tar.gz
.
File metadata
- Download URL: pytest_pook-0.1.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e87650217f48717913788d506a01f4f1e4ae471026fae3a5e2975cb14dbffa57 |
|
MD5 | 58a29a499b87f986f3d671c9e96c7c54 |
|
BLAKE2b-256 | 548d9eb64b16d002a143da778edab1288676aaec9308c99cd85f0d643b44a338 |
File details
Details for the file pytest_pook-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: pytest_pook-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d5b1a6e7fa808e19ce411ef2a21f9e2002d980dc16ad36a966ac394181e3065 |
|
MD5 | d4940a5fa920bf9bf8b9b96e64062977 |
|
BLAKE2b-256 | 387407312996362dc2c460673a4f451660ff08e37cb79eb36f354d234f64d4f5 |