基于shell写的一个简单的人机交互的测试框架

                 在以前项目上根据项目的需要写了个shell的测试框架,在这里分享一下。

                 这个框架式用来测试一些需要人机交互的应用功能测试,在框架中集成了expect 工具来实现人机交互功能需要。

                  项目的大概目录结构如下:

                  data/

                  lib/python

                  lib/tcl

                  lib/sh

                  lib/utils_check

                  log/run_log

                  result/result

                  testcase/

                  regression.sh

                  start_test.sh

                  下面是几个核心代码文件的样例:


start_test.sh
cd /home/jason/testcase_sh
time ./regression.sh >> ./result/result 


regression.sh
#!/bin/sh

Run_Dir=`pwd`
case_num=0
pass_case_num=0
fail_case_num=0

export auto_test_path=$Run_Dir
echo "auto_tes path is " $auto_test_path

find_file()
{
	file_dir=$1

	if [ -d $file_dir ]
	then
		find $file_dir |grep sh$|sort >>$Run_Dir"/data/"testcase_list
	fi

}


find_testcase()
{
        str_dir="`pwd`/testcases"

        if [ -z "$1" ]
        then
                echo "run all testcase "

        else
                echo "run all testcase for module " $1
                str_dir=$str_dir/$1

        fi
	
	find_file $str_dir
                
}


execute_file()
{
	file_name=$1

        echo "testcase_name:" $1	
	
	if [ -f $file_name ]
	then
		echo "" >>$Run_Dir"/log/run_log"
		echo "TESTCASE:"  $file_name >>$Run_Dir"/log/run_log"

		if [ -n "`ls $file_name|grep py$|grep -v grep`" ]
		then
			echo "run a python script "
			time python $file_name >>$Run_Dir"/log/run_log"
		elif [ -n "`ls $file_name|grep exp$|grep -v grep`"  -o  -n "`ls $file_name|grep tcl$|grep -v grep`" ]
		then
			echo "run a tcl script "
			time tcl $file_name >>$Run_Dir"/log/run_log"
		else
			echo "run a shell script "
			time sh $file_name >>$Run_Dir"/log/run_log"
		fi
	fi


	case_status=0
	case_status_line=`grep ^Testcase $Run_Dir"/log/run_log" |tail -1`
	#echo "status line:" $case_status_line
	testcase_name=`echo $case_status_line|awk '{print $2}'`
	#echo "testcase_name:" $testcase_name
	testcase_statu=`echo $case_status_line|awk '{print $4}'`

	echo "Test_status:" $testcase_statu
	echo " "
	echo " "




	if [ -z $testcase_name -o -z $testcase_statu ]
	then
		#echo "test status..."
		case_status=1
	elif [ `echo $testcase_statu | awk '{printf("%d\n", match($0,"FAIL"));}'` -ne 0 ]
	then
		#echo "test status "
		case_status=1
	fi

	#get some useful information according to the project 
	echo "**********detail result log**************" >> $Run_Dir"/log/run_log"
	grep ^/ out |grep -v Welcome>> $Run_Dir"/log/run_log" 
	echo " " >> $Run_Dir"/log/run_log "
	


	
}


Output_print(){
	Time=`date '+%Y-%m-%d %H:%M'`
	str_line=`uname -a`
	Machine=`echo $str_line|awk '{print $2}'`
	OS=`echo $str_line|awk '{print $1" "$3}'`

	echo " ===============The result of Regression testing for Delica Project============">>$Run_Dir"/result/result"	
	echo "         Test set : "$1 >>$Run_Dir"/result/result"
	echo "         Machine  : "$Machine >>$Run_Dir"/result/result"
	echo "         OS       : "$OS >>$Run_Dir"/result/result"
	echo "         Time     : "$Time >>$Run_Dir"/result/result"
	echo " ==============================================================================">>$Run_Dir"/result/result"
	echo "     ">>$Run_Dir"/result/result"
	echo "     ">>$Run_Dir"/result/result"

}


#initial log and execute list 
testcase_list=$Run_Dir"/data/testcase_list"
>$testcase_list
testcase_log=$Run_Dir"/log/run_log"
>$testcase_log
>$Run_Dir"/result/result"


if [ -z $1 ]
then 
	Output_print "ALL"
else
	Output_print $1
fi


find_testcase $1

if [ -s $testcase_list ]
then
	for line in `cat $testcase_list`
	do

		if [  "$line" == "" ]
		then
			echo "don't find any file "
			exit 0
		fi

		if [ -f $line ]
		then
			#echo "file : "$line
			execute_file $line
			case_num=`expr $case_num + 1`
			#echo "testcase name is " $testcase_name "status is " $case_status

			if [ $case_status -eq 0 ]
			then 
				pass_case_num=`expr $pass_case_num + 1`
			else
				fail_case_num=`expr $fail_case_num + 1`				
			fi
		fi

	done
else
	echo "don't find any file to execute"
fi


echo "There are "$case_num" testcase be run"
echo "----------PASS CASE: "$pass_case_num
echo "--------- FAIL CASE: "$fail_case_num
echo " "
echo " "
echo " "

echo "================================Detail Information============================ "
cat log/run_log 

./lib/python/mail1.py 

run_cmd.exp
#!/tools/bin/expect


set workPath "/home/jason/080811";
set file "$workPath/file";
set file1 "$workPath/file1";
set master [lindex $argv 0];
set master_port [lindex $argv 1];
set dataserver_port [lindex $argv 2];
set cmds [lindex $argv 3] ;

set i 0;

puts "this is insid test"

puts $cmds ; 
#puts [llength $cmds];

set timeout 300 ;
spawn $workPath/src/test/DelicaTest -s $master -p $master_port -q $dataserver_port ;

# mkdir
set i 0 ;
set j [llength $cmds];

while {$i<=$j} {
expect "$"  {send "[lindex $cmds $i]\n"};
incr i
}



expect "$" {send "quit\n"}
expect eof
 
  
 
  



 
  
 
  

你可能感兴趣的:(基于shell写的一个简单的人机交互的测试框架)