OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Testing

Testing is an important step in every kind of software development, including scripts. There is a saying in the open-source world, “release early, release often,” which reflects this fact. By releasing early and often, software gets more exposure to use and testing.


Experience has shown that bugs are much easier to find, and much less expensive to fix, if they are found early in the development cycle.

In Chapter 26, we saw how stubs can be used to verify program flow. From the earliest stages of script development, they are a valuable technique to check the progress of our work.

Let’s look at the file-deletion problem above and see how this could be coded for easy testing. Testing the original fragment of code would be dangerous, since its purpose is to delete files, but we could modify the code to make the test safe:



if [[ -d $dir_name ]]; then if cd $dir_name; then

echo rm * # TESTING

else

echo "cannot cd to '$dir_name'" >&2 exit 1

fi else

echo "no such directory: '$dir_name'" >&2 exit 1

fi

exit # TESTING

if [[ -d $dir_name ]]; then if cd $dir_name; then

echo rm * # TESTING

else

echo "cannot cd to '$dir_name'" >&2 exit 1

fi else

echo "no such directory: '$dir_name'" >&2 exit 1

fi

exit # TESTING


Since the error conditions already output useful messages, we don't have to add any. The most important change is placing an echo command just before the rm command to al- low the command and its expanded argument list to be displayed, rather than the com- mand actually being executed. This change allows safe execution of the code. At the end of the code fragment, we place an exit command to conclude the test and prevent any other part of the script from being carried out. The need for this will vary according to the design of the script.

We also include some comments that act as “markers” for our test-related changes. These can be used to help find and remove the changes when testing is complete.


 

Top OS Cloud Computing at OnWorks: