Skip to content

Commit ce0ea05

Browse files
committed
Add notes about testing
1 parent d5ba5d3 commit ce0ea05

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,38 @@ func Workflow2(ctx workflow.Context, msg string) (string, error) {
344344
}
345345
```
346346
347+
### Unit testing
348+
349+
go-workflows includes support for testing workflows, a simple example using mocked activities:
350+
351+
```go
352+
func TestWorkflow(t *testing.T) {
353+
tester := tester.NewWorkflowTester(Workflow1)
354+
355+
// Mock two activities
356+
tester.OnActivity(Activity1, mock.Anything, 35, 12).Return(47, nil)
357+
tester.OnActivity(Activity2, mock.Anything, mock.Anything, mock.Anything).Return(12, nil)
358+
359+
// Run workflow with inputs
360+
tester.Execute("Hello world")
361+
362+
// Workflows always run to completion, or time-out
363+
require.True(t, tester.WorkflowFinished())
364+
365+
var wr int
366+
var werr string
367+
tester.WorkflowResult(&wr, &werr)
368+
require.Equal(t, 59, wr)
369+
require.Empty(t, werr)
370+
371+
// Ensure any expectations set for activity or sub-workflow mocks were met
372+
tester.AssertExpectations(t)
373+
}
374+
```
375+
376+
- Timers are automatically fired by advancing a mock workflow clock that is used for testing workflows
377+
- You can register callbacks to fire at specific times (in mock-clock time). Callbacks can send signals, cancel workflows etc.
378+
347379
## Versioning
348380
349381
For now, I've intentionally left our versioning. Cadence, Temporal, and DTFx all support the concept of versions for workflows as well as activities. This is mostly required when you make changes to workflows and need to keep backwards compatibility with workflows that are being executed at the time of the upgrade.

samples/timer/timer_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/cschleiden/go-workflows/internal/tester"
7+
"github.com/google/uuid"
8+
"github.com/stretchr/testify/mock"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func Test_Workflow(t *testing.T) {
13+
tester := tester.NewWorkflowTester(Workflow1)
14+
15+
tester.OnActivity(Activity1, mock.Anything, 35, 12).Return(47, nil)
16+
17+
tester.Execute("Hello world" + uuid.NewString())
18+
19+
require.True(t, tester.WorkflowFinished())
20+
21+
var wr string
22+
var werr string
23+
tester.WorkflowResult(&wr, &werr)
24+
require.Equal(t, "result", wr)
25+
require.Empty(t, werr)
26+
tester.AssertExpectations(t)
27+
}

0 commit comments

Comments
 (0)