如何用STAF进行自动化测试分布式运行

  本文的目的在于引导读者去了解STAF及如何调用其接口去实现自动化测试的分布式动行。

  提到分布式运行,很多人想到了Jenkins,Jenkins里面有个node插件,可以去分派任务给slave,Jenkins也有console可以查看运行LOG,但Jenkins存在如下几个问题:

  1.需要事先在node里填写好slave的信息。

  2.需要人为的去指派任务到某个slave上

  3.多个task一起运行时,console信息是混乱的

  4.slave上的报告不好整合

  为了解决以上几点,向大家推荐一款工具STAF(Software Testing Automation Framwork),这款工具可以实现机器与机器间的通信,只要两台机器互相安装好STAF工具后,就可以用代码或控制台来控制另一台slave,这样的操作是不需要用户名与密码的。列举一下STAF的一些特性:

  1.官网及下载地址:http://staf.sourceforge.net/

  2.拥有丰富的Service

  3.支持JAVA与PY语言扩展

  4.我们做分布式一般要用到的Service包括:Ping Service,File System (FS) Service,Process Service,Resource Pool (ResPool) Service

  先来看一下分布式应该如何来规划:

  1.有三台机器(A1,A2,A3)

  2.十个测试脚本要运行(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10)

  3.在配置文件中配置每台机器上最多同时运行3个脚本

  4.运行时应该这样分配:A1(T1,T2,T3),A2(T4,T5,T6),A3(T7,T8,T9),T10等待,如果A1,A2,A3中有任何一个脚本运行完成,T10即填充进去。

  5.每个脚本运行完成后,把每个脚本运行完成后的结果回传给主机(可以配置主机为A1)

  6.在A1中整合成最后的报告

  流程图如下:

  

  架构图:

  

  到这里,我相信大家对整个架构应该有一个了解了,如果你有代码基础,且对此有兴趣的话,请马上动手实现吧。

  下面来讲讲STAF的Service的调用(JAVA语言)

  以Process Service为例,我们打开STAF的user gride且找到Process Service后, 其Description:  

  The PROCESS service is one of the internal STAF services. It provides the following commands

  • START - Starts a process, and optionally registers for process end notification
  • STOP - Stops a process
  • LIST - Retrieves brief information on processes or lists the operational settings for the Process service
  • QUERY - Retrieves detailed information about a process with a specified handle
  • FREE - Releases the results data about a process
  • NOTIFY REGISTER - Registers for process end notification
  • NOTIFY UNREGISTER - Unregisters for process end notification
  • NOTIFY LIST - Displays the list of machines/processes to receive process end notification
  • SET - Sets operational settings for the Process service
  • HELP - Returns syntax information

  Syntax

START [SHELL [<Shell>]] COMMAND <Command> [PARMS <Parms>]  [WORKDIR <Directory>]
      [VAR <Variable=Value>]...  [ENV <Variable=Value>]... [USEPROCESSVARS]
      [WORKLOAD <Name>]  [TITLE <Title>]  [WAIT [<Number>[s|m|h|d|w]] | ASYNC]
      [STOPUSING <Method>]  [STATICHANDLENAME <Name>]
      [NEWCONSOLE | SAMECONSOLE]  [FOCUS <Background | Foreground | Minimized>]
      [USERNAME <User name> [PASSWORD <Password>]]
      [DISABLEDAUTHISERROR | IGNOREDISABLEDAUTH]
      [STDIN <File>] [STDOUT <File> | STDOUTAPPEND <File>]
      [STDERR <File> | STDERRAPPEND <File> | STDERRTOSTDOUT]
      [RETURNSTDOUT] [RETURNSTDERR] [RETURNFILE <File>]...
      [NOTIFY ONEND [HANDLE <Handle> | NAME <Name>]  [MACHINE <Machine>]
      [PRIORITY <Priority>] [KEY <Key>]]
如何去用呢:
public void runSTAFProcess() {		
		try {
			STAFHandle handle = new STAFHandle("MyApp/Test");
		} catch (STAFException e) {
			System.out.println("Error registering with STAF, RC: " + e.rc);
		}
		String service = "PROCESS";
		String request = "START SHELL  COMMAND Ruby test.rb RETURNSTDOUT STDERRTOSTDOUT WAIT";
		try {
			String result = handle.submit("192.168.1.1", service,
					request);
			STAFMarshallingContext mc = STAFMarshallingContext
					.unmarshall(result);
			Map mcMap = (Map) mc.getRootObject();			
			handle.unRegister();
		} catch (STAFException e) {
			System.out.println("run process error");
		} 		
	}

   从以上代码可以看出,我们在submit时的几个参数,第一个时slave的IP地址,第二个是service名称(在user guide中可以找到),第三个就是我们需要在slave上运行的命令,其格式在user guide中也可以找到,同时我也相信通过上面的示例,懂一些代码的人应该都能够通过syntax而写出来。

  先介绍到这里,希望大家能从中得到启发,有任何问题可进群(254285583)进行咨询,很乐意为大家解答问题。

posted on 2013-07-03 11:39  张飞_  阅读(2961)  评论(1编辑  收藏  举报

导航