您所在的位置: 首页 > 开发 > 软件测试 >

BeanShell在人工测试与管理之中的应用

http://developer.51cto.com  2007-08-07 13:53  佚名  赛迪网  我要评论(0)
  • 摘要:本文告诉您如何使用BeanShell来解决TestCase允许测试人员作动态修改时的重复工作量大和如何方便地重复使用、组合、保存等问题。
  • 标签:BeanShell  人工  测试  管理

JUnit 单元测试学深入人心的同时,也发现它对用户交互测试无能为力:

◆TestCase允许测试人员作动态的修改

可以在Test Case中实现一个测试参数输入功能(UI 或参数配置文件)来解决这个问题,但实现这些功能的代价与重复工作量会很大。

◆TestCase可以方便地重复使用、组合、保存

不是所有所有测试环境下,都容许打开一个重量级的Java IDE编写有严格规范的Java代码。这就是脚本语言受欢迎的原因。

BeanShell可以较好解决以上问题。

BeanShell基本

bsh.Interpreter 是beanShell 的主要接口。

以下可以实现一个简单的Java Shell:

public class TestInt {
public static void main(String[] args) {
bsh.Interpreter.main(args);
}
}

结果:

BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net) 
bsh % System.out.println("Hello BeanShell");
Hello BeanShell
bsh %

你也可以用以下代码实现同样的功能,代码中可以比较明显地看出其结构:

public static void main(String[] args) {
Reader in = new InputStreamReader( System.in );
PrintStream out = System.out;
PrintStream err = System.err;
boolean interactive = true;;
bsh.Interpreter i = new Interpreter( in, out, err, interactive );
i.run();//线程在这里阻塞读System.in
}

1、BeanShell上下文(Context/Namespace)

public static void main(String[] args) throws Throwable {
Reader in = new InputStreamReader( System.in );
PrintStream out = System.out;
PrintStream err = System.err;
boolean interactive = true;;
bsh.Interpreter i = new Interpreter( in, out, err, interactive );
Collection theObjectReadyForShellUser = new ArrayList();
theObjectReadyForShellUser.add("Str1");
i.set("myObject", theObjectReadyForShellUser);
i.run();
}

用户的UI:

BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net) 
bsh % System.out.println( myObject.get(0) );
Str1
bsh %

Shell的上下文在测试中特别有用。想一下,如果将上面的“theObjectReadyForShellUser”换成一个预先为测试用户生成的RMI本地接口存根,由测试用户调用相应的存根方法。这可应用于动态测试,也可以应用于系统的远程管理。

2、静态Java代码与动态Java代码的组合使用

public static void main(String[] args) throws Throwable {
Reader in = new InputStreamReader( System.in );
PrintStream out = System.out;
PrintStream err = System.err;
boolean interactive = true;;
bsh.Interpreter i = new Interpreter( in, out, err, interactive );
//show a dialog for user to input command.
String command = JOptionPane.showInputDialog( "Input Command(s)" );
i.eval( command );//Run the command
}

【相关文章】

【责任编辑:火凤凰 TEL:(010)68476606-8036】

软件测试 走向成功之路
虚拟存储管理与应用
系统应用日志分析管理
测试开发人员参考手册
网络管理系统如何支撑ITSM
 
 验证码: (点击刷新验证码)   匿名发表
  • 亮剑.NET. 图解C#开发实战

  • 作者:李新峰 付志涛 缪勇
  • 本书采用全新的图解思路,分3篇介绍使用微软C#语言开发实际应用程序的基本知识。第1篇包括10章,介绍了C#语言的基础知识,主要..
Copyright©2005-2009 51CTO.COM 版权所有