[PR #84] [MERGED] feat: add testing utils #102

Closed
opened 2026-02-16 02:16:35 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/run-llama/workflows-py/pull/84
Author: @AstraBert
Created: 9/8/2025
Status: Merged
Merged: 9/11/2025
Merged by: @masci

Base: mainHead: clelia/testing-utils


📝 Commits (5)

  • 09902aa feat: add testing utils
  • 374c6da fix: make python 3.9 happy
  • bac5d2e chore: removed run_test method on Workflow and send_test_event on WorkflowTestRunner, rename run_and_collect to run
  • f53587f chore: make the test runner an async context manager again
  • a2eef38 chore: removing the context manager

📊 Changes

3 files changed (+137 additions, -0 deletions)

View changed files

src/workflows/testing/__init__.py (+3 -0)
src/workflows/testing/runner.py (+79 -0)
tests/test_testing_utils.py (+55 -0)

📄 Description

With this PR, we add testing utils to workflows through an async context manager (WorkflowTestRunner).

Example:

import pytest

from workflows.events import StartEvent, StopEvent, Event, StepStateChanged, EventsQueueChanged
from workflows import Context, Workflow, step
from workflows.testing import WorkflowTestRunner

class SecondEvent(Event):
    greeting: str

class SimpleWf(Workflow):
    @step
    async def step_one(self, ev: StartEvent, ctx: Context) -> SecondEvent | None:
        async with ctx.store.edit_state() as state:
            state.test = "this is a test"
        ctx.write_event_to_stream(SecondEvent(greeting="hello"))
        return SecondEvent(greeting="hello")
    @step
    async def step_two(self, ev: SecondEvent, ctx: Context) -> StopEvent:
        async with ctx.store.edit_state() as state:
            state.hello = "hello"
        return StopEvent(result="done")

@pytest.mark.asyncio
async def test_testing_utils():
    wf = SimpleWf()
    async with wf.run_test() as test_runner:
        assert isinstance(test_runner, WorkflowTestRunner)
        collected, freqs, res = await test_runner.run_and_collect(start_event=StartEvent(message="hi"), exclude_events=[EventsQueueChanged])
        assert len(collected) == sum([freqs[k] for k in freqs])
        assert freqs.get(SecondEvent, 0) == 1
        assert freqs.get(StopEvent, 0) == 1
        assert freqs.get(StepStateChanged, 0) == len(collected) - (freqs.get(SecondEvent, 0) + freqs.get(StopEvent, 0))
        assert str(res) == "done"
        output_event = await test_runner.send_test_event(step="step_one", event=StartEvent(message="hello"), ctx=Context(wf))
        assert isinstance(output_event, SecondEvent)
        assert output_event.greeting == "hello"

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/run-llama/workflows-py/pull/84 **Author:** [@AstraBert](https://github.com/AstraBert) **Created:** 9/8/2025 **Status:** ✅ Merged **Merged:** 9/11/2025 **Merged by:** [@masci](https://github.com/masci) **Base:** `main` ← **Head:** `clelia/testing-utils` --- ### 📝 Commits (5) - [`09902aa`](https://github.com/run-llama/workflows-py/commit/09902aaa9fb0073bff9277ad41c3688b257cf277) feat: add testing utils - [`374c6da`](https://github.com/run-llama/workflows-py/commit/374c6da5d3b4dd3899ed6a22613eca275fbc8bbe) fix: make python 3.9 happy - [`bac5d2e`](https://github.com/run-llama/workflows-py/commit/bac5d2ee9e8ed8725fb47efc29ba58e73e9e94dd) chore: removed run_test method on Workflow and send_test_event on WorkflowTestRunner, rename run_and_collect to run - [`f53587f`](https://github.com/run-llama/workflows-py/commit/f53587fb3cea240a51133472df62cbb1c493f40f) chore: make the test runner an async context manager again - [`a2eef38`](https://github.com/run-llama/workflows-py/commit/a2eef3896f471a59e0018a1b40d2f7cea86e569e) chore: removing the context manager ### 📊 Changes **3 files changed** (+137 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `src/workflows/testing/__init__.py` (+3 -0) ➕ `src/workflows/testing/runner.py` (+79 -0) ➕ `tests/test_testing_utils.py` (+55 -0) </details> ### 📄 Description With this PR, we add testing utils to workflows through an async context manager (`WorkflowTestRunner`). Example: ```python import pytest from workflows.events import StartEvent, StopEvent, Event, StepStateChanged, EventsQueueChanged from workflows import Context, Workflow, step from workflows.testing import WorkflowTestRunner class SecondEvent(Event): greeting: str class SimpleWf(Workflow): @step async def step_one(self, ev: StartEvent, ctx: Context) -> SecondEvent | None: async with ctx.store.edit_state() as state: state.test = "this is a test" ctx.write_event_to_stream(SecondEvent(greeting="hello")) return SecondEvent(greeting="hello") @step async def step_two(self, ev: SecondEvent, ctx: Context) -> StopEvent: async with ctx.store.edit_state() as state: state.hello = "hello" return StopEvent(result="done") @pytest.mark.asyncio async def test_testing_utils(): wf = SimpleWf() async with wf.run_test() as test_runner: assert isinstance(test_runner, WorkflowTestRunner) collected, freqs, res = await test_runner.run_and_collect(start_event=StartEvent(message="hi"), exclude_events=[EventsQueueChanged]) assert len(collected) == sum([freqs[k] for k in freqs]) assert freqs.get(SecondEvent, 0) == 1 assert freqs.get(StopEvent, 0) == 1 assert freqs.get(StepStateChanged, 0) == len(collected) - (freqs.get(SecondEvent, 0) + freqs.get(StopEvent, 0)) assert str(res) == "done" output_event = await test_runner.send_test_event(step="step_one", event=StartEvent(message="hello"), ctx=Context(wf)) assert isinstance(output_event, SecondEvent) assert output_event.greeting == "hello" ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 02:16:35 -05:00
yindo closed this issue 2026-02-16 02:16:35 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: run-llama/workflows-py#102