Since : I always work for my future.
And : Linux is the future.
So: I work for Linux
网页自动测试 3.一个独立的测试单元
上一篇 /
下一篇 2008-05-06 07:04:16
/ 个人分类:automation
写一个完成的
测试软件和写
其它的测试软件没有什么不同,大体上是两种方法,top-down, or bottoms-up
要么从顶到底,从整体到局部,要么从底到顶,从局部到具体。只要始终不失去对全局的把握,无论那种方法都是没有关系的。
我习惯上是从底到顶,因为当我能够实现一个最小的测试单元的时候,我会更加有信心。
====
即便是最小的测试单元,它也是需要完整的规划的。我设计中的逻辑步骤如下:
1。建立测试环境
2。建立测试数据
3。开始测试
4。清理测试数据
5。完成
在考虑了模块化之后,我的主测试程序如下:
-------- tc_adduser.pl -----------
#!/usr/bin/perluse strict;use warnings;use Time::HiRes qw(sleep);use Test::WWW::Selenium;use Test::More tests => 11;use Test::Exception;use lib '/path/to/util pm file';use Util;# global veriablesour $host;our $port;our $browser;our $browser_url;our $configfile="test.conf";# read configruation fileour $config=Util::readconfig($configfile);$host=$config->{'host'};$port=$config->{'port'};$browser=$config->{'browser'};$browser_url=$config->{'browser_url'};# check testing environmentif (envcheck()){ print "\nEnvironment is ready for testing...";}else{ exit 1;}my $testdata=prepare_data();# run testrun_test($testdata);cleanup_data($testdata);print "\ntest finished\n";------------------ end of main, (not end of file) ---------
功能部件如下:
#######################################################
######## sub routiens ###########
#######################################################
# run all test case heresub run_test{ print "\ntest starts...\n"; my $data=shift; my $sel = Test::WWW::Selenium->new( host => $host, port => $port, browser => $browser, browser_url => $browser_url); $sel->open_ok("/ipa"); $sel->click_ok("link=Add User"); $sel->wait_for_page_to_load_ok("30000"); $sel->type_ok("form_title", $data->{'title'}); $sel->type_ok("form_givenname", $data->{'givenname'}); $sel->type_ok("form_sn", $data->{'sn'}); $sel->type_ok("form_krbprincipalkey", $data->{'krbprincipalkey'}); $sel->type_ok("form_krbprincipalkey_confirm", $data->{'krbprincipalkey'}); $sel->click_ok("document.form.submit[1]"); $sel->wait_for_page_to_load_ok("30000"); $sel->is_text_present_ok($data->{'sn'}." added!");}# run_test# loadconfig : this sub will call readconfig, and do environment check on the input data# return 1 if (1) config file can read (2) configuration data is oksub envcheck{ my $retval=0; if (defined $host || defined $port || defined $browser || defined $browser_url) { print "\ntest with the following configuration:\n"; print "\nhost : $host"; print "\nport : $port"; print "\nbrowser: $browser"; print "\nurl : $browser_url"; print "\nstart environment check"; if (pinghost($host, $port)){ print "\nEnviromnent report: selenium server alive at [$host:$port]"; $retval=1; }else{ print "\nEnvironment report: selenium server can not be reached at [$host:$port]"; print "\nexit testing on error: can not reach selenium server\n"; } }else { print "no test.conf found, and no default value defined, exit test"; } return $retval;}#envcheck# prepare_data : this sub will generate testing data.sub prepare_data{ #TODO: i need make sure this data does not exist before we start test my %newuser=('title'=>'auto001', 'givenname'=>'selenium', 'sn'=>'002', 'krbprincipalkey'=>'password123'); return \%newuser;}# prepare_datasub cleanup_data{ #TODO: Clean data from server, so we have clean system each time print "\ntest data should be deleted from server, so we have a clean system after we finish the test";}------------ now this is end of tc_adduser.pl -------------
----- here is the util.pm -----------
#!/usr/bin/perl# filename: general utilities#package Util;use Carp;#use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);use IO::Socket;require Exporter;#require AutoLoader;$VERSION='0.01';@ISA=qw(Exporter);@EXPORT = qw(printhash printarray loadconfig pinghost);# in: hash reference# out:NONE, just printsub printhash { my $hash=shift; my %h=%$hash; foreach (sort keys %h){ print "\nkey=[$_] value=[$h{$_}]"; }}sub printarray { my $arrey=shift; my @a=@$array; foreach (sort @a){ print "\n value=[$_]"; }}# readconfig : input: a key=value pair config filesub readconfig{ my $configfile=shift; my %c; if (open(CONFIG,$configfile)){ print "\nloading configruation fle [$configfile] ..."; my @con= <CONFIG>; foreach my $line (@con){ chomp($line); # the basic format of config file would be: position = name ; sample data 0=version next if $line=~ m/^#/; # ignore commends line ==> starts with "#" char next if $line=~ m/^\s*$/; # ignore empty lines next if $line=~ m/^\[/; # ignore lines such as [system] my @pair = split(/=/,$line); $pair[0] =~s/ //g; # replace " " - white space with nothing, which means delete all white space $pair[1] =~s/ //g; $c{$pair[0]} = $pair[1]; #print $pair[0]; #print $pair[1]; } print " done \n"; close CONFIG; }else{ print "\nfile [$configfile] can not open "; } return \%c;}#readconfig############################### network utilities ###############################sub pinghost{ # input: ($host, $port) # output: return 1 if remote host active on given port, otherwise, return 0 my ($host, $port)= @_; my $return = 0; # Try to connect my $remote = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => $port, Timeout => 8, ); # verify response if ($remote) { # print "$host is alive\n"; close $remote; $return = 1; } return $return;}1;----------------- end of Util.pm ------------------
今天的任务就到此为止了。
完成了一个独立测试单元是为了给我一个具体而微的全局概念。我现在基本上知道我还需要做那些
工作了。我需要好好想一下,给出一个任务清单--等于是列出一个具体的设计方案
收藏
举报
TAG:
web
Automation
automation