使用脚本语言创作测试
除 C++ 和 C# 外,TAEF 还支持使用脚本语言创作测试。
使用支持 Microsoft COM 脚本接口的任何脚本语言创建脚本组件。 支持这些接口的脚本语言包括 JScript、Microsoft Visual Basic Scripting Edition (VBScript) 、PERLScript、PScript、Ruby 和 Python。
脚本测试创作的当前限制
Windows 开箱即用,仅支持 JScript 和 VBScript。
脚本测试文件格式
对于脚本语言测试,TAEF 使用略微修改Windows 脚本组件文件格式。 以下示例演示包含 VBScript 和 JScript 测试类的测试文件。
1 <?xml version="1.0" ?>
2
3 <!-- Debugging helpers -->
4 <!-- error Set this to true to display detailed error messages for syntax or run-time errors in the script component.-->
5 <!-- debug Set this to true to enable debugging. If this debugging is not enabled, you cannot launch the script debugger for a script -->
6 <?component error="true" debug="true"?>
7
8 <package>
9 <!-- Test module metadata -->
10 <ModuleProperty name="Owner" value="Someone"/>
11
12 <!-- Define a test class -->
13 <component id="VBSampleTests">
14 <!-- define and instantiate a logger -->
15 <object id="Log" progid="WEX.Logger.Log" />
16
17 <!-- include a reference to the logger so you could use the constants defined in logger library -->
18 <reference guid="e65ef678-a232-42a7-8a36-63108d719f31" version="1.0"/>
19
20 <!-- Test class metadata -->
21 <TestClassProperty name="DocumentationUrl" value="http://shelltestkb/"/>
22
23 <public>
24 <!-- Define a test method with metadata -->
25 <method name="TestOne">
26 <!-- Test method metadata -->
27 <TestMethodProperty name="Priority" value="1"/>
28 </method>
29
30 <!-- Define a test method without metadata -->
31 <method name="TestTwo"/>
32 </public>
33
34 <script language="VBScript">
35 <![CDATA[
36 Function TestOne()
37 Log.Comment("Calling TestOne")
38 End Function
39
40 Function TestTwo()
41 Log.Comment("Calling TestTwo")
42 End Function
43 ]] >
44 </script>
45 </component>
46
47 <!-- Define another test class -->
48 <component id="JScriptSampleTests">
49 <object id="Log" progid="WEX.Logger.Log" />
50
51 <!-- need reference to use logger constants -->
52 <reference guid="e65ef678-a232-42a7-8a36-63108d719f31" version="1.0"/>
53
54 <public>
55 <!-- Test setup and cleanup methods are declared using corresponding type = '' attributes -->
56 <method name="ClassSetup" type="TestClassSetup"/>
57 <method name="ClassCleanup" type="TestClassCleanup"/>
58 <method name="MethodSetup" type="TestMethodSetup"/>
59 <method name="MethodCleanup" type="TestMethodCleanup"/>
60
61 <method name="TestOne"/>
62 <method name="TestTwo"/>
63 </public>
64
65 <!-- Setup and Cleanup methods return false on failure -->
66 <script language="JScript">
67 <![CDATA[
68 function ClassSetup()
69 {
70 Log.Comment("Calling class setup");
71 return true;
72 }
73
74 function ClassCleanup()
75 {
76 Log.Comment("Calling class cleanup");
77 return true;
78 }
79
80 function MethodSetup()
81 {
82 Log.Comment("Calling method setup");
83 return true;
84 }
85
86 function MethodCleanup()
87 {
88 Log.Comment("Calling method cleanup");
89 return true;
90 }
91
92 function TestOne()
93 {
94 Log.Comment("Calling TestOne");
95
96 // For the purpose of demonstration, declare the test failed
97 Log.Result(TestResult_Failed);
98 }
99
100 function TestTwo()
101 {
102 Log.Comment("Calling TestTwo");
103 }
104 ]] >
105 </script>
106 </component>
107 </package>
此示例是一个 XML 文件,以普通 XML 标头开头:
<?xml version="1.0" ?>
可以通过设置属性 错误 并调试来配置文件的 调试设置:
<?component error="true" debug="true"?>
- 将 error 设置为 true 可显示脚本组件中语法或运行时错误的详细错误消息。
- 将 debug 设置为 true 以启用调试。 如果未启用调试,则无法为脚本 (启动脚本调试程序,例如使用 JScript 代码) 中的调试关键字 (keyword) 。
<package> 元素将测试类定义包含在 .wsc 文件中。 在此元素之后,可以通过添加 ModuleProperty 元素来插入模块级元数据:
<ModuleProperty name = "Owner" value = "Someone"/>
ModuleProperty 元素必须包含名称和值属性。
Component 元素启动脚本测试类的声明。 此元素应始终具有设置为类名的 id 属性。
在 Component 元素之后,可以使用 TestClassProperty 元素插入类级元数据。 与 ModuleProperty 元素一样,它必须具有名称和值属性。
此时,还可以创建对象并定义对对象的引用。 有关详细信息 ,请参阅其他组件部分 。 XML 示例中的第 15、18、49 和 52 行演示如何引用和初始化 WEX。Logger.Log 对象。
公共<>元素包含测试脚本模块的测试方法声明。 通过在方法元素的 name 属性<中指定测试方法名称来声明测试方法>。 还可以在方法元素中添加测试方法<>属性。 与其他级别的属性一样,这不是必需的。 但是,如果添加它,则必须包含名称和值属性。
script<> 元素标识测试脚本语言,并包含测试方法的实现。
!<[CDATA[]]> 节包含测试的实际实现 - 以脚本语言编写的代码。 在本部分中,你将实现在 public></public> 节中<声明的测试方法。