Workflow unit tests questions: why use dummy activities, how to ensure activity was not called, how to create an instance of workflow context?

Hello! Thank you very much for the very nice framework!

I am writing the unit tests for the workflows to be used in our company. We use go-sdk for managing the workflows. And I have some questions about the unit tests. In fact, I have created a folder in my repo for temporal experiments in order to demonstrate what I mean.

  1. Why do we need to register an activity for the workflow to be tested? env.OnActivity function is expected to provide a mock for the activity so mentioning the name as the string seems to be enough. In fact, the registered activity should not be called, it is just dummy. If we do not register the activity we have the panic test panicked: activity "Activity1" is not registered with the TestWorkflowEnvironment

  2. How can we ensure that a given activity is not called for the workflow execution? env.OnActivity().Return().Times(0) or env.OnActivity().Times(0) result in failing the test despite they are not called.

  3. How can we create the workflow context instance? Background() is hidden in the internal package. I need it in order to test the custom workflow context propagators. We cannot test it with the workflow test suite because of So I am trying to make the workflow context, execute the loop InjectFromWorkflow then ExtractToWorkflow using a mock HeaderWriter+HeaderReader. Then I check the resulting workflow context. Since there is no way to create the workflow context explicitly, I need to run a dummy test workflow to get the workflow context from there. And the test looks rather ugly.

  1. The framework needs to know the activity signature to be integrated with the mocking framework. I spend quite a time trying to make it work, but could not. Note that it doesn’t work when you use string activity name. It works if you use a function reference. For example the following works without registration:
	var a *Activities
	env.OnActivity(a.GetGreeting).Return("Hello", nil)

but this does not:

	env.OnActivity("GetGreeting").Return("Hello", nil)
  1. This was missing. I submitted a PR to add support for OnActivity(…).Panic(…)

  2. We cannot open Background() for the workflow context as only the framework should be allowed to create the root workflow context. Unfortunately you have to use your workaround (or use an integration test) to test the propagator until 190 is resolved.

1 Like