BeanShell在人工測試與管理之中的應用

來源:互聯網
上載者:User

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
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.