Skip to content

Make async functions support in pytest hooks #13059

Open
@NadezhdaBel

Description

@NadezhdaBel

What's the problem this feature will solve?

I'm trying to make scteenshots on test failture.
The ways I found in internet is to make it via pytest_runtest_makereport hook.

Somthing like:

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()

    if rep.when == "call" and rep.failed:
        page = item.funcargs["async_page"]
        full_path = "some_path"
        page.screenshot(path=f"{full_path}")
        logging.debug(f"Screenshot saved: {full_path}")

But I got an error: RuntimeWarning: coroutine 'Page.screenshot' was never awaited

The reason of error is that async_page fixture is async:

import pytest_asyncio
from playwright.async_api import Page, async_playwright
from typing import AsyncGenerator


@pytest_asyncio.fixture
async def async_page() -> AsyncGenerator[Page]:
    async with async_playwright() as playwright:
        browser = await playwright.chromium.launch(headless=False)
        context = await browser.new_context(viewport={"width": 1440, "height": 900})
        page = await context.new_page()

        yield page

Describe the solution you'd like

I'd like to have an option to call async functions in Pytest hook.
Or if there are any solutions already exiss, please tell me)

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions