How to automate test cases need reboot: a simple script solution

July 1, 2011 by · Comments Off on How to automate test cases need reboot: a simple script solution
Filed under: automation, test 

During the first 2 years in my career as a tester, I learned that for a tester, everything should be automated if possible. 2 years later I beginning my programming life and found that automation is a must skill for programmers too. There is lots of interesting things on my testings experience, here’s one of them.

The background: Sometimes we need to run some test cases, and each case must be run in a newly booted environment. Or when case B is depended on case A which need reboot after run. And we want automation of the process. (IT software installation, Antivirus performance test, etc is some practice requirement like this).

A simple way to automate such test case is : write the test cases in scripts, define all the
test cases script file name in an array scriptlist,  then run the script files whose name
is defined in the elements of the array one by one.  We can define an index for which script
is to be running in the array.  And each time we run a script file, we save the index, lets
say idx to a file on local disk.

Assum you have 4 scripted test cases to run in order: A.pl, B.pl, C.pl, D.pl (the script
language doesn’t matter, you can choose whatever you love). and after running of each case, need a system reboot.

We can write a new script called reboot-test-manager.pl, and put it to the auto start. Then
run the script manually as a trigger for the starting of all these 4 cases. Then each one
will run and reboot, and continue to run the next one.

Here’s the 4 simple things should be contained in the reboot-test-manager.pl, I’ll
demonstrate it in perl on windows, for linux some command must the changed, you know).

1).Initialize the index(idx) value
If next-file-torun.txt exist, try to read idx value from next-file-torun.txt,  else set $idx  value to zero.

my $idx = 0;
my @scriptlist=  ("A.pl","B.pl","C.pl","D.pl");

2). Save the idx value to a local file called next-file-torun.txt
The idx value indicates the file whose name is equal to scriptlist[idx] will be run.
echo $idx >next-file-torun.txt

3). Run the file indicated by scriptlist[idx] , and then increase idx by 1

my $filetorun = $scriptlist[$idx];
system("$filetorun");
$idx=$idx+1;

4). Reboot by restart command

system("shutdown -r -t 01");

Then script A.pl will be run, then reboot, then script B.pl will be run, then reboot and C.pl
….

This method is really useful on automation lots of software installation for IT if the
software need reboot. Also useful when we need to test some software performance  on a newly booted system, especially for antivirus performance test.

Here’s a sample script for running:

# The script would be run after system reboot

my @filelist =  ("install.a.pl","install.b.pl","install.c.pl");

##
my $idfile="next-file-torun.txt";
open READIN,"< $idfile" or die "can not open file:$!";
my $id = <READIN>;
close (READIN);
print "id=$id \n";

if($id>$#filelist)
{
	print "all files has been run!\n";
	print "\x07\x07"; # ring the bell
	exit(-1);
}

# sleep some seconds to run the following scripts:
print "sleeping...\n ";
sleep(10);

my $filetorun = $filelist[$id];
print "to run file:  $filetorun \n";

$id=$id+1;
open OUT,"> $idfile" or die "can not open file:$!";
print OUT $id;
close (OUT);

system("$filetorun");  ### call the script
system("shutdown -r -t 01"); ### reboot the system
Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)