for the testing flows example (<https://docs.prefe...
# prefect-community
d
for the testing flows example (https://docs.prefect.io/core/idioms/testing-flows.html), is there an equivalent example for using the
Copy code
with Flow() as flow:
   e = Extract()
   t = Transform(e)
   l = Load(t)
state = flow.run()
syntax? I'm getting key not found results on asserting state.result[e], and wondering if you have to do it in the way given in the testing-flows link above to unit test the flows.
I'm wondering if you have to explicitly build the dag as the example code shows in order to explicitly test things.
n
Hi @Dolor Oculus I don’t think there’s any reason you’d need to follow a particular syntax when testing - can you share a minimum reproducible example of what you’re seeing?
d
Hi, I was probably doing something dumb. Here's a repro that works perfectly
Copy code
def test_simple_flow():
    @task
    def extract():
        return 42

    @task
    def transform(x: int) -> int:
        return int(x / 2 + 2)

    @task
    def load(x: int) -> str:
        return str(x)

    with Flow(__name__, environment=LocalEnvironment()) as flow:
        e = extract()
        t = transform(e)
        l = load(t)

    state = flow.run()
    assert state.is_successful()

    assert state.result[e].is_successful()
    assert state.result[t].is_successful()
    assert state.result[l].is_successful()

    assert state.result[e].result == 42
    assert state.result[t].result == 23
    assert state.result[l].result == "23"
n
Happens to the best of us!