Stop test function after first assert fails#526
Merged
Chemaclass merged 4 commits intomainfrom Nov 30, 2025
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📚 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:
Previously, if
create_userreturned 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 failedstate::mark_assertion_failed_in_test()- marks that an assertion failedUpdated all assertion functions (
assert.sh): Every assertion now callsassert::guardat 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.shpublic API: Thebashunit::assertion_failedandbashunit::assertion_passedfunctions 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
CHANGELOG.mdto reflect the new feature or fix