共用方式為


使用腳本語言撰寫測試

除了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"?>
  • 錯誤 設定為 true ,以顯示文稿元件中語法或運行時間錯誤的詳細錯誤訊息。
  • [偵錯 ] 設定為 true 以啟用偵錯。 如果未啟用偵錯,您無法啟動腳本的腳本調試程式(例如 JScript 程式代碼中的 debug 關鍵詞)。

<package> 元素會在 .wsc 檔案中括住測試類別定義。 在這個項目之後,您可以藉由新增 ModuleProperty 元素來插入模組層級元資料:

<ModuleProperty name = "Owner" value = "Someone"/>

ModuleProperty 元素必須包含名稱和屬性。

Component 元素會啟動腳本測試類別的宣告。 這個項目應該一律有設定為類別名稱的 id 屬性。

在 Component 元素之後,您可以使用 TestClassProperty 元素插入類別層級元數據。 如同ModuleProperty元素,它必須具有名稱和屬性。

此時,您也可以建立 物件,並定義對象的參考。 如需詳細資訊,請參閱 其他元件一節 。 XML 範例中的行 15、18、49 和 52 會顯示如何參考和初始化 WEX。Logger.Log 物件。

<公共>元素包含測試腳本模組的測試方法宣告。 您可以在方法項目 的名稱 屬性 <中指定測試方法名稱,以宣告測試方法> 。 您也可以在<方法>元素內新增測試方法屬性。 如同其他層級的屬性,它並非必需。 不過,如果您新增它,則必須包含名稱和屬性。

<腳本>元素識別測試腳本語言,並包含測試方法的實作。

<![CDATA[]]> 區段包含測試的實際實作 - 以腳本語言撰寫的程序代碼。 在本節中,您會實作您在 public></public> 區段中宣告<的測試方法。