Zum Hauptinhalt springen
Sichere ProgrammierungCloud Resume Challenge - Building a Cloud-Native Portfolio(19.09.2026 um 00:11 Uhr)
Sichere ProgrammierungSQL Joins Explained.(19.09.2026 um 00:25 Uhr)
IT Security VideoSecurity Weekly - A CRA Resource: Bots Gone Wild(19.09.2026 um 00:00 Uhr)
IT NachrichtenAzerbaijan Grand Prix: How to Watch the F1 Race Today(19.09.2026 um 00:00 Uhr)
Sichere ProgrammierungCloud Resume Challenge - Building a Cloud-Native Portfolio(19.09.2026 um 00:11 Uhr)
Sichere ProgrammierungSQL Joins Explained.(19.09.2026 um 00:25 Uhr)
IT Security VideoSecurity Weekly - A CRA Resource: Bots Gone Wild(19.09.2026 um 00:00 Uhr)
IT NachrichtenAzerbaijan Grand Prix: How to Watch the F1 Race Today(19.09.2026 um 00:00 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Building a Basic Testing Framework in Bash 🐚

In the world of software development, testing is crucial for ensuring that your code behaves as expected. In this article, we'll explore a simple yet effective Bash script that serves as a basic testing framework. This framework will help you track test results, log outcomes, and summarize the results of your tests. Let's dive in! 🚀

Overview of the Script

The script consists of several components that work together to facilitate testing:

  1. Variables for Tracking Test Results: These variables keep count of the total tests, passed tests, and failed tests.
  2. Logging Function: A function to log the results of each test.
  3. Assertion Functions: Functions to check for equality and command success.
  4. Summary Function: A function to display the overall results after running the tests.

Breakdown of the Script

1. Variables for Tracking Test Results

total_tests=0
passed_tests=0
failed_tests=0

These variables initialize counters for tracking how many tests have been run, how many have passed, and how many have failed.

2. Logging Function

log_result() {
    local status=$1
    local message=$2

    if [ "$status" -eq 0 ]; then
        echo "✅ PASS: $message"
        ((passed_tests++))
    else
        echo "❌ FAIL: $message"
        ((failed_tests++))
    fi
}

The log_result function takes a status code and a message. If the status is 0, it logs a pass; otherwise, it logs a failure and increments the respective counters.

3. Assertion Functions

  • Assert Equality
assert_equal() {
    local expected=$1
    local actual=$2
    local test_name=$3

    ((total_tests++))
    if [ "$expected" == "$actual" ]; then
        log_result 0 "$test_name"
    else
        log_result 1 "$test_name (Expected: '$expected', Got: '$actual')"
    fi
}

This function checks if two values are equal. It logs the result and updates the total test count.

  • Assert Command Success
assert_command_success() {
    local command="$1"
    local test_name=$2

    ((total_tests++))
    eval "$command" &> /dev/null
    if [ $? -eq 0 ]; then
        log_result 0 "$test_name"
    else
        log_result 1 "$test_name (Command failed)"
    fi
}

This function runs a command and checks its exit status. If the command succeeds, it logs a pass; otherwise, it logs a failure.

4. Summary Function

summary() {
    echo
    echo "Total Tests: $total_tests"
    echo "Passed: $passed_tests"
    echo "Failed: $failed_tests"

    if [ "$failed_tests" -eq 0 ]; then
        echo "🎉 All tests passed!"
    else
        echo "⚠️ Some tests failed."
    fi
}

After all tests are executed, this function summarizes the results, indicating how many tests passed or failed.

Example Usage

Below is an example of how to use this testing framework:

echo "Running Tests..."
assert_equal "hello" "hello" "Check if strings are equal"
assert_equal "1" "2" "Check if integers are equal"
assert_command_success "ls /" "Check if 'ls /' command succeeds"
assert_command_success "invalid_command" "Check if invalid command fails"

# Display the summary
summary

In this example:

  • We check string equality.
  • We validate integer equality (which will fail).
  • We run a valid command (ls /) and an invalid command (invalid_command).

Conclusion 🎊

This basic testing framework in Bash provides a simple way to automate your testing process. By using functions to assert conditions and log results, you can easily track the health of your scripts. Feel free to expand upon this framework by adding more complex assertions or incorporating additional features as needed!

Happy scripting! 🖥️✨

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Building a Basic Testing Framework in Bash 🐚

Thematisch verwandte Begriffe: Building, Basic, Testing, Framework · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-61591 | djust provides Phoenix LiveView-style reactive server-side rendering for…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Rechts: Artikel Ziehen Links: RSS
Hoch: nächster Artikel Runter: zurück / schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Rechts: Original Links: RSS-Ansicht
↗ Original-Quelle
Social Reaktionen Stimme abgeben (+5 Karma)
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick