首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Eclipse TPTP 實戰教程(三):測試應用程式

     編輯:Java 相關知識

開始之前

關於本系列

在交付健壯產品質量的漫長過程中,編寫代碼只是第一階段。必須對代碼進行測試,檢驗操作與準確性;往往還需分析代碼,消除性能瓶頸與資源浪費(特別是記憶體);還必須監視代碼,定位故障、識別使用模式、尋找優化機會、探測入侵嘗試。

Eclipse Test and Performance Tools Platform (TPTP) 是一種軟體架構及擴展 Eclipse 平台的組件,包含測試、效能與監視工具。本系列教程講解 TPTP 功能:第 1 部分示範分析 Java 應用;第 2 部分講解捕獲日誌並轉換為 CBE 格式;第 3 部分解釋如何管理應用程式測試。

關於本教程

本教程演示如何使用 TPTP 測試特性度量 Web 應用響應時間,講解創建與管理改進的 JUnit 測試,描述創建計算機引導的手工測試。

目標

學習使用 Eclipse TPTP 測試特性度量 Web 應用響應時間;創建與管理改進的 JUnit 測試;創建計算機引導的手工測試。

前提條件

具備軟體開發經驗並了解完整軟體開發生命週期;熟悉命令列安裝軟體、設置與管理 shell 和系統環境變量(PATH、LD_LIBRARY_PATH、CLASSPATH);了解 Java、Eclipse IDE 與 UI 有幫助但非必需。

系統需求

可在支援 JVM 的任何系統運行 Eclipse(Solaris、Linux、Mac OS X、Windows)。無 JVM 需預留 250 MB 磁碟空間。核心 Eclipse 與插件需 50 MB。建議 64 MB 以上空閒物理記憶體。

所需軟體

  • Java (Sun 或 IBM),下載 JRE 1.5 最新更新
  • Eclipse 3.2 IDE
  • Eclipse TPTP Runtime 4.2.0
  • Eclipse TPTP Testing Tools Runtime 4.2.0
  • Eclipse TPTP Agent Controller
  • EMF SDK 2.2
  • XSD SDK 2.2
  • Mozilla 1.7.8+(手工測試工具需其 UI 組件)

開始

安裝 Eclipse 與 TPTP

下載所需組件後安裝。以下說明適用 Linux/UNIX;Windows 請參閱 Eclipse.org。假設檔案放在 /tmp:

# 解壓 JRE 到 ~/java-tptp
mkdir ~/java-tptp; cd ~/java-tptp
sh /tmp/jre-1_5_0_07-linux-i586.bin

# 解壓 Eclipse、TPTP 及前置套件
tar xzvf /tmp/eclipse-SDK-3.2-linux-gtk.tar.gz
unzip /tmp/xsd-runtime-2.2.0.zip
unzip /tmp/tptp.platform.runtime-TPTP-4.2.0.zip
unzip /tmp/tptp.test.runtime-TPTP-4.2.0.zip
unzip /tmp/emf-sdo-runtime-2.2.0.zip

