Testing Activities

Hi guys,
I’m trying to use a test suite for verifying the workflows and activities input and output. Regarding Activities I was able to verify inputs by using method env.onActivity but I was not able to test output. Is it possible to call an activity, really execute it and test the related result? I did not find an example in the go ask sample repository regarding this.
Could you suggest me an approach?

Great thanks for your time
Marco

Use TestActivityEnvironment for activity testing. It is created through WorkflowTestSuite.NewActivityEnvironment.

Great thanks Maxim,
I successfully used the suggested method and all works fine.
A last question about the topic: could you confirm that the test suite requires the temporal server up and running and that there is no possibility to test and mock without it?
Thanks in advance.
Marco

could you confirm that the test suite requires the temporal server up and running and that there is no possibility to test and mock without it?

Not confirming.

The unit testing framework does not need the temporal service up and running.

It was my fault. I wrongly run the worker during test. I adjusted code and all works fine.

Thanks again for your support
Regards
Marco

1 Like

Hi @maxim, I am trying to test workflow using testsuite.TestWorkflowEnvironment.

I am able to test activities for errors using OnActivity().Return() by returning errors. But when i use the similar OnActivity().Return() for response with nil error, i am not able to see the value from response in the actual workflow.

sample activity call in workflow :

var response string
err = workflow.ExecuteActivity(ctx, SampleActivity).Get(ctx, &response)
if err != nil {
	workflow.GetLogger(ctx).Error("SampleActivity failed with ", err)
	return err
}

Mocking used in unit test :

s.env.OnActivity(SampleActivity, mock.Anything).Return(func(ctx context.Context) (string, error) {
		return "test_string", nil
})

i am not able to see the value from response in the actual workflow.

Can you clarify this? What is response after ExecuteActivity is invoked there or otherwise what error is happening during run? Also, you don’t have to do a callback, .Return("test_string", nil) should be acceptable.

(if easier, you might want to make a new forum post instead of reusing this older one)

Create new post