Testing a program in bash

I wrote a program in C ++ and now I have a binary file. I also created a bunch of tests to test. Now I want to automate the testing process using bash. I want to save three things in one execution of my binary:

  • lead time
  • exit code
  • program output

Right now I am adding up with a script that only checks the binary file and returns 0 and does not save the information I mentioned above. My script is as follows

#!/bin/bash

if [ "$#" -ne 2 ]; then
    echo "Usage: testScript <binary> <dir_with_tests>"
    exit 1
fi

binary="$1"
testsDir="$2"

for test in $(find $testsDir -name '*.txt'); do
    testname=$(basename $test)

    encodedTmp=$(mktemp /tmp/encoded_$testname)
    decodedTmp=$(mktemp /tmp/decoded_$testname)

    printf 'testing on %s...\n' "$testname"

    if ! "$binary" -c -f $test -o $encodedTmp > /dev/null; then
        echo 'encoder failed'
        rm "$encodedTmp"
        rm "$decodedTmp"
        continue
    fi

    if ! "$binary" -u -f $encodedTmp -o $decodedTmp > /dev/null; then
        echo 'decoder failed'
        rm "$encodedTmp"
        rm "$decodedTmp"
        continue
    fi

    if ! diff "$test" "$decodedTmp" > /dev/null ; then
        echo "result differs with input"
    else
        echo "$testname passed"
    fi

    rm "$encodedTmp"
    rm "$decodedTmp"
done

I want to save the output $binaryin a variable and not send it to /dev/null. I also want to save time using timebash function

+4
source share
3 answers

, - () ( ).

if ! "$binary" -c -f $test -o $encodedTmp > /dev/null; then

if ! output=$("$binary" -c -f $test -o $encodedTmp); then

$binary . ( ) , if , $binary .

, echo "$output".

, , , , :

if ! time-output=$(time "$binary" -c -f $test -o $encodedTmp) 2>&1); then

time stderr, , stderr stdout. , echo "$time-output", :

<program output>
<blank line>
real    0m0.041s
user    0m0.000s
sys     0m0.046s
+1

bash $? echo $?. , sth,

{ time sleep 1 ; } 2> time.txt

(time ls) > out.file 2>&1
+1

You can save the output to a file using output redirection. Just change the first line /dev/null:

if ! "$binary" -c -f $test -o $encodedTmp > /dev/null; then

to

if ! "$binary" -c -f $test -o $encodedTmp > prog_output; then

then change the second and third / dev / null lines accordingly:

if ! "$binary" -u -f $encodedTmp -o $decodedTmp >> prog_output; then

if ! diff "$test" "$decodedTmp" >> prog_output; then

To measure program execution, put

start=$(date +%s)

in the first line

then

end=$(date +%s)
echo "Execution time in seconds: " $((end-start)) >> prog_output

at the end.

+1
source

Source: https://habr.com/ru/post/1610922/


All Articles