.tspec 文件包含 ScaTestSpecification 的 XML 格式。与我们先前编写的 PostCode 测试对应的规范为:
<?xml version="1.0" encoding="UTF-8" ?> <ScaTest:TestDefinition xsi:type="ScaTestFramework:ScaTestSpecification" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ScaTestFramework="http://LT_Utilities" xmlns:ScaTest="http://scatest/issw/ibm/com"> <service>I_PostcodePartner</service> <method>getDetailsForPostCode</method> <payloadString>BR1 0AB</payloadString> <description>Get details for good postcode</description> <expectedResultFile>GoodPostCodeDetails.xml</expectedResultFile> </ScaTest:TestDefinition> |
这里您可以看到服务合作伙伴 I_PostcodePartner 和方法 getDetailsForPostCode 以及输入字符串 BR1 0AB。您还可以看到在独立文件(在接下来的部分中进行讨论)中指定了预期结果,即带有多个字段的数据对象。另请注意,测试具有文字描述;在测试执行时,会将这些描述写入日志,这有助于在错误发生时能够将错误与特定的测试相关联。
可以用类似的方法指定 testBadPostCodeExternal,我们指定一个不存在的 PostCode 就会导致一个特定的错误:
<?xml version="1.0" encoding="UTF-8" ?> <ScaTest:TestDefinition xsi:type="ScaTestFramework:ScaTestSpecification" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ScaTestFramework="http://LT_Utilities" xmlns:ScaTest="http://scatest/issw/ibm/com"> <service>I_PostcodePartner</service> <method>getDetailsForPostCode</method> <payloadString>Bad Post Code</payloadString> <description>Bad postcode - should fault</description> <expectedFaultName>notFound</expectedFaultName> </ScaTest:TestDefinition>
|
要在测试数据文件夹中创建 tspec 文件,请右键单击该文件夹,然后选择 New => Other => Simple=> File,并粘入所显示的内容。您可能注意到,创建的此测试只是简单地复制并修改了一下先前的测试。
D. 创建配置文件,以描述有效负载和预期的结果
testGoodPostCodeExternal 测试需要预期的返回数据的规范,即 PostCodeDetails 数据类型。测试规范包括下面的行:
<expectedResultFile>GoodPostCodeDetails.xml</expectedResultFile>
引用文件 GoodPostCodeDetails.xml,需要将该文件放置在与测试规范相同的位置。
该文件的内容应为:
<?xml version="1.0" encoding="UTF-8"?> <_:TestDefinition xsi:type="l:PostCodeDetails" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance>" xmlns:l="http://L_MailService" xmlns:_="http://scatest/issw/ibm/com"> <postCode>BR1 0AB</postCode> <city>Bradford</city> <county>Yorkshire</county> <street>Haworth Terrace</street> <addressList> <address>12, Haworth Terrace</address> </addressList> <addressList> <address>13, Haworth Terrace</address> </addressList> </_:TestDefinition>
|
通过右键单击该文件夹,然后选择 New => Other => Simple => File,并粘入所显示的内容,可创建预期的结果文件。在接下来的部分中将描述创建此类文件的技巧。不过,测试中有了此文件后,我们就可以执行了。如果我们先前更改了测试代码,您将注意到,Web 应用程序会在服务器上重新启动,并且控制台将显示类似以下的消息:
[22/05/06 12:40:35:533 BST] 00000051 ApplicationMg A WSVR0221I: Application
started: MT_TestMailServiceApp
所以,新版本的应用程序及其关联的测试数据将自动部署到服务器。
现在可以使用以下几种方法启动测试:
右键单击类,然后选择 Run => JUnit Test。
从工具栏中的 Run 下拉列表(图 16)中选择 PostCodeTest。
图 16. 从工具栏执行测试
从 JUnit 视图选择 Rerun Last Test 图标。图 17 显示了此图标以及测试执行的结果。
图 17. JUnit 视图、测试执行和结果