使用脚本语言创作数据驱动的测试

若要了解本部分,应熟悉如何使用 脚本语言创作测试。 本部分不会讨论 各种 TAEF 数据驱动测试方法的详细信息。 有关快速概述,请查看不同的 TAEF 数据驱动测试构造:

你甚至可以选择通过具有上述任一项的一个或多个数据源来组合数据源。 有关详细信息 ,请参阅指定多个数据源

使用脚本语言指定数据源

TAEF 中的数据驱动测试允许在类或测试级别指定 DataSource 。 在数据驱动类中,数据可用于类和测试方法设置、清理以及类中的所有测试方法。 DataSource 参数是指示从何处检索数据的信息。 对于基于表的数据驱动测试,此值包括 XML 文件的相对路径和数据所在的 XML 文件中的 TableId。 有关更多详细信息,请参阅上面列出的链接。

以下示例演示如何指定 DataSource 属性。

1   <?xml version="1.0" ?>
2   <?component error="true" debug="true"?>
3   <package>
4       <component id="VBSampleTests">
5           <object id="Log" progid="WEX.Logger.Log" />
6           <object id="TestData" progid="Te.Common.TestData" />
7
8           <public>
9               <method name="TestOne">
10                  <TestMethodProperty name="DataSource" value="WMI:SELECT Label, Caption FROM Win32_Volume"/>
11              </method>
12
13              <method name="TestTwo">
14                  <TestMethodProperty name="DataSource" value="Table:ScriptExampleTable.xml#MyTable;WMI:SELECT Label, Caption FROM Win32_Volume"/>
15              </method>
16          </public>
17
18          <script language="VBScript">
19              <![CDATA[
20                  Function TestOne()
21                      dim caption
22                      caption = "NoCaption"
23                      Log.Comment("Caption is " + caption)
24
25                      If TestData.Contains("Caption") Then
26                      caption = TestData.GetValue("Caption")
27                      End If
28                      Log.Comment("Caption is " + caption)
29                  End Function
30
31                  Function TestTwo()
32                      Log.Comment("Calling TestTwo")
33                      dim caption
34                      caption = "NoCaption"
35                      Log.Comment("Caption is " + caption)
36
37                      If TestData.Contains("Caption") Then
38                      caption = TestData.GetValue("Caption")
39                      End If
40                      Log.Comment("Caption is " + caption)
41
42                      dim size
43                      If TestData.Contains("Size") Then
44                      size = TestData.GetValue("Size")
45                      End If
46                      Log.Comment("Size is " + CStr(size))
47
48                      dim transparency
49                      If TestData.Contains("Transparency") Then
50                      transparency = TestData.GetValue("Transparency")
51                      End If
52                      Log.Comment("Transparency is " + CStr(transparency))
53                  End Function
54              ]] >
55          </script>
56      </component>
57
58      <component id="JScriptSampleTests">
59          <object id="Log" progid="WEX.Logger.Log" />
60          <object id="TestData" progid="Te.Common.TestData" />
61
62          <TestClassProperty name="DataSource" value="Table:ScriptExampleTable.xml#MyTable"/>
63
64          <public>
65              <method name="ClassSetup" type="TestClassSetup"/>
66              <method name="ClassCleanup" type="TestClassCleanup"/>
67              <method name="MethodSetup"  type="TestMethodSetup"/>
68              <method name="MethodCleanup" type="TestMethodCleanup"/>
69
70              <method name="TestOne"/>
71              <method name="TestTwo">
72                  <TestMethodProperty name="DataSource" value="WMI:SELECT Label, Caption FROM Win32_Volume"/>
73              </method>
74          </public>
75
76          <script language="JScript">
77              <![CDATA[
78                  function ClassSetup()
79                  {
80                      Log.Comment("Calling class setup");
81                      var size;
82                      if(TestData.Contains("Size"))
83                      {
84                          size = TestData.GetValue("Size");
85                      }
86                      Log.Comment("Size is " + size);
87  
88                      var transparency;
89                      if(TestData.Contains("Transparency"))
90                      {
91                          transparency = TestData.GetValue("Transparency");
92                      }
93                      Log.Comment("Transparency is " + transparency);
94                  }
95
96                  function ClassCleanup()
97                  {
98                      Log.Comment("Calling class cleanup");
99                      return true;
100                 }
101
102                 function MethodSetup()
103                 {
104                     Log.Comment("Calling method setup");
105                     var size;
106                     if(TestData.Contains("Size"))
107                     {
108                         size = TestData.GetValue("Size");
109                     }
110                     Log.Comment("Size is " + size);
111
112                     var transparency;
113                     if(TestData.Contains("Transparency"))
114                     {
115                         transparency = TestData.GetValue("Transparency");
116                     }
117                     Log.Comment("Transparency is " + transparency);
118                     return true;
119                 }
120
121                 function MethodCleanup()
122                 {
123                     Log.Comment("Calling method cleanup");
124                     return true;
125                 }
126
127                 function TestOne()
128                 {
129                     Log.Comment("Calling TestOne");
130                     var size;
131                     if(TestData.Contains("Size"))
132                     {
133                         size = TestData.GetValue("Size");
134                     }
135                     Log.Comment("Size is " + size);
136
137                     var transparency;
138                     if(TestData.Contains("Transparency"))
139                     {
140                         transparency = TestData.GetValue("Transparency");
141                     }
142                     Log.Comment("Transparency is " + transparency);
143                 }
144
145                 function TestTwo()
146                 {
147                     Log.Comment("Calling TestTwo");
148                     var caption = "NoCaption";
149                     Log.Comment("Initial caption: " + caption);
150
151                     if(TestData.Contains("Caption"))
152                     {
153                         caption = TestData.GetValue("Caption");
154                     }
155                     Log.Comment("Caption is " + caption);
156
157                     var size;
158                     if(TestData.Contains("Size"))
159                     {
160                         size = TestData.GetValue("Size");
161                     }
162                     Log.Comment("Size is " + size);
163
164                     var transparency;
165                     if(TestData.Contains("Transparency"))
166                     {
167                         transparency = TestData.GetValue("Transparency");
168                     }
169                     Log.Comment("Transparency is " + transparency);
170                 }
171             ]] >
172         </script>
173     </component>
174 </package>

在上面的示例中,第 6 行和第 60 行声明并实例化一个 TestData 对象,该对象允许访问数据驱动测试的数据。

<TestMethodProperty><TestClassProperty> 标记是为测试或类定义 DataSource 的行。 在 VBSampleTests 中, TestOneWMI 查询 作为其 数据源。 参数标签描述文字可用于 TestOne 的设置、清理和测试方法。 在同一类中, TestTwo 定义了 多个数据源 。 第一个是基于 表的数据源,第二个是基于 WMI 的 DataSourceTestOne 相同。

TAEF 为每个 DataSource 属性生成参数集的组合扩展。 每个测试方法调用都有一个参数集。 如果 WMI 查询 (Win32_Volume) 返回四组结果,并且基于表的 DataSource 中有三行, 则 TestOne 将执行四次 --WMI 查询返回的每个Win32_Volume一次。 另一方面, TestTwo 对表指定的Win32_Volume数据和 Row 的每个组合执行 12 (4 X 3) 次。 数据还可用于关联的设置和清理方法。

在 JScriptSampleTests 中,可以看到数据驱动类的示例。 由于该示例在类级别指定 DataSource ,因此数据可用于所有测试方法以及测试和类级别的设置和清理方法。 由于 TestTwo 是数据驱动类中的数据驱动测试,因此类级别的 DataSource 中的数据以及测试级别的数据可用于 TestTwo

可用于脚本测试的数据类型

以下参数类型可用于脚本语言。 这些是在基于表的数据驱动测试中可以指定的类型。 默认参数类型为 StringBSTR (,表示 VT_BSTR) 。

基于表的 DataSource 中的参数类型部分介绍了在以脚本语言创作测试时,如何在本机和托管代码) 中查看可用参数类型 (。

执行数据驱动的脚本

/listproperties 选项不仅列出了元数据,还列出了可用于每次调用测试的数据。 (对整个 dll 运行 /listproperties 选项是留给 reader 的练习。) 以下示例使用选择查询语言从 VBSampleTests 中选择 TestOne 的调用:

f:\spartadev.binaries.x86chk\WexTest\CuE\TestExecution>te Examples\DataDrivenTest.wsc /listproperties /name:VBSampleTests::TestOne*

Test Authoring and Execution Framework v.R10 Build 6.1.6939.0 For x86

        f:\spartadev.binaries.x86chk\WexTest\CuE\TestExecution\Examples\DataDrivenTest.wsc
            VBSampleTests
                VBSampleTests::TestOne#0
                        Property[DataSource] = WMI:SELECT Label, Caption FROM Win32_Volume

                        Data[Caption] = C:\
                        Data[Label] =

                VBSampleTests::TestOne#1
                        Property[DataSource] = WMI:SELECT Label, Caption FROM Win32_Volume

                        Data[Caption] = D:\
                        Data[Label] = New Volume

                VBSampleTests::TestOne#2
                        Property[DataSource] = WMI:SELECT Label, Caption FROM Win32_Volume

                        Data[Caption] = F:\
                        Data[Label] = New Volume

                VBSampleTests::TestOne#3
                        Property[DataSource] = WMI:SELECT Label, Caption FROM Win32_Volume

                        Data[Caption] = E:\
                        Data[Label] = New Volume

                VBSampleTests::TestOne#4
                        Property[DataSource] = WMI:SELECT Label, Caption FROM Win32_Volume

                        Data[Caption] = G:\
                        Data[Label] = New Volume

                VBSampleTests::TestOne#5
                        Property[DataSource] = WMI:SELECT Label, Caption FROM Win32_Volume

                        Data[Caption] = H:\
                        Data[Label] = New Volume

                VBSampleTests::TestOne#6
                        Property[DataSource] = WMI:SELECT Label, Caption FROM Win32_Volume

                        Data[Caption] = K:\
                        Data[Label] =

/listproperties 选项显示 TAEF 调用测试方法 VBSampleTests::TestOne 7 次 - 每个Win32_Volume一次。 对于每个调用,TAEF 都会向测试方法追加一个隐式 索引 ,以区分每个调用。 还可以查看可用于每次调用测试方法的数据和元数据。

使用 /listproperties 选项中的信息,可以应用基于数据值或索引值的选择查询,以便更精细地控制要执行哪些测试调用。 以下示例演示如何仅运行描述文字为 E:\的调用:

te Examples\DataDrivenTest.wsc /select:"@Name='VBSampleTests::TestOne*' and @Data:Caption='E:\'"

以下命令使用 索引来选择相同的测试:

te Examples\DataDrivenTest.wsc /select:"@Name='VBSampleTests::TestOne*' and @Data:Index=3"

在脚本测试中使用基于 PICT 的轻量级数据驱动测试留给读者练习。