Skip to content

Stop test function after first assert fails#526

Merged
Chemaclass merged 4 commits intomainfrom
feat/492-stop-func-after-first-assesrt-fails
Nov 30, 2025
Merged

Stop test function after first assert fails#526
Chemaclass merged 4 commits intomainfrom
feat/492-stop-func-after-first-assesrt-fails

Conversation

@Chemaclass
Copy link
Copy Markdown
Member

@Chemaclass Chemaclass commented Nov 30, 2025

📚 Description

Closes #492

This PR implements a fundamental change to how bashunit handles multiple assertion failures within a single test function. Previously, when a test had multiple assertions and one failed, all subsequent assertions would still execute and report failures. This created noisy output and didn't match the behavior of established testing frameworks.

The Problem:

Consider a test like this:

function test_user_creation() {
  local user=$(create_user "John")
  
  assert_not_empty "$user"           # If this fails...
  assert_equals "John" "$user.name"  # ...this still runs and fails too
  assert_equals "active" "$user.status"  # ...and so does this
}

Previously, if create_user returned empty, you'd get 3 separate failure messages, making it harder to identify the root cause. The first failure is the important one; the rest are just noise caused by the same underlying issue.

The Solution:

Now bashunit stops executing assertions after the first failure within a test function—matching the default behavior of PHPUnit, Jest, and most other testing frameworks. The test above would now report only the first failure (assert_not_empty), making it immediately clear what went wrong.

Other test functions continue to run normally, so you still get full visibility into which tests pass/fail across your test suite.

🔖 Changes

  • Added assertion guard mechanism (assert::guard): A guard function that checks if a previous assertion in the current test has already failed. If so, subsequent assertions return early without executing.

  • Added state tracking (state.sh):

    • state::is_assertion_failed_in_test() - checks if an assertion has failed
    • state::mark_assertion_failed_in_test() - marks that an assertion failed
  • Updated all assertion functions (assert.sh): Every assertion now calls assert::guard at the start. For exit code assertions (assert_exit_code, assert_successful_code, etc.), $? is captured before the guard check to preserve the exit status.

  • Updated helper functions (assert::mark_failed): Centralized the logic for marking an assertion as failed, which both increments the failure counter and sets the guard flag.

  • Updated bashunit.sh public API: The bashunit::assertion_failed and bashunit::assertion_passed functions now also respect the guard, ensuring custom assertions built on top of bashunit's API get the same behavior.

  • Updated snapshots: Test snapshots reflect the reduced number of assertion failures reported when tests have multiple failing assertions.

✅ To-do list

  • I updated the CHANGELOG.md to reflect the new feature or fix
  • I updated the documentation to reflect the changes

@Chemaclass Chemaclass added the enhancement New feature or request label Nov 30, 2025
@Chemaclass Chemaclass self-assigned this Nov 30, 2025
@Chemaclass Chemaclass changed the title Stop func after first assesrt fails Stop test function after first assert fails Nov 30, 2025
@TypedDevs TypedDevs deleted a comment from chatgpt-codex-connector Bot Nov 30, 2025
@Chemaclass Chemaclass merged commit c92a582 into main Nov 30, 2025
15 checks passed
@Chemaclass Chemaclass deleted the feat/492-stop-func-after-first-assesrt-fails branch November 30, 2025 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stop test function afterfirst assertion fails

1 participant