使用 TPTP 自動化服務

TPTP 提供了若干可以從 Eclipse 工作台環境定義之外呼叫的自動化服務。 這些服務包括執行測試、詢問測試結果和發佈測試結果。

這個說明主題解說如何利用內建的 TPTP 自動化服務來執行下列動作:

測試執行服務

測試執行服務用於透過程式來啟動、執行及產生測試結果。 測試執行服務封裝現有的測試執行控管功能,並淘汰目前的獨立式測試執行控管方法。 利用測試執行服務,可以從 ant Script、Shell Script 及任意程式中使用無監視器型 Eclipse 來啟動測試。 只要執行機器上有足夠的記憶體,即可同時執行多個測試(目前,每一個服務執行需要獨立的 Eclipse 主機實例)。 即使在遠端機器上以 TPTP 遠端測試執行特性和部署來執行測試, 服務使用端的機器一樣是執行 Eclipse 主機實例的機器。

服務內容

請參閱支援的測試執行服務內容的參考資料 -- 相同內容適用於各用戶端自動化配接器。 以 ant Script 而言,有一組簡單的 ant 作業可設定服務內容和執行服務。 以 Shell Script 而言,內容是以規定的指令行格式來指定 (以虛線指定內容,後面接著空格,接著再指定實例的值)。 以 Java 程式而言,將參照自動化用戶端 jar,並用來設定內容和控制服務執行。 有些用戶端配接器可能提供特定用戶端環境所需的其他選項(或視為該配接器的額外特性)。

自動化服務架構套裝

TPTP 自動化服務元件封裝在 org.eclipse.hyades.execution 外掛程式內, 以三個 jar 檔、plugin.xml 中的一些 XML 及 MANIFEST.MF 檔案中的一些項目來表示。

  1. tptp-automation-client.jar (建立服務匯流排的用戶端所需的一小段程式碼、必要時會啟動 Eclipse、能夠在無須監控的情況下執行服務, 從指令行和 Shell Script 中執行測試時,以及從任意 Java 程式碼中執行測試時,將參照此 jar)。
  2. tptp-automation-server.jar (TPTP 自動化伺服器、利用架構來詢問和執行服務時隨需載入、實作核心 Eclipse 應用程式延伸點、作為分配管理系統和服務定位器)
  3. ant-tptp.jar(此 jar 的內容同於 tptp-automation-client.jar,但為了與其他 ant 工作庫達成一致,所以重新命名)

從 Ant Script 執行測試

如果打算在 Eclipse 執行實例之外執行測試,請確定已適當安裝 ant -- 請參閱 ant 文件來適當設定。 除了標準的 ant 安裝,也要將 ant-tptp.jar 連同其他作業一起複製到 ant lib 目錄。 如果是在 Eclipse 之內執行,Ant 已安裝好,但請確定在 Ant 啟動配置的其他作業標籤中,或在「Ant 類別路徑」喜好設定頁面的「廣域項目」區段中,含有 ant-tptp.jar。 以下的 ant Script 內嵌範例是 ant Script "test.xml" 的範例,可在測試專案的 unit-tests 資料夾內執行 testA 測試。 測試專案位於指定的工作區 "d:\development\workspace",Eclipse 主機實例指定為 "d:\development\eclipse"。關於其他有效的內容組合,請參閱支援的內容參照清單。

雖然可以使用 TPTP 自動化作業來執行測試,但下列範例使用 TPTP 測試作業。 相較於較特殊的 TPTP 測試作業,執行任意 TPTP 自動化服務的 ant 作業是較一般的自動化作業。 測試作業是一種為了啟動測試執行服務而特別建立的安全類型作業。 org.eclipse.hyades.execution 外掛程式的 src-automation-client 來源資料夾的 org.eclipse.hyades.automation.test 套件中有更多 ant Script 範例。

<project name="TPTP Ant Tasks Test Script"
 default="test" 
 xmlns:tptp="antlib:org.eclipse.hyades.automation.client.adapters.ant">

<property name="eclipse.home" value="D:\development\eclipse"/>

 <description>
  This ant script tests the TPTP automation ant task client adapters
  and also can be used to test the underlying execution of TPTP automatable
  services via the TPTP automation server
 </description>

 <!-- Execute TPTP test suites -->
 <target 
name="test" description="Executes TPTP Test Suites using default TPTP Ant 
Tasks"> 

   <!-- 
Execute a TPTP test suite using the test task and providing the workspace 
-->
   <tptp:test
     workspace="D:\development\workspace"  
     project="tests"
     suite="unit-tests/testA.testsuite"
     synchronicity="synchronous"/>

  </target>
</project>

從 Ant Script 執行多項測試及詢問它們的結果

