Code Monkey home page Code Monkey logo

Comments (5)

h2non avatar h2non commented on June 12, 2024 2

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.

lmammino avatar lmammino commented on June 12, 2024

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):

Screenshot 2020-12-04 at 10 52 30

from pook.

lmammino avatar lmammino commented on June 12, 2024

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())

Screenshot 2020-12-04 at 11 14 52

from pook.

lmammino avatar lmammino commented on June 12, 2024

Thanks a lot @h2non . I'll give it a try on monday and let you know :)

from pook.

lmammino avatar lmammino commented on June 12, 2024

This seems to work as intended. Thank you very very much @h2non! 😛

from pook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.