Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a pytest fixture for pook #134

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions examples/pytest_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ def test_no_match_exception():
pook.get("server.com/bar", reply=204)
with pytest.raises(Exception):
requests.get("http://server.com/baz")


def test_fixture(pook):
pook.get("https://example.org/").reply(204)
res = requests.get("https://example.org/")
assert res.status_code == 204
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ requires-python = ">=3.8"
[project.urls]
Homepage = "https://github.com/h2non/pook"

[project.entry-points.pytest11]
pook = "pook.pytest_fixture"

[tool.hatch.version]
path = "src/pook/__init__.py"

Expand Down
11 changes: 11 additions & 0 deletions src/pook/pytest_fixture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest

import pook as pook_mod


@pytest.fixture
def pook():
"""Pytest fixture for HTTP traffic mocking and testing"""
pook_mod.on()
yield pook_mod
pook_mod.off()
Copy link
Collaborator

@sarayourfriend sarayourfriend Jun 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pook_mod.on()
yield pook_mod
pook_mod.off()
with pook_mod.use() as engine:
yield pook_mod

(Updated to yield pook_mod as per the discussion)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would not work with intended usage, which look like the example included in this PR.

Now it would be something like

AttributeError: 'Engine' object has no attribute 'get'

How do you use the engine? Can the fixture still yield the module instead?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the fixture still yield the module instead?

Yes, I think that's right, apologies!

Loading