2023-04-15 13:23:49 +02:00
The one of the simplest way to check your bash/sh scripts is run it and check it output or run it and check the result. This tutorial shows how-to use [[https://github.com/pahaz/bashtest|bashtest]] tool for testing your scripts.
==== Write simple util ====
We have a simple **stat.sh** script:
2023-04-15 13:53:05 +02:00
<code>
2023-04-15 13:23:49 +02:00
#!/usr/bin/env bash
if [ -z "$1" ]
then
DIR=./
else
DIR=$1
fi
echo "Evaluate *.py statistics"
FILES=$(find $DIR -name '*.py' | wc -l)
LINES=$((find $DIR -name '*.py' -print0 | xargs -0 cat) | wc -l)
echo "PYTHON FILES: $FILES"
echo "PYTHON LINES: $LINES"
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
This script evaluate the number of python files and the number of python code lines in the files.
2023-04-15 13:53:05 +02:00
We can use it like **./stat.sh <dir>**
2023-04-15 13:23:49 +02:00
==== Create testsuit ====
Then make test suits for **stat.sh**. We make a directory **testsuit** which contain test python files.
**testsuit/main.py**
2023-04-15 13:53:05 +02:00
<code>
2023-04-15 13:23:49 +02:00
import foo
print(foo)
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
**testsuit/foo.py**
2023-04-15 13:53:05 +02:00
<code>
2023-04-15 13:23:49 +02:00
BAR = 1
BUZ = BAR + 2
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
Ok! Our test suit is ready!
We have 2 python files which contains 4 lines of code.
==== Write bashtests ====
Lets write tests. We just write a shell command for testing our work.
Create file **tests.bashtest**:
2023-04-15 13:53:05 +02:00
<code>
2023-04-15 13:23:49 +02:00
$ ./stat.sh testsuit/
Evaluate *.py statistics
PYTHON FILES: 2
PYTHON LINES: 4
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
This is our test! This is simple. Try to run it.
2023-04-15 13:53:05 +02:00
<code>
2023-04-15 13:23:49 +02:00
# install bashtest if required!
$ pip install bashtest
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
2023-04-15 13:53:05 +02:00
<code>
2023-04-15 13:23:49 +02:00
# run tests
$ bashtest *.bashtest
1 items passed all tests:
1 tests in tests.bashtest
1 tests in 1 items.
1 passed and 0 failed.
Test passed.
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
Thats all. We wrote one test. You can write more tests if you want.
2023-04-15 13:53:05 +02:00
<code>
2023-04-15 13:23:49 +02:00
$ ls testsuit/
foo.py main.py
$ ./stat.sh testsuit/
Evaluate *.py statistics
PYTHON FILES: 2
PYTHON LINES: 4
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
And run tests again:
2023-04-15 13:53:05 +02:00
<code>
2023-04-15 13:23:49 +02:00
$ bashtest *.bashtest
1 items passed all tests:
2 tests in tests.bashtest
2 tests in 1 items.
2 passed and 0 failed.
Test passed.
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
You can find more **.bashtest** examples in the [[https://github.com/pahaz/bashtest | bashtest github repo]]. You can also write your question or report a bug [[https://github.com/pahaz/bashtest/issues | here]].
Happy testing!