使用 TPTP 可自动化服务

TPTP 提供一些可以从 Eclipse 工作台的上下文外部调用的可自动化服务。这些服务包括测试执行、测试结果分析和测试结果发布。

此有用的主题说明如何使用内置 TPTP 可自动化服务来执行下列操作:

测试执行服务

提供测试执行服务的目的是以编程方式启动测试、执行测试和生成测试结果。测试执行服务对现有的测试执行装置功能进行包装并弃用当前独立测试执行装置方法。通过使用测试执行服务,可以使用无外设 Eclipse 来从 Ant 脚本、shell 脚本和任何程序中启动测试。当执行机器上有足够内存时,可以同时运行多个测试(目前,执行的每个服务都需要独立的 Eclipse 主机实例)。即使正在使用 TPTP 远程测试执行功能和部署来在远程机器上运行测试,服务使用者的机器也是运行 Eclipse 主机实例的机器。

服务属性

请参阅有关受支持测试执行服务属性的参考材料(不同客户机自动化适配器的有效属性是相同的)。对于 Ant 脚本来说,提供了一组简单的 Ant 任务来设置服务属性和执行服务。对于 shell 脚本来说,属性是按所需的命令行范例指定的(属性带有连字符,后跟空格,接着是实例值)。对于 Java 程序来说,将引用自动化客户机 jar 并使用它们来设置属性和控制服务的执行。某些客户机适配器可能具有该特定客户机环境所需的其他选项(或者作为该适配器的附加功能提供的选项)。

可自动化服务框架的打包

TPTP 可自动化服务组件作为 3 个 jar 文件、plugin.xml 中的一些 XML 以及 MANIFEST.MF 文件中的一些条目打包在 org.eclipse.hyades.execution 插件中。

  1. tptp-automation-client.jar(建立服务总线的客户端、根据需要启动 Eclipse 以及以无外设方式启动 Eclipse 所需的轻量级代码位。当从命令行和 shell 脚本中运行测试以及从任何 Java 代码中运行测试时,将引用这个 jar 文件)
  2. tptp-automation-server.jar(这是 TPTP 自动化服务器。当使用框架来查询和执行服务时,将根据需要装入此服务器。此服务器实现了用作代理和服务定位器的核心 Eclipse 应用程序扩展点)
  3. ant-tptp.jar(这个 jar 文件的内容与 tptp-automation-client.jar 相同,但已重命名为与其他 Ant 任务库一致)

从 Ant 脚本中运行测试

如果要在运行中的 Eclipse 实例外部运行测试,请确保正确地安装了 Ant。请参阅 Ant 文档以正确地进行设置。除了标准的 Ant 安装版本以外,还需要将 ant-tptp.jar 复制到 ant lib 目录以及完成其他任务。即使在 Eclipse 中运行并且已安装 Ant,也要确保在 Ant 启动配置的“其他任务”选项卡中包括 ant-tptp.jar,或在“Ant 类路径”首选项页的“全局条目”部分中包括 ant-tptp.jar。以下嵌入式样本 Ant 脚本是名为“test.xml”的 Ant 脚本示例,它执行测试项目的 unit-tests 文件夹中名为 testA 的测试。测试项目在指定的工作空间“d:\development\workspace”中,指定的 Eclipse 主机实例是“d:\development\eclipse”。请参阅受支持属性参考列表以了解其他有效属性组合。

以下示例使用 TPTP 测试任务,但也可以使用 TPTP 自动化任务来执行测试。更常用的自动化任务是用于执行任意 TPTP 可自动化服务的 Ant 任务,这与更为具体的 TPTP 测试任务形成对照。测试任务是专门为了启动测试执行服务而创建的类型安全任务。在 org.eclipse.hyades.execution 插件中,src-automation-client 源代码文件夹的 org.eclipse.hyades.automation.test 包提供了更多 Ant 脚本示例。

<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 脚本中运行多个测试并分析测试结果

确保已正确配置 Ant 来运行 TPTP 可自动化服务(请参阅上一节中的从 Ant 运行测试)。以下示例说明如何使用 tptp:execution 任务来执行多个测试,以及如何使用 tptp:interrogation 任务来分析这些测试执行的结果。这两个 Ant 任务都接受多个 Ant fileset 和/或 filelist,从而允许您使用通配符或直接将多个文件指定为任务的输入。在此示例中,执行 TPTPJUnitTests 项目中的所有测试套件,并将所有生成的 .execution 文件集作为 filelist 在提供的 Ant 变量(在此示例中,为“tptp.test.results”)中返回。然后,将该 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 脚本中生成测试报告

确保已正确配置 Ant 来运行 TPTP 可自动化服务(请参阅上一节中的从 Ant 运行测试)。以下示例说明如何使用 tptp:execution 任务来执行多个测试,以及如何使用 tptp:publication 任务来根据这些测试执行的结果生成基于 BIRT 的报告。发布任务也接受多个 Ant fileset 和/或 filelist,从而允许您使用通配符或直接将多个文件指定为输入。在此示例中,执行 TPTPJUnitTests 项目中的所有测试套件,并将所有生成的 .execution 文件集作为 filelist 在提供的 Ant 变量(在此示例中,为“tptp.test.results”)中返回。然后,将该 filelist 作为输入传递给 tptp:publication 服务,此服务将使用缺省报告模板(org.eclipse.tptp.test.report.birt 插件中的 TestExecution.rptdesign 模板)生成 TPTP 执行报告,并将该报告放置在指定位置(在此示例中,为 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 脚本中定制测试报告

可以通过指定另一个 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 脚本中运行测试

可以直接从命令行和 shell 脚本中执行测试并指定命令行参数格式的测试属性。TPTP 自动化客户机适配器提供了用于 Windows 的 .bat 和 .cmd 文件以及用于 Linux 操作系统的 .sh 文件。也可以在配置文件中指定大量的参数和/或经常重复的参数,以最大程度地减少从命令行运行测试执行服务时所必需的输入量。要从命令行执行测试,shell 自动化客户机适配器清单(这是一个文本文件,用于批处理文件或脚本)和补充的 Java 组件都必须存在。虽然 ant-tptp.jar 重复了某些 Ant jar 形式的适配器代码(这是为了保持 TPTP 任务 jar 的命名与其他可用的 Ant 任务一致),但还是可以在 tptp-automation-client.jar 中找到 TPTP 提供的所有自动化客户机适配器。

测试执行服务的属性是通过对属性名添加连字符前缀(从而确保还指定了 Eclipse 主目录或根目录)设置的。

从 Java 应用程序和插件中运行

可以从任何 Java 应用程序中启动测试,事实上,其他客户机自动化适配器(例如 Ant 和 shell 适配器)在内部使用 Java 适配器来连接到服务总线。要从某些 Java 代码中执行测试,只需要确保正确引用 tptp-automation-client.jar(在 Eclipse 中,这表示此库在 Java 项目的构建路径中)。当然,所有典型的 TPTP 要求(例如代理控制器必须正在运行)也都适用。  

Java 自动化客户机适配器提供了一个非常简单的 API 来执行测试,并且所有可指定的属性都包含在标准的 Java 属性对象中。以下示例执行上一个 Ant 脚本示例中引入的测试。注意,传递给执行方法的服务标识就是 TPTP 测试执行服务(此服务支持所有基本 TPTP 测试类型)的服务标识 - 如果有其他可自动化服务可用,也可以执行它们,但需更改以下代码中配置的标识和属性。

// 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);

相关概念
可自动化服务框架概述

相关任务
从脚本和应用程序中启动测试

相关参考
受支持的测试执行服务属性