Comments (5)
Python decorators are executed on eager evaluation as part of its declaration signature, so the expected behavior here is legit.
Indeed, based on your example, you do not have to wrap the test function with @pook.on
, instead you can rely on the use context statement that implicitly enables the pook
engine (equivalent to @pook.on
) during the nested context code execution:
@pytest.mark.asyncio
async def some_test():
with pook.post("somedomain.com/some_path/", reply=200):
# do actual request using await & assertions
pass
If you do not wish to use with
statements, you can explicitly activate the pook engine on demand by calling: pook.on()
and pook.off()
once done.
from pook.
For a more complete example (starting from one provided in the examples) you can check out this one:
import pytest
import pook
import aiohttp
@pook.on
@pytest.mark.asyncio
async def test_async_http_request_with_pook():
pook.get(
"httpbin.org/ip",
reply=403,
response_headers={"pepe": "lopez"},
response_json={"error": "not found"},
)
async with aiohttp.ClientSession(loop=loop) as session:
async with session.get("http://httpbin.org/ip") as res:
print("Status:", res.status)
print("Headers:", res.headers)
print("Body:", await res.text())
print("Is done:", pook.isdone())
print("Pending mocks:", pook.pending_mocks())
print("Unmatched requests:", pook.unmatched_requests())
Once we run pytest
this will be the outcome (again, test skipped):
from pook.
As a workaround we were able to use pook in an async context by using asyncio.get_event_loop
and use the run_until_complete
function directly.
This updated example shows this approach which seems to work as expected:
import pytest
import pook
import aiohttp
import asyncio
@pook.on
def test_async_http_request_with_pook():
async def run_test():
pook.get(
"httpbin.org/ip",
reply=403,
response_headers={"pepe": "lopez"},
response_json={"error": "not found"},
)
async with aiohttp.ClientSession(loop=loop) as session:
async with session.get("http://httpbin.org/ip") as res:
print("Status:", res.status)
print("Headers:", res.headers)
print("Body:", await res.text())
print("Is done:", pook.isdone())
print("Pending mocks:", pook.pending_mocks())
print("Unmatched requests:", pook.unmatched_requests())
loop = asyncio.get_event_loop()
loop.run_until_complete(run_test())
from pook.
Thanks a lot @h2non . I'll give it a try on monday and let you know :)
from pook.
This seems to work as intended. Thank you very very much @h2non! 😛
from pook.
Related Issues (20)
- Cannot pass `param`, `param_exists` or `params` kwarg to `Mock` constructor
- Query param types incorrect compared directly rather than string-to-string
- `param_exists` fails when requested query param has no value
- Deprecate then remove regex formatted string parsing
- `Mock::body` ignores `binary` parameter HOT 1
- `httpx` requests with enabled network are not working HOT 3
- The docs published on pook.rtfd.io seem to be outdated (latest == 0.2.5) HOT 2
- httpx streaming via `iter_raw` raises `httpx.StreamConsumed` HOT 2
- HttpxInterceptor has no disable but a deactivate method HOT 3
- Support httpx client HOT 1
- pook.use() context manager is stilll active after use even in follow-up testcases HOT 7
- The @fluent decorator breaks mypy support
- Missing git tag for 1.0.2 release HOT 1
- Disable PookNoMatch attempted matching output HOT 3
- Intercept mutliple urls
- aiohttp mock only works when using request methods as context managers, but not when awaited HOT 3
- Importing pook fails on Python 3.11 HOT 7
- pook broken in urllib3>=2
- `HeadersMatcher: 're.Pattern' object has no attribute 'decode'` with pook 1.2.0 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pook.