Riccardo Tesselli
08/01/2022, 11:51 AMtest
of a custom block type Config
, and when trying to load its values within a flow with Config.load('test')
I get this error:
except prefect.exceptions.ObjectNotFound as e:
> raise ValueError(
f"Unable to find block document named {block_document_name} for block type {block_type_slug}"
) from e
E ValueError: Unable to find block document named test for block type config
pytest
and the normal one, they differ in prefect.client.get_client
in line 110 ctx = prefect.context.get_settings_context()
they differ because in the pytest
run in this entry for ctx
: PREFECT_API_URL=None
, while the other has the field setPREFECT_API_KEY
is set correctly in both runs<http://logger.info|logger.info>(f"PREFECT_API_URL: {PREFECT_API_URL.value()}")
when running normally (within
if __name__ == '__main__':
) I get the correct URL, but if I run pytest (which then calls the flow) the result is PREFECT_API_URL: None
Anna Geller
from prefect import flow
from prefect.testing.utilities import prefect_test_harness
@flow
def my_favorite_flow():
return 42
def test_my_favorite_flow():
with prefect_test_harness():
# run the flow against a temporary testing database
assert my_favorite_flow() == 42
Riccardo Tesselli
08/01/2022, 1:39 PMPREFECT_API_URL
Anna Geller
Riccardo Tesselli
08/01/2022, 1:49 PMAnna Geller
Riccardo Tesselli
08/02/2022, 9:32 AM@flow
def myflow(config_block: str):
config = MyConfig.load(config_block)
then I want to test the execution of that flow by doing in a test file something like this:
def test_flow():
myflow("test-config")
the reason why it fails loading the block it’s because I’ve set this fixture in pytest (as suggested in https://docs.prefect.io/tutorials/testing/#unit-testing-flows)
@pytest.fixture(autouse=True, scope="session")
def prefect_test_fixture():
with prefect_test_harness():
yield
so, when the fixture is in place, it prevents the flow to run on the cloud (which is what I want since I do not want my local tests to be shown in Prefect Cloud), but then I’m not able to load a block from Cloud. Do you have a way/best practice in order to be able to run local tests in the local database but at the same time being able to load blocks from Cloud?