This is a little integration test for a legacy application:
set -o errexit # this tell bash to stop on any error
set -o xtrace # this tell bash to print any command before execution
# setup the database
mysql -u root test-database << EOF
DELETE FROM orders_to_be_exported;
INSERT INTO orders_to_be_exported(id) VALUES('1235');
DELETE FROM order;
INSERT INTO order(customer_name, amount) VALUES('andrea', '1234');
EOF
# clean up filesystem
rm /tmp/orders/exported-*.order -f
# start up the application
mvn jetty:run
curl -X POST http://localhost:8080/application/export-orders
test -f /tmp/exported-1235.order # this check if the file exists
# the following checks the contents of the file
diff /tmp/exported-1235.order - << EOF
Customer: Andrea
Amount: 1234 eur
EOFWhenever this script fail in any of its operation it stops with an non zero exit code. This is because the set -o errexit instruction. As you may known a non zero exit status often means that an error has been occured. Also Hudson knows this and when this happen it marks the build as broken.
The set -o xtrace instructions tell to bash to write each command before executing it.
- This script demonstrate clearly how powerfull could be the shell, you can
- invoke ReST web services (with curl)
- inspect the contents of a file (with diff)
- test for the existence of a file (with test -f), a directory (test -d), and many others facts about your filesystem
- interact with the database
They may be also useful when you work with legacy code. You may want to put some part of the system under unit test but can't do that before of many refactoring. You could create an integration test, refactor the code toward a better modularity and then place the unit test and, maybe, delete the integration test.