Thursday, October 16, 2014
Unit Testing Bash Scripts
Although I have written a lot of shell scripts the past year, I did not write a single unit test to verify my code. In Python, I have unittest module. In Java, I have JUnit for that... it seems I don't have any tool for bash scripts. Inspired by haridsv in http://stackoverflow.com/questions/971945/unit-testing-for-shell-scripts, I have achieved something that looks quite promising without installing extra lib, but repeating few lines...
Task: Run a test.sh script to change the content of the /etc/ssh/sshd_config
How can we test this script? We put everything into functions and test the functions!
We are going to put the functions into a file with the same name + "_functions"
The last declare, grep and while statement will automatically run every functions starting with "test_".
You will get the line number too if it fails.
You can also put setUp() or tearDown() feature into the while loop before/after the eval.
To run this test, just run ./test_functions.sh
Hope that helps.
*EDIT: add ERROR or the eval will return 0 even it failed the middle tests.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment