TEST-INFRA: [test-architecture] Refactor step definitions to reduce code duplication #3103

Open
opened 2026-04-05 06:14:41 +00:00 by freemo · 1 comment
Owner

Metadata

  • Branch: refactor/test-arch-step-definitions-dry-refactor
  • Commit Message: refactor(tests): extract shared step helpers and reduce code duplication in Behave step definitions
  • Milestone: (backlog — see note below)
  • Parent Epic: #1678

Background and Context

During a review of the test architecture, a recurring pattern of duplicated code was identified in the Behave step definitions. This duplication violates the DRY (Don't Repeat Yourself) principle and makes the test suite more difficult to maintain.

Examples of duplicated code include:

  • Error handling: Many steps have nearly identical code for handling common exceptions like ValidationError, CleverAgentsError, and NotFoundError.
  • CLI invocation: Steps that invoke CLI commands often have very similar code for setting up arguments and running the command.
  • Mocking: The setup for mocking services is repeated across multiple given steps.

This duplication means that if the underlying code changes (e.g., an error message is updated, or a CLI argument is renamed), the same change needs to be made in multiple places in the test suite. This is inefficient and error-prone.

Proposal

To address this issue, we propose refactoring the step definitions to reduce code duplication. This will involve the following:

  • Create shared step definitions: Identify common patterns in the step definitions and create shared, reusable steps. For example, a single step could be created for handling all types of expected exceptions.
  • Use helper functions: Create helper functions to encapsulate common tasks, such as invoking the CLI or setting up mocks. All mocking code must remain in features/mocks/ per CONTRIBUTING.md.
  • Refactor existing step definitions: Refactor the existing step definitions to use the new shared steps and helper functions.

By making these changes, we can make the test suite more maintainable, easier to read, and less prone to errors.

Subtasks

  • Identify common patterns of code duplication in the step definitions.
  • Create a new module for shared step definitions and helper functions.
  • Implement shared steps for common tasks like error handling and CLI invocation.
  • Implement helper functions to reduce boilerplate code (mocking helpers in features/mocks/).
  • Refactor the existing step definitions to use the new shared steps and helper functions.
  • Run all tests to ensure that the refactoring has not introduced any regressions.

Definition of Done

  • All subtasks above are completed and checked off.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation.
  • The commit is pushed to the remote on the branch matching the Branch in Metadata exactly.
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.
  • All nox stages pass
  • Coverage >= 97%

Backlog note: This issue was discovered during autonomous operation
on milestone v3.2.0. It does not block milestone completion and has been
placed in the backlog for human review and future milestone assignment.


Automated by CleverAgents Bot
Supervisor: Test Infrastructure | Agent: ca-new-issue-creator

## Metadata - **Branch**: `refactor/test-arch-step-definitions-dry-refactor` - **Commit Message**: `refactor(tests): extract shared step helpers and reduce code duplication in Behave step definitions` - **Milestone**: *(backlog — see note below)* - **Parent Epic**: #1678 ## Background and Context During a review of the test architecture, a recurring pattern of duplicated code was identified in the Behave step definitions. This duplication violates the DRY (Don't Repeat Yourself) principle and makes the test suite more difficult to maintain. Examples of duplicated code include: * **Error handling:** Many steps have nearly identical code for handling common exceptions like `ValidationError`, `CleverAgentsError`, and `NotFoundError`. * **CLI invocation:** Steps that invoke CLI commands often have very similar code for setting up arguments and running the command. * **Mocking:** The setup for mocking services is repeated across multiple `given` steps. This duplication means that if the underlying code changes (e.g., an error message is updated, or a CLI argument is renamed), the same change needs to be made in multiple places in the test suite. This is inefficient and error-prone. ## Proposal To address this issue, we propose refactoring the step definitions to reduce code duplication. This will involve the following: * **Create shared step definitions:** Identify common patterns in the step definitions and create shared, reusable steps. For example, a single step could be created for handling all types of expected exceptions. * **Use helper functions:** Create helper functions to encapsulate common tasks, such as invoking the CLI or setting up mocks. All mocking code must remain in `features/mocks/` per CONTRIBUTING.md. * **Refactor existing step definitions:** Refactor the existing step definitions to use the new shared steps and helper functions. By making these changes, we can make the test suite more maintainable, easier to read, and less prone to errors. ## Subtasks - [ ] Identify common patterns of code duplication in the step definitions. - [ ] Create a new module for shared step definitions and helper functions. - [ ] Implement shared steps for common tasks like error handling and CLI invocation. - [ ] Implement helper functions to reduce boilerplate code (mocking helpers in `features/mocks/`). - [ ] Refactor the existing step definitions to use the new shared steps and helper functions. - [ ] Run all tests to ensure that the refactoring has not introduced any regressions. ## Definition of Done - [ ] All subtasks above are completed and checked off. - [ ] A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation. - [ ] The commit is pushed to the remote on the branch matching the **Branch** in Metadata exactly. - [ ] The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done. - [ ] All nox stages pass - [ ] Coverage >= 97% > **Backlog note:** This issue was discovered during autonomous operation > on milestone v3.2.0. It does not block milestone completion and has been > placed in the backlog for human review and future milestone assignment. --- **Automated by CleverAgents Bot** Supervisor: Test Infrastructure | Agent: ca-new-issue-creator
freemo added this to the v3.8.0 milestone 2026-04-05 06:32:14 +00:00
Author
Owner

Issue triaged by project owner:

  • State: Verified
  • Priority: Backlog — test infrastructure refactoring; does not block any milestone deliverable
  • Milestone: v3.8.0
  • MoSCoW: Could Have — reducing code duplication in step definitions is a maintenance improvement. While it would improve long-term maintainability, the current test suite is functional and meets coverage requirements. This is refactoring with no behavior change.
  • Parent Epic: #1678

Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: ca-project-owner

Issue triaged by project owner: - **State**: Verified - **Priority**: Backlog — test infrastructure refactoring; does not block any milestone deliverable - **Milestone**: v3.8.0 - **MoSCoW**: Could Have — reducing code duplication in step definitions is a maintenance improvement. While it would improve long-term maintainability, the current test suite is functional and meets coverage requirements. This is refactoring with no behavior change. - **Parent Epic**: #1678 --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: ca-project-owner
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
cleveragents/cleveragents-core#3103
No description provided.