請確定您已適當配置 Ant 來執行 TPTP 自動化服務(請參閱上一節,以瞭解如何從 Ant 執行測試。) 以下範例顯示如何利用 tptp:execution 作業來執行多重測試,以及如何利用 tptp:interrogation 作業來詢問這些測試執行的結果。 這些 Ant 作業都接受多個 Ant filesets 和/或 filelists,可讓您使用萬用字元或直接將多個檔案指定成作業的輸入。 在這個範例中,我們執行 TPTPJUnitTests 專案中的所有測試套組,所有產生的全套 .execution 檔都是作為我們所提供的 Ant 變數(這裡是 "tptp.test.results")中之 filelist 而傳給我們。 之後,我們再將這個 filelist 當作輸入傳給 tptp:interrogation 服務,這項服務會詢問每個這些執行檔來判斷整體的判定,且會將它指派給我們所提供的 Ant 變數(這裡是 "tptp.test.verdict")。

<project name="TPTP Automatable Services Tutorial"
default="main" xmlns:tptp="antlib:org.eclipse.hyades.automation.client.adapters.ant"> <!-- Define common properties for all automatable services --> <property name="tptp.test.workspace" value="C:\eclipsecon\eclipse\workspace"/> <property name="tptp.test.project" value="TPTPJUnitTests" /> <property name="tptp.test.connection" value="tptp:rac://localhost:10002/default"/> <property name="tptp.automation.eclipse" value="C:\eclipseconwb_birt\eclipse" />
<!-- Define script-local properties, that vary by installation --> <property name="project.dir" value="${tptp.test.workspace}\${tptp.test.project}"/>
<target name="main" depends="test-execution, test-results-interrogation"/>
<!-- Execute test suites using default results name --> <target name="test-execution"> <echo message="Executing test suite..." /> <tptp:execution resultsrefid="tptp.test.results"> <fileset dir="${project.dir}">     <include name="**/*.testsuite"/> </fileset> </tptp:execution> </target>
<!-- Interrogate test suite results for verdict --> <target name="test-results-interrogation"> <echo message="Interrogating test suite results..." /> <condition property="tptp.test.success">     <tptp:interrogation verdictproperty="tptp.test.verdict">         <filelist refid="tptp.test.results"/>     </tptp:interrogation> </condition> <echo message="The overall test result verdict is: '${tptp.test.verdict}'" /> </target>
</project>

從 Ant Script 產生測試報告

請確定您已適當配置 Ant 來執行 TPTP 自動化服務(請參閱上一節,以瞭解如何從 Ant 執行測試。) 以下範例顯示如何利用 tptp:execution 作業來執行多重測試,以及如何利用 tptp:publication 作業,根據這些測試執行的結果來產生 BIRT 型的報告。 發佈作業也接受多個 Ant filesets 和/或 filelists,可讓您使用萬用字元或直接將多個檔案指定成輸入。 在這個範例中,我們執行 TPTPJUnitTests 專案中的所有測試套組,所有產生的全套 .execution 檔都是作為我們所提供的 Ant 變數(這裡是 "tptp.test.results")中之 filelist 而傳給我們。 之後,我們再將這個 filelist 當作輸入傳給 tptp:publication 服務,這項服務會利用預設報告範本(org.eclipse.tptp.test.report.birt 外掛程式中的 TestExecution.rptdesign)來產生 TPIP 執行報告,且會將它放在指定的位置(這裡是 c:\temp\report.html)。

<project name="TPTP Automatable Services Tutorial"
default="main" xmlns:tptp="antlib:org.eclipse.hyades.automation.client.adapters.ant"> <!-- Define common properties for all automatable services --> <property name="tptp.test.workspace" value="C:\eclipsecon\eclipse\workspace"/> <property name="tptp.test.project" value="TPTPJUnitTests" /> <property name="tptp.test.connection" value="tptp:rac://localhost:10002/default"/> <property name="tptp.automation.eclipse" value="C:\eclipseconwb_birt\eclipse" />
<!-- Define script-local properties, that vary by installation --> <property name="project.dir" value="${tptp.test.workspace}\${tptp.test.project}"/> <property name="report.publication.location" location="c:/temp" />
<target name="main" depends="test-execution, test-results-publication"/>
<!-- Execute test suites using default results name --> <target name="test-execution"> <echo message="Executing test suite..." /> <tptp:execution resultsrefid="tptp.test.results"> <fileset dir="${project.dir}">     <include name="**/*.testsuite"/> </fileset> </tptp:execution> </target>
<!-- Publish test suite results report -->
<target name="test-results-publication"> <echo message="Publishing test suite results report..." /> <tptp:publication report="${report.publication.location}/report.html">     <filelist refid="tptp.test.results"/> </tptp:publication> </target>
</project>

從 Ant Script 自訂測試報告

您可以指定不同的 BIRT 報告範本來自訂 tptp:publication 作業的行為,例如,在 org.eclipse.tptp.test.report.birt 外掛程式範本目錄中的 TabularReport.rptdesign。 另外,您也可以使用 publish-testsuites 發佈服務,不提供測試執行結果檔,而提供測試套組作為發佈服務的輸入來自訂行為。 如果您使用 publish-testsuites 發佈服務,且將測試套組檔案當作輸入來傳給發佈服務,您也必須指定報告視窗(用來查詢工作區,以找出這個時間視窗內的所有執行結果)的起始和結束日期。 下列範例顯示如何選取過去 60 天的每個執行結果,以及利用「列表格式」 報告設計來產生報告,以在 Test1 專案的所有測試套組上執行報告。