# 解壓、配置、啟動 Agent Controller
mkdir tptpd; cd tptpd
unzip /tmp/tptpdc.linux_ia32-TPTP-4.2.0.zip
chmod +x ./{bin,lib}/*

# 設定環境變量
export JAVA_HOME=~/java-tptp/jre1.5.0_07
export CLASSPATH=$JAVA_HOME
export PATH=$JAVA_HOME/bin:$JAVA_HOME/tptpd/bin:$PATH
export MOZILLA_FIVE_HOME=/usr/lib/mozilla
export LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME:$JAVA_HOME/tptpd/lib:$LD_LIBRARY_PATH

# 啟動 Agent Controller
cd ./bin
./SetConfig.sh
./RAStart.sh

# 啟動 Eclipse
~/java-tptp/eclipse/eclipse

或使用 Eclipse 內建 Update Manager 安裝 TPTP:Software Updates → Find and Install → Search for new features → New Remote Site,名稱 TPTP Update Site,URL https://eclipse.org/tptp/updates/site.xml,完成安裝後重啟 Eclipse。

Eclipse TPTP 測試工具

提供四種測試軟體:

  • URL 性能工具:自動對 Web 應用性能測試,捕獲瀏覽器會話建立可重複 JUnit 測試,自動添加度量措施度量響應時間,結果以圖表呈現。
  • JUnit 工具:在 Eclipse 中創建管理 JUnit 測試,提供深度交互式報告快速研究失敗測試。
  • 手工測試工具:記錄必須手工執行的測試,捕獲指令、預期結果與檢查表,使手工測試更有規律、可靠。
  • Automated GUI Recorder (AGR):捕獲回放用戶與應用 GUI 交互,支援原始螢幕座標或 SWT 控件屬性記錄,驗證鉤子判斷正確性,資料池調整輸入。(功能豐富,另闢教程講解)

本教程演示 URL 工具、JUnit 工具、手工測試工具。

度量 Web 應用性能

Web 應用性能測試因組件眾多(瀏覽器、網絡、Web 伺服器、遠程主機、應用、資料庫等)而複雜。TPTP URL 工具旨在記錄與自動回放用戶交互並基準測試,能記錄每次「點擊」並度量請求響應延遲。

使用 URL 工具

範例 PHP 應用(清單 2)消耗 CPU 週期並連接 Web 伺服器,可選擇休眠 0.25s、0.5s、1s。部署為 $DOCROOT/burn/index.php,瀏覽器訪問 https://localhost/burn/index.php。

<!DOCTYPE html ...>
<html ...>
<head><title>Consume Cycles Example</title></head>
<body>
<?php
$time = 0;
switch($_GET['mode']) {
case 'short': $time=250; break;
case 'medium': $time=500; break;
case 'long': $time=1000; break;
}
$time *= 1000;
if ($time > 0) { usleep($time); print "<p>I snoozed for $time microseconds</p>"; }
?>
<h3>Choose how long you want the application to sleep:</h3>
<ul>
<li><a href="index.php?mode=short">A short time</a></li>
<li><a href="index.php?mode=medium">A little while</a></li>
<li><a href="index.php?mode=long">A long time</a></li>
</ul>
</body></html>

構建 URL 測試

  1. 啟動 Eclipse,創建名為 PHP 的一般專案。
  2. 設置瀏覽器與代理端口首選項:Window → Preferences → Test → HTTP Recording,配置如圖 3。(HTTP Recorder 使用這些設置啟動 Mozilla 並轉發流量,使用端口 1080 SOCKS 4 代理)
  3. New → Other → Test → Recording → HTTP Recording,命名記錄檔為 Simple,完成。
  4. Recorder Control 面板出現,空白 Mozilla 視窗開啟。在 Mozilla 輸入 PHP 應用 URL,點擊三個鏈接並返回,切回 Eclipse 點擊 Stop recording。生成 Simple.testsuite。
  5. 查看捕獲的 HTTP 請求:HTTP Requests 標籤可修改請求(添加頭、修改參數)。
  6. 定義測試套件行為:Behavior 標籤編輯循環與請求順序,設置循環迭代次數(如 10),保存。
  7. 編譯測試套件:切換到 Test Perspective,右鍵 Simple 選 Generate,選擇 Java 專案 Testing,生成 JUnit 測試代碼。
  8. 運行 URL 測試:右鍵 Simple.java 選 Run As → JUnit test 驗證功能;再切回 Test Perspective,右鍵 test 資料夾選 Run As → Run,選擇 Simple 執行性能測試。
  9. 分析結果:雙擊生成的測試日誌 Simple,Events 標籤查看響應時間(毫秒)。
  10. 生成報告:運行套件 3-5 次,右鍵 Simple 選 Options → Report → HTTP Page Response Time 查看總結。

創建與管理 JUnit 測試

範例類 Account(清單 3)建模銀行帳戶,含 balance、deposit、withdrawal、compoundAnnually 方法。

package demo;
import java.lang.Math;
public class Account {
    private double balance = 0;
    public Account() { this.balance = 0; }
    public Account(double deposit) { this.balance = 0; if (deposit > 0) this.balance = deposit; }
    public double deposit(double deposit) { if (deposit < 0) return this.withdrawal(deposit); this.balance += deposit; return this.balance; }
    public double withdrawal(double withdrawal) { if (withdrawal < 0) return this.deposit(-withdrawal); if (this.balance >= withdrawal) this.balance -= withdrawal; return this.balance; }
    public double balance() { return this.balance; }
    public double compoundAnnually(double rate) { int n = 356 * 24; if (rate > 0) this.balance *= Math.pow((1 + (rate/n)), n); return this.balance; }
}
  1. 創建 Java 專案 Demo,包 demo 下建立 Account 類貼上代碼。
  2. 右鍵 Account.java 選 New → Other → Test → TPTP JUnit Test,命名 AccountTest,添加三個測試方法:testCreateEmpty、testCreateBalance、testVerifyDeposit。
  3. 編輯測試代碼(清單 4):
public void testCreateEmpty() throws Exception {
    Account zeroAccount = new Account();
    assertEquals("balance not zero", zeroAccount.balance(), 0.0, 0.01);
}
public void testCreateBalance() throws Exception {
    double startingBalance = 510.96;
    Account newAccount = new Account(startingBalance);
    assertEquals("starting balance incorrect", newAccount.balance(), startingBalance, 0.01);
}
public void testVerifyDeposit() throws Exception {
    double startingBalance = 987.91, deposit = 1560.98;
    Account newAccount = new Account(startingBalance);
    assertEquals("deposit not credited", newAccount.deposit(deposit), deposit+startingBalance, 0.01);
}
  1. 配置測試套件行為:Behavior 標籤添加三個測試方法,右鍵 AccountTest.testsuite 選 Run As → Test,生成執行日誌 AccountTest_#.execution。
  2. 查看結果:Events 標籤展開查看各事件,Overview 看總結。若測試失敗(如修改 testVerifyDeposit 斷言),可看到詳細錯誤與堆疊追蹤。
  3. 導入現有 JUnit 測試:File → Import → JUnit tests to TPTP。

創建手工測試

並非所有測試可自動化。TPTP 提供工具創建計算機引導的手工測試與測試套件。可創建測試案例在多套件重用,調整步驟順序,指定循環重複,「運行」手工測試套件時 TPTP 引導執行每步並記錄結果。

  1. 右鍵 PHP 資料夾 New → Other → Manual TPTP Test,命名描述性名稱。
  2. 添加測試案例(如「Home Page responds」、「Click short」、「Click medium」、「Click long」),Behavior 標籤創建循環與調用,拖曳調整順序,設置迭代次數。
  3. 切換到 Test Perspective,右鍵 Manual Web test 選 Run As → Run,選擇 Manual Web test 與 local_deployment 執行。
  4. Manual Test View 應用程式啟動(基於 Mozilla SWT UI),左側顯示得分與測試案例,右上描述操作,下方 Verdict 與 Reason 記錄通過/失敗原因。完成案例點擊 Next,全部通過後點擊 Stop 返回 Eclipse,查看總結。

結語

軟體構建兼具藝術與科學。若開發者不發揮創造力,代碼無法最快運行,問題糾纏不清,電腦可能比打字機強不了多少。雖軟體非齒輪發動機組成的物理機器,但有許多必須緊密咬合的「運動部件」。應善用 Eclipse TPTP 編輯構建代碼、測試、調整、觀察性能與穩定性。精明的開發者應好好利用 Eclipse。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved