Wednesday, November 24, 2010

UNIX shell Scripting

One of the main part of the SDLC cycle is Testing. The Shell, which is a part of UNIX, can help to do the testing smartly and quickly. Here we will discuss some of the advantages of UNIX shell scripting for test automation. some shell commands for automatic testing. The how to port the testing scripts to windows.

Introduction to UNIX

UNIX is the most popular among various operating systems; it has so many advantages like.

Multitasking: UNIX is designed to do many things at the same time. in computing, multitasking is a method by which multiple tasks or processes share common processing resources. In a computer with a single CPU, only one task is said to be running at any point of time, meaning that the CPU is executing instructions for one task. Multitasking solves the problem by scheduling. Like spooling to the printer of one file and editing of other file. This is important for users as they don't need to wait for one application to end before starting second one.

Multi-user: Multi-user is a term that defines an operating system or application software that allows concurrent access by multiple users of a computer. Time-sharing systems are multi-user systems. The computer can take the commands of a number of users to run programs, access files, and print documents at the same time.

Stability: One of the design goals of the UNIX is Robustness and Stability. The UNIX is stable by its own nature. UNIX doesn’t need Periodic reboot to keep the system stable to maintain performance levels. There is no problem of memory leak ups so it won’t freeze up or slows down. It has continues up time more than a year or hundreds of days. Therefore it requires less administration and maintenance.

Performance: In networks and workstations UNIX system provides high level performance. At a time it can handle large numbers of users. it is possible to tune the UNIX systems in a better way to meet our performance needs ranging from embedded systems to Symmetric multiprocessing systems.

Compatibility - UNIX can be installed on different types of hardware machines, including main-frame computers, supercomputers and micro-computers. Linux- One of the popular variants of UNIX which will run on almost 25 processor architectures including Alphs/VAX, intel, PowerPC etc. UNIX also is compatible with windows for file sharing etc via smb(samba file system) and NFS(Network File system).

Security: UNIX is one of the most secure operating systems. “Firewalls” and flexible file access permission systems prevent access of unwanted visitors or viruses.
Shell is the ‘command interpreter’ for UNIX systems. It resides at the base of most of the user level UNIX programs. All the commands invoked by user are interpreted by shell and it loads the necessary programs into memory. Thus being a default command interpreter on UNIX makes shell a preferred choice to interact with programs and write glue code for test scripts.

Advantages of using Shell for test automation on UNIX

Following are some of the advantages of using Shell for test automation on UNIX,

Free: Most of the popular shells are free and open source no additional cost. No Additional software required: All the UNIX systems have a default shell already installed and configured (bash/ksh/csh). So there is no need to spend extra time to set up the shell. Shell is something very common to UNIX systems and a inhabitant always understands the problems pretty well and help resolving it.

Powerful: It provides plenty of programming constructs to develop scripts with simple or medium complexity.

Extensible: It is possible to extend the shell scripts by using additional useful commands/programs for extending the functionality. it is possible to write shell scripts using default editors available (vi, emacs etc) and can run and test it. No specialized tool is needed for the same.

Color high lighted report: Can even generate color-highlighted reports of test case execution, which is of great help.

Portability: Shell scripts are portable to other UNIX platforms as well as to Windows via Cygwin. Cygwin which is a shell on windows allows us to execute shell scripts on windows also.

Shell Commands

For testing it is important to do test setup, test procedure steps, validation of actual result with expected result, clean up steps to bring the application back to original state, scheduling a test, prepare test results log, and report the test results. Shell has many commands, which can help to achieve automation of these test activities.

Following are some useful Unix Shell commands for automation.

Verification and setup testing: When we want to test for installation/ uninstallation etc we can effectively use the file verification functionality of the shell.
-f to check whether a file exist
-r to check whether a file is readable
-w to check whether a file is writeable
-x to check whether a file is executable
We can also invoke external commands and check for their return code for success/failure of execution using predefined variable’$?’.
Also availability of common looping constructs like 'for' and 'while' make shell obvious choice to automate installation/ uninstallation testing, checking out whether commands/programs are executing successfully or not and functionality testing as well.
Most of the time we need to setup some environment variables, have some proper links (test environment) to set, this task can be automated using shell and is of great help.
Interactive Application testing using expect
Expect is a program that talks to other interactive programs based on a script. We need to mention the “expect” to what to expect from the program and what should be the response need to send. When writing an “expect” script, the output from the program is an input to the “expect script” and output of the “expect” script is input to the program. So now the “expect” script keep on expecting output from the program and keep on feeding input the interactive program, thus automating the interactive programs. Expect is generalized so that it can interact with any of the user level command/ program. Expect can also talk to several programs at the same time. In general expect is useful for running any program, which requires interaction between user and the program. All that is necessary is the interaction can be characterized using a program.

Executing shell scripts on Windows using Cygwin
Cygwin is a Linux like environment for windows. It consists of two parts - A dll, cygwin1.dll which acts as a Linux emulation layer providing Linux API functionality.
- A collection of tools, which provide Linux look and feel.

Cygwin is available under GPL (GNU Public License) and is free software. Cygwin gives us almost all standard unix shells (bash, ksh, csh etc) so you can run most of your scripts on windows as well. Thus cygwin provides lot of portability to shell scripts.
When not to use shell scripts for automated testing

It’s not a good idea to use shell scripts in following cases.
- Need to generate or manipulate graphics or GUI
- Need port or socket I/O
- Complex applications with type checking, function prototyping etc
- Need data structures like linked lists, trees etc.
If any of the above is true it’s a good idea to use more powerful languages like C, C++ or Perl/ Python for test automation

No comments:

Post a Comment