<project name="TPTP Automatable Services Tutorial"
default="main" xmlns:tptp="antlib:org.eclipse.hyades.automation.client.adapters.ant"> <!-- Define common properties for all automatable services --> <property name="tptp.test.workspace" value="C:\eclipsecon\eclipse\workspace"/> <property name="tptp.test.project" value="TPTPJUnitTests" /> <property name="tptp.test.connection" value="tptp:rac://localhost:10002/default"/> <property name="tptp.automation.eclipse" value="C:\eclipseconwb_birt\eclipse" />
<!-- Define script-local properties, that vary by installation --> <property name="project.dir" value="${tptp.test.workspace}\${tptp.test.project}"/> <property name="report.publication.location" location="c:/temp" /> <property name="tptp.publication.service" value="org.eclipse.tptp.test.report.birt.publish-testsuites"/>
<!-- Run the report --> <target name="main" depends="test-results-publication"/>
<target name="set-date-range">     <!-- Use standard ant tstamp mechanisms to specify the desired date range -->     <!-- This example uses a range between today and 60 days days prior -->     <tstamp>         <format property="startDateTime" pattern="MM/dd/yyyy hh:mm aa" offset="-60" unit="day"/>     </tstamp>     <tstamp>         <format property="endDateTime" pattern="MM/dd/yyyy hh:mm aa"/>     </tstamp> </target>
<!-- Publish test suite results report --> <target name="test-results-publication" depends="set-date-range"> <echo message="Publishing test suite results report..." /> <tptp:publication report="${report.dir}/report.html"     reportTemplateLocation="C:\TPTP_TestPass\ReportBuild\eclipse\plugins\org.eclipse.tptp.test.report.birt_4.2.0.v200605180959\templates\TabularReport.rptdesign"     startDateTime="${startDateTime}" endDateTime="${endDateTime}" >     <fileset dir="${project.dir}">         <include name="**/*.testsuite" />     </fileset> </tptp:publication> </target>
</project>

從指令行和 Shell Script 執行測試

從指令行和 Shell Script 中,採用指令行格式的測試內容,可以直接執行測試。 TPTP 自動化用戶端配接器提供適用於 Windows 的 .bat 和 .cmd 檔案,也提供適用於 Linux 作業系統的 .sh 檔案。 在配置檔中,可以指定大量引數及/或經常重複使用的引數,以減少從指令行執行測試執行服務時所需的打字次數。 Shell 自動化用戶端配接器以文字檔(批次檔或 Script)和補充性 Java 元件表示 -- 兩者必須存在,才能從指令行執行測試。 在 tptp-automation-client.jar 中可以找到所有 TPTP 提供的自動化用戶端配接器, 但 ant-tptp.jar 以 ant jar 的形式重複一些配接器程式碼(這是為了讓 TPTP 作業 jar 和其他可用的 ant 作業有一致的命名方式)。

設定測試執行服務的內容時,內容名稱前面加上連字號,以確定指定 Eclipse 起始目錄或根目錄。

從 Java 應用程式和外掛程式執行測試

從任何 Java 應用程式中可以啟動測試,事實上,其他用戶端自動化配接器在內部也使用 Java 配接器來連結服務匯流排,例如 ant 和 Shell 配接器。 從一些 Java 程式碼中執行測試的唯一需求是確定適當地參照 tptp-automation-client.jar(在 Eclipse 中,相當於表示程式庫必須在 Java 專案的建置路徑中), 當然,所有一般的 TPTP 需求也同樣適用,例如必須執行代理程式控制器。  

Java 自動化用戶端配接器提供非常簡單的 API,能夠以標準 Java 內容物件中所有可指定的內容來執行測試。 下列範例執行上一個 ant Script 範例中介紹的相同測試。 請注意,傳入執行方法中的服務 ID 是 TPTP 測試執行服務的服務 ID (此服務支援所有基本的 TPTP 測試類型)-- 也可能執行其他自動化服務(如果有的話),但必須變更下列程式碼中配置的 ID 和內容。

// Create the Java adapter associated with the specified Eclipse home
AutomationClientAdapter automation = new AutomationClientAdapter
  ("d:\\development\\eclipse");
  
// Create and configure a properties object
Properties properties = new Properties();
properties.setProperty("workspace", "D:\\development\\workspace");
properties.setProperty("project", "tests");
properties.setProperty("suite", "unit-tests/testA.testsuite");

// Execute the service named below using the configured properties
automation.execute("org.eclipse.hyades.test.tools.core.execute", properties);

相關概念
自動化服務架構的概觀

相關作業
從 Script 和應用程式啟動測試

相關參照
支援的測試執行服務內容