https://prefect.io logo
Title
a

Austin Weisgrau

02/16/2023, 11:40 PM
Think I have a bug but want to check that I'm using these methods correctly. The use of
prefect.get_run_logger()
is incompatible with testing task functions. A minimal example based on the documentation for testing task functions. I only added the two lines defining and using the logger.
import pytest
from prefect import flow, get_run_logger, task
from prefect.testing.utilities import prefect_test_harness

@pytest.fixture(autouse=True, scope="session")
def prefect_test_fixture():
    with prefect_test_harness():
        yield

@task
def my_favorite_task():
    logger = get_run_logger()
    <http://logger.info|logger.info>("running task")
    return 42

@flow
def my_favorite_flow():
    val = my_favorite_task()
    return val

def test_my_favorite_task():
    assert my_favorite_task.fn() == 42
Raises
MissingContextError("There is no active flow or task run context.")
on the call to
prefect.get_run_logger()