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

豆豆网   技术应用频道   2007年08月07日  【字号: 收藏本文

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

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

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

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

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

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

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

  1.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.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本地接口存根,由测试用户调用相应的存根方法。这可应用于动态测试,也可以应用于系统的远程管理。

  1.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
}

责编:豆豆技术应用

正在加载评论...