Skip to content

Instantly share code, notes, and snippets.

@mtvbrianking
Last active November 25, 2024 14:25
Show Gist options
  • Save mtvbrianking/e64f9114306da09fd308246f9b0fcdda to your computer and use it in GitHub Desktop.
Save mtvbrianking/e64f9114306da09fd308246f9b0fcdda to your computer and use it in GitHub Desktop.
Bash Automated Testing System (BATS)
# 1. Download the backend proxy server
# 2. Install and server the proxy server
# 3. Run tests while pointing to the proxy localhost:3000
name: Frontend CI Workflow
on:
push:
branches:
- main
jobs:
start-mock-api:
runs-on: ubuntu-latest
steps:
- name: Check out repository for frontend
uses: actions/checkout@v2
- name: Set up Node.js for mock server
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Checkout the API proxy repo (ussd/api-proxy)
uses: actions/checkout@v2
with:
repository: ussd/api-proxy # github.com/ussd/api-proxy
token: ${{ secrets.GITHUB_TOKEN }} # Automatically authorized with GitHub token
path: api-proxy # Specify a sub-directory to check out the repo
- name: Install dependencies for API Proxy
run: |
cd api-proxy
npm install
- name: Start the API Proxy Server
run: |
cd api-proxy
node server.js &
# Starts the mock API server in the background
run-frontend-tests:
runs-on: ubuntu-latest
needs: start-mock-api # Ensures the mock API server is up before frontend tests run
steps:
- name: Check out frontend repository
uses: actions/checkout@v2
- name: Set up Node.js for frontend
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install frontend dependencies
run:
#!/usr/bin/env bats
@test "TC_1 Can exit" {
run bash script.sh <<< $'exit'
[ "$status" -eq 0 ]
[[ "$output" == *"Enter a number (or type 'exit' to quit):"* ]]
[[ "$output" == *"Exiting script."* ]]
}
@test "TC_2 Handles single input" {
run bash script.sh <<< $'1\nexit'
[ "$status" -eq 0 ]
[[ "$output" == *"Enter a number (or type 'exit' to quit):"* ]]
[[ "$output" == *"You entered: 1"* ]]
[[ "$output" == *"Exiting script."* ]]
}
@test "TC_3 Handles multiple inputs" {
run bash script.sh <<< $'John\n123\nexit'
[ "$status" -eq 0 ]
[[ "$output" == *"Enter a number (or type 'exit' to quit):"* ]]
[[ "$output" == *"You entered: John"* ]]
[[ "$output" == *"Enter a number (or type 'exit' to quit):"* ]]
[[ "$output" == *"You entered: 123"* ]]
[[ "$output" == *"Enter a number (or type 'exit' to quit):"* ]]
[[ "$output" == *"Exiting script."* ]]
echo "$output" | awk 'NR==1' >&3
echo "> John" >&3
echo "$output" | awk 'NR==2' >&3
echo "$output" | awk 'NR==3' >&3
echo "> 123" >&3
echo "$output" | awk 'NR==4' >&3
echo "$output" | awk 'NR==5' >&3
echo "> exit" >&3
echo "$output" | awk 'NR==6' >&3
}
#!/bin/bash
while true; do
echo "Enter a number (or type 'exit' to quit):"
read user_input
if [ "$user_input" == "exit" ]; then
echo "Exiting script."
break
fi
echo "You entered: $user_input"
done
@mtvbrianking
Copy link
Author

bash script.sh
bash script.sh <<< $'Hello\n\John\nexit'

# ---

bats script.bats
bats --verbose-run --show-output-of-passing-tests script.bats

@mtvbrianking
Copy link
Author

mtvbrianking commented Nov 24, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment