elsa-core/doc/qa/test-guidelines.md

262 lines
17 KiB
Markdown
Raw Normal View History

# Elsa Core — Testing Strategy
2025-09-26 09:38:38 +00:00
## Purpose
2025-10-02 10:08:44 +00:00
This document is a practical test guideline. It tells you *what* to test, *when* to test it, and *how* to write deterministic, actionable tests using the repository's existing test helpers and patterns.
2025-09-26 09:38:38 +00:00
---
## Summary
The philosophy of testing in Elsa can be summarized as:
***Whenever a test fails, it should provide a clear direction towards the cause of the problem.***
Tests should be fast, deterministic, and precise: they should pinpoint the failing subsystem (activity, invoker, persistence, scheduler, etc.) with minimal noise.
For contributors, tests are the first line of code review: they must document intended behaviour and prevent regressions.
2025-09-26 09:38:38 +00:00
---
2025-10-13 14:28:33 +00:00
## Glossary
- **Activity** — a single unit of workflow logic (e.g., WriteLine, If, ForEach, HttpRequest).
- **Workflow** — a graph of activities connected by control flow.
- **Workflow Definition** — a serializable representation of a workflow (JSON or code).
- **Workflow Instance** — a persisted execution of a workflow definition, including state, variables, and journal.
- **Bookmark** — a pause point in a workflow where execution is suspended until an external event resumes it.
- **Invoker** — the core engine component that orchestrates workflow execution, including activity execution, scheduling, and state transitions.
- **Scheduler** — the subsystem that manages background tasks, timers, and resumption of workflows.
- **Journal** — a log of all activity executions and state changes in a workflow instance.
- **Persistence** — the storage mechanism for workflow definitions and instances (e.g., EF Core, MongoDB).
- **Unit Test** — a test that verifies a small, isolated piece of code (such as a function or method) works as expected.
- **Integration Test** — a test that verifies the interaction between multiple components or subsystems works as expected.
- **Component Test** — a test that verifies the behavior of a larger part of the system, often involving persistence and external dependencies.
---
## High-level testing pyramid
2025-09-26 09:38:38 +00:00
- **Unit tests** — single-class logic (activities, converters, expression evaluators, serializers, service providers). Fast; no persistence.
- **Integration tests** — multiple Elsa subsystems together (e.g., invoker + activities + registries). In-process; may deserialize workflow JSON. Use [`IWorkflowRunner.RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs) and [`PopulateRegistriesAsync()`](../../src/common/Elsa.Testing.Shared.Integration/ServiceProviderExtensions.cs) when using existing definitions.
- **Component tests** — persisted behaviour, journal/instance store assertions, bookmarks/resumption across lifecycle boundaries. Use [`AppComponentTest`](../../test/component/Elsa.Workflows.ComponentTests/Helpers/Abstractions/AppComponentTest.cs) to instantiate and [`IWorkflowInstanceStore`](../../src/modules/Elsa.Workflows.Management/Contracts/IWorkflowInstanceStore.cs) queries for assertions.
2025-09-26 09:38:38 +00:00
Each test layer has distinct goals and clear boundaries — see [**Which parts of Elsa to test**](#which-parts-of-elsa-to-test-and-which-test-types-to-use) for precise mapping of which aspects belong to which layer.
---
## Quick Start for Contributors
2025-09-26 09:38:38 +00:00
2025-10-13 14:28:33 +00:00
**Before you writ e a test:**
1. ✅ Understand what you're testing (see [**Which parts of Elsa to test**](#which-parts-of-elsa-to-test-and-which-test-types-to-use))
2. ✅ Choose the right test layer (unit vs integration vs component)
3. ✅ Use existing helpers (don't reinvent - see [**Test Helpers Reference**](#test-helpers-reference-quick-lookup))
2025-09-26 09:38:38 +00:00
**5-Minute Checklist:**
- [ ] Read the relevant section below for your change type:
2025-10-09 14:55:12 +00:00
- Changed activity logic or created a new activity? → See [Activities](#activities)
- Changed workflow execution? → See [Workflows execution](#workflow-execution-invoker-middleware-bookmarks)
- Changed persistence? → See [Persistence & serialization](#persistence--serialization)
- [ ] Follow steps and code patterns in that section
- [ ] Run tests locally: `dotnet test`
- [ ] Verify no flaky behavior (run 10 times: `dotnet test --no-build -- repeat 10`)
2025-09-26 09:38:38 +00:00
---
2025-09-26 09:38:38 +00:00
## Characteristics for testing
2025-09-26 09:38:38 +00:00
2025-10-13 14:28:33 +00:00
- **Activities:** First-class pluggable units. Each activity implements execution logic and interacts with the [`ActivityExecutionContext`](../../src/modules/Elsa.Workflows.Core/Contexts/ActivityExecutionContext.cs). Many activity tests in the repository use [`RunActivityAsync`](../../src/common/Elsa.Testing.Shared.Integration/RunActivityExtensions.cs) to create the required context and invoke the activity inline. More details in [**Activities**](#activities).
2025-09-26 09:38:38 +00:00
2025-10-13 14:28:33 +00:00
- **Workflows:** Graphs of activities. A workflow can run synchronously or schedule asynchronous work (bookmarks, timers). When you run a workflow in-process with [`IWorkflowRunner.RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs), the runner will return when synchronous work completes. Some activities set `RunAsynchronously` causing background scheduling — tests need to take care when asserting. More details in [**Workflow execution**](#workflow-execution-invoker-middleware-bookmarks).
2025-09-26 09:38:38 +00:00
---
## Which parts of Elsa to test, and which test types to use
2025-09-26 09:38:38 +00:00
This section maps Elsa aspects to the exact kinds of tests you should write, with examples and code patterns referencing repository conventions.
2025-09-26 09:38:38 +00:00
### Activities
2025-09-26 09:38:38 +00:00
#### **Unit tests:**
- Test the activity class logic only (no persistence, no scheduler). Cover configuration permutations and boundary inputs.
- Use [`TestApplicationBuilder`](../../src/common/Elsa.Testing.Shared.Integration/TestApplicationBuilder.cs) + [`RunActivityAsync`](../../src/common/Elsa.Testing.Shared.Integration/RunActivityExtensions.cs) to obtain an [`ActivityExecutionContext`](../../src/modules/Elsa.Workflows.Core/Contexts/ActivityExecutionContext.cs) and run the activity.
2025-09-26 09:38:38 +00:00
**Example:**
2025-09-26 09:38:38 +00:00
```csharp
// Arrange
var serviceProvider = new TestApplicationBuilder(testOutputHelper)
.WithCapturingTextWriter(capturingTextWriter)
.Build();
// Act
var writeLine = new WriteLine("Hello world!");
await serviceProvider.RunActivityAsync(writeLine);
2025-09-26 09:38:38 +00:00
// Assert
Assert.Equal("Hello world!", capturingTextWriter.Lines.Single());
2025-09-26 09:38:38 +00:00
```
2025-10-13 14:28:33 +00:00
#### **Integration tests:**
- Place the activity inside a minimal workflow definition and run via [`IWorkflowRunner.RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs). Assert outputs/variables and that the activity integrates correctly with preceding/following activities.
- If activity creates bookmarks or relies on scheduler semantics, integration tests should resume bookmarks via the engine APIs to validate resumption.
2025-09-26 09:38:38 +00:00
Pattern note: [`RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs) returns a [`RunWorkflowResult`](../../src/modules/Elsa.Workflows.Core/Models/RunWorkflowResult.cs) (or equivalent) containing the [`WorkflowInstance`](../../src/modules/Elsa.Workflows.Management/Entities/WorkflowInstance.cs) and output variables when run to completion.
Use returned state for deterministic assertions where possible.
2025-09-26 09:38:38 +00:00
### Workflow execution (invoker, middleware, bookmarks)
2025-09-26 09:38:38 +00:00
#### Unit tests:
- Rare: low-level pure helpers in the invoker may have unit tests for edge cases. Most invoker behavior requires integration testing.
2025-09-26 09:38:38 +00:00
#### Integration tests:
- Use [`IWorkflowRunner.RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs) with small workflows to test variables propagation, branch logic (If/ForEach/Parallel), expression evaluation, and `RunAsynchronously` flags.
- When a workflow schedules async child work (bookmarks), simulate resumption by calling resume APIs.
2025-09-26 09:38:38 +00:00
Call the workflow runner to execute a workflow object or a loaded definition. Prefer this when asserting logical flow and outputs.
2025-09-26 09:38:38 +00:00
```csharp
var runner = serviceProvider.GetRequiredService<IWorkflowRunner>();
await serviceProvider.PopulateRegistriesAsync();
var runResult = await runner.RunAsync(workflow);
Assert.Equal(WorkflowStatus.Finished, runResult.WorkflowInstance!.Status);
```
2025-09-26 09:38:38 +00:00
#### Component tests (persistence & resumption):
- Start a workflow that creates a bookmark. Persisted instance must be found via [`IWorkflowInstanceStore`](../../src/modules/Elsa.Workflows.Management/Contracts/IWorkflowInstanceStore.cs) after the creation point. Simulate host restart by disposing and rebuilding the service provider (keeping the same persistence store) and resume the bookmark to assert resumption completes.
2025-09-26 09:38:38 +00:00
**Code pattern to resume a bookmark (integration/component):**
2025-09-26 09:38:38 +00:00
```csharp
// assume instanceId found via RunAsync or correlation id
await workflowTriggerService.ResumeAsync(instanceId, activityId, input, CancellationToken.None);
var resumed = await runner.RunAsync(workflowInstance);
Assert.Equal(WorkflowStatus.Finished, resumed.WorkflowInstance.Status);
```
2025-09-26 09:38:38 +00:00
---
2025-09-26 09:38:38 +00:00
## Test Helpers Reference (Quick Lookup)
2025-09-26 13:16:08 +00:00
2025-10-13 14:28:33 +00:00
| Helper | Purpose | Use When |
|--------|------------------------------------------------------------------------------|-----------------------------------------------------------|
| `TestApplicationBuilder` | Build test service provider | All tests (entry point) |
| `RunActivityAsync` | Run single activity | Unit testing activities |
| `IWorkflowRunner.RunAsync` | Execute workflow in-process | Integration / Component tests |
| `PopulateRegistriesAsync` | Register types for JSON deserialization. | Loading JSON workflows. <br/>Integration tests only |
| `IWorkflowInstanceStore` | Query persisted instances | Component tests (persistence) |
| `RunWorkflowUntilEndAsync` | Drive workflow to completion. | Complex resumption scenarios. <br/>Integration tests only |
2025-09-26 09:38:38 +00:00
---
2025-09-26 09:38:38 +00:00
## Decision helper (what to add — follow in order)
2025-09-26 09:38:38 +00:00
1. **Changed code is a single activity class with no persistence/external calls?** → Unit test only.
2025-10-13 14:28:33 +00:00
2. **Change touches multiple activities or workflow logic (If, ForEach, Parallel, Flow activities, etc.)?** → Integration test using [`IWorkflowRunner.RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs) and a small workflow.
3. **Change touches invoker/scheduler/bookmarks or similar multi-component feature?** → Integration test using [`IWorkflowRunner.RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs) and a small workflow. If persistence semantics change, add component tests.
4. **Change touches persistence/serializers or requires durable evidence (journal, bookmarks)?** → Component tests against [`IWorkflowInstanceStore`](../../src/modules/Elsa.Workflows.Management/Contracts/IWorkflowInstanceStore.cs).
**Rule of thumb:**
- If its about **internal logic**, write a **unit** test.
- If its about **collaboration between components**, write an **integration** test.
- If its about **end-to-end feature behavior**, write a **component** test
2025-09-26 09:38:38 +00:00
When in doubt, add the minimal unit tests plus one integration test that reproduces the scenario.
2025-09-26 09:38:38 +00:00
---
2025-09-26 09:38:38 +00:00
## Deterministic patterns to avoid flaky tests
2025-09-26 09:38:38 +00:00
1. **Prefer returned state from [`RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs).** Always inspect [`RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs) results first — it is deterministic for synchronous workflows.
2. **Resume bookmarks explicitly.** Do not wait for external schedulers — call the engine's resume/trigger APIs in your test to continue execution.
3. **Locate instances deterministically.** Use an instance id returned by [`RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs) or attach a `CorrelationId` test variable and query [`IWorkflowInstanceStore.FindByCorrelationIdAsync(...)`](../../src/modules/Elsa.Workflows.Management/Contracts/IWorkflowInstanceStore.cs). Avoid using "latest" queries.
4. **Use short polling where necessary.** If you must poll the instance store (e.g., testing asynchronous controllers), use a short interval and a deterministic timeout (helper code snippets in examples above).
2025-09-26 09:38:38 +00:00
---
2025-09-26 09:38:38 +00:00
## Failure testing (faults & incidents)
- **Integration test faulted workflows**: build a workflow that throws and run via [`RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs) — assert [`WorkflowInstance.Status`](../../src/modules/Elsa.Workflows.Management/Entities/WorkflowInstance.cs) == [`Faulted`](../../src/modules/Elsa.Workflows.Core/Enums/WorkflowStatus.cs) on the returned state or via [`IWorkflowInstanceStore`](../../src/modules/Elsa.Workflows.Management/Contracts/IWorkflowInstanceStore.cs).
- **Component tests for recovery/resume**: persist a faulted instance (or cause a host restart scenario), run your recovery logic, and assert the final state.
2025-09-26 09:38:38 +00:00
---
2025-09-26 09:38:38 +00:00
## Practical test recipes & snippets (copy/paste-ready)
2025-09-26 09:38:38 +00:00
### Unit test (activity) — pattern
2025-09-26 09:38:38 +00:00
```csharp
[Fact]
public async Task MyActivity_WritesExpectedOutput()
{
var sp = new TestApplicationBuilder(testOutput).Build();
var activity = new MyActivity { Input = "x" };
2025-09-26 09:38:38 +00:00
await sp.RunActivityAsync(activity);
2025-09-26 09:38:38 +00:00
// assert behavior of activity in isolation
}
```
2025-09-26 09:38:38 +00:00
### Integration test — pattern using [`IWorkflowRunner.RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs)
2025-09-26 09:38:38 +00:00
```csharp
[Fact]
public async Task Workflow_With_MyActivity_Completes()
{
var sp = new TestApplicationBuilder(testOutput).Build();
await sp.PopulateRegistriesAsync();
2025-09-26 09:38:38 +00:00
var runner = sp.GetRequiredService<IWorkflowRunner>();
var workflow = new MyWorkflowDefinition();
2025-09-26 09:38:38 +00:00
var result = await runner.RunAsync(workflow);
2025-09-26 09:38:38 +00:00
Assert.Equal(WorkflowStatus.Finished, result.WorkflowInstance!.Status);
}
```
2025-09-26 09:38:38 +00:00
### Component test — pattern asserting persisted state
2025-09-26 09:38:38 +00:00
```csharp
[Fact]
public async Task Workflow_Persists_Instance_And_Journal()
{
var sp = new TestApplicationBuilder(testOutput)
.Build();
2025-09-26 09:38:38 +00:00
var runner = sp.GetRequiredService<AsyncWorkflowRunner>();
var result = await runner.RunAndAwaitWorkflowCompletionAsync(WorkflowDefinitionHandle.ByDefinitionId(someDefinitionId, VersionOptions.Published));
result.WorkflowExecutionContext.Status.Should().Be(WorkflowStatus.Finished);
}
```
2025-09-26 09:38:38 +00:00
---
## FAQ (quick pointers)
2025-09-26 09:38:38 +00:00
2025-10-13 14:28:33 +00:00
**Q: How do I import workflow definitions in tests and where do I put the workflow definitions?**
A: For JSON-defined workflows use the repo's test integration helpers ([`PopulateRegistriesAsync()`](../../src/common/Elsa.Testing.Shared.Integration/ServiceProviderExtensions.cs) or the test registration helpers in `test/common`). See integration test examples in the test tree. Leave the definitions next to the tests that use them.
2025-09-26 09:38:38 +00:00
**Q: Which helper should I use to run a workflow?**
A: Prefer [`IWorkflowRunner.RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs) for in-process deterministic runs. For activities use [`RunActivityAsync`](../../src/common/Elsa.Testing.Shared.Integration/RunActivityExtensions.cs) via [`TestApplicationBuilder`](../../src/common/Elsa.Testing.Shared.Integration/TestApplicationBuilder.cs).
**Q: How do I check persisted journal entries?**
A: Query [`IWorkflowInstanceStore`](../../src/modules/Elsa.Workflows.Management/Contracts/IWorkflowInstanceStore.cs) and inspect the persisted journal on the instance. Use deterministic instance id or correlation id to locate the exact instance.
2025-09-26 09:38:38 +00:00
**Q: Do I need a new helper to wait for workflow completion?**
2025-10-13 14:28:33 +00:00
A: No. The repo provides [`RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs) and integration helpers that cover all necessary scenarios.
2025-09-26 09:38:38 +00:00
---
## Appendix — examples in the repository (where to look)
Search the `test/` tree for examples that follow the above patterns:
- Unit activity examples: `test/unit/*` (look for [`RunActivityAsync`](../../src/common/Elsa.Testing.Shared.Integration/RunActivityExtensions.cs) usage).
- Integration workflow examples: `test/integration/*` (look for [`PopulateRegistriesAsync()`](../../src/common/Elsa.Testing.Shared.Integration/ServiceProviderExtensions.cs) and [`IWorkflowRunner.RunAsync`](../../src/modules/Elsa.Workflows.Core/Contracts/IWorkflowRunner.cs) usage).
- Component scenarios exercising persistence: `test/component/*` (look for [`AppComponentTest`](../../test/component/Elsa.Workflows.ComponentTests/Helpers/Abstractions/AppComponentTest.cs) scaffolds and [`IWorkflowInstanceStore`](../../src/modules/Elsa.Workflows.Management/Contracts/IWorkflowInstanceStore.cs) assertions).
2025-09-26 09:38:38 +00:00