https://prefect.io logo
Title
c

Crawford Collins

05/01/2020, 7:18 PM
how does everyone test the results of a task?
with Flow("test_target_transformer") as f:
        te = fit_target_transformer(problem, target, train_data)
        df = target_encoder_transform(te, imputed_categorical_df)
 assert df.isna().sum() == 0
I'm trying to test a @task that returns a dataframe, but assert statement errors on
AttributeError: 'FunctionTask' object has no attribute 'isna'
. How do I return this as a DataFrame and not a task?
j

josh

05/01/2020, 7:22 PM
To test the result of a task I believe you need run the flow and then can test based off of that state. e.g.:
with Flow("task-results") as flow:
    v = get_value()

state = flow.run()

assert state.result[v].result == ...
j

Joe Schmid

05/01/2020, 7:27 PM
@Crawford Collins Just to add to what @josh said: think of the
with Flow()...
code as defining the structure of the Flow. Without having ever run the Flow yet, there aren't any results available from your tasks, i.e.
assert df...
doesn't give what you might expect since
df
is only a placeholder in the definition of the flow. Once you run your Flow, then you can examine the results of a Flow run. (This difference between a Flow and a Flow run can be a little confusing at first but once you understand it, it'll make sense.)
:upvote: 3
c

Crawford Collins

05/01/2020, 9:52 PM
thanks. @josh @Joe Schmid this all makes sense.
j

Jim Crist-Harif

05/01/2020, 11:27 PM
If you're just trying to test individual task functions (in the absence of a flow), you could also call the task manually with the
.run
attribute:
@task
def my_function(a, b):
    ...

res = my_function.run(a, b)
assert res.isna().sum() == 0
:upvote: 3