請先確定您已熟悉 TAEF 的基本執行,並知道如何利用 編寫測試,再繼續進行本節。 您可能也想查看《使用者指南》中列出的一些資料驅動測試範例的逐步指南。
使用 TAEF 進行Scenario-Based 測試
當您談論場景級測試時,您實際上是在談論一系列測試,其中只有當場景中的前一個測試成功時,執行下一個測試才有意義。 在某些情況下,如果上一個測試失敗,您甚至可能沒有執行下一個測試所需的所有資訊。 為此,在將執行單位保留為測試方法並允許測試案例的同時,TAEF 支援所謂的「ExecutionGroup」。 在 TAEF 中,您可以進行案例型測試,同時仍然享有其他功能,例如資料驅動測試。 如果您設計案例以利用數據驅動測試,您可以使用 TAEF 所提供的數據驅動類別功能,在類別層級套用數據驅動支援。 藉由在類別層級套用資料驅動支援,您可以針對每一列循序執行類別內的所有測試。
本頁將重點介紹如何將類別中的測試序列指定為「ExecutionGroup」。
執行群組
在討論執行群組之前,請務必注意並記住, 在 TAEF 中,類別內測試的執行順序是您在原生程式碼的情況下將它們限定為 TEST_METHOD (...) 的順序,或在 Managed 程式碼的情況下在方法之前新增 [TestMethod] 屬性的順序。 TAEF 不保證類別本身的執行順序。
現在,在基於場景的測試中,僅保證執行順序可能還不夠,您還需要保證場景中所有先前的測試都成功,然後才能繼續進行場景中的下一個測試。 這是您會發現“ExecutionGroup”概念有用的地方。
考慮一個原生範例:
1 class ExecutionDependencyExample
2 {
3 BEGIN_TEST_CLASS(ExecutionDependencyExample)
4 TEST_CLASS_PROPERTY(L"ExecutionGroup", L"DependentTests")
5 END_TEST_CLASS()
6
7 TEST_METHOD(Test1)
8 {
9 Log::Comment(L"Test1 passes.");
10 }
11
12 TEST_METHOD(Test2)
13 {
14 Log::Comment(L"Test2 fails.");
15 VERIFY_ARE_EQUAL(2, 3);
16 }
17
18 TEST_METHOD(Test3)
19 {
20 Log::Comment(L"Test3 is blocked; so you shouldn't see this.");
21 }
22 };
請參閱上述 C++ 檔案片段中的第 4 行。 在此特定案例中,您會將類別 ExecutionDependencyExample 內的所有測試限定為屬於名為 “DependentTests” 的 “ExecutionGroup”。 這表示 “Test1”、“Test2” 和 “Test3” 是 “DependentTests” 執行群組的一部分。 如前所述,當且僅當 Test1 成功執行並通過時,Test2 才會被執行。 同樣地,當且僅當 Test2 成功執行並通過時,才會執行 Test3。
您會看到 Test2 已設計為失敗 (請參閱上面的第 14 行和第 15 行)。
由於 Test2 在我們的 “DependentTests” “ExecutionGroup” 中失敗,因此 Test3 將不會被執行,而是被標記為已阻止。 讓我們嘗試運行上面的測試,看看這是否確實如此。
te Examples\CPP.ExecutionDependency.Example.dll
Test Authoring and Execution Framework v2.93k for x86
StartGroup: WEX::TestExecution::Examples::ExecutionDependencyExample::Test1
Test1 passes.
EndGroup: WEX::TestExecution::Examples::ExecutionDependencyExample::Test1
[Passed]
StartGroup: WEX::TestExecution::Examples::ExecutionDependencyExample::Test2
Test2 fails.
Error: Verify: AreEqual(2, 3) - Values (2, 3) [File: >f:source\executiondependencyexample\executiondependencyexample.cpp,
Function: WEX::TestExecution::Examples::ExecutionDependencyExample::Test2, Line:21]
EndGroup: WEX::TestExecution::Examples::ExecutionDependencyExample::Test2[Failed]
StartGroup: WEX::TestExecution::Examples::ExecutionDependencyExample::Test3
Blocked: This test belongs to an execution group and depends on the previous test being executed in the same environment successfully. The dependent test must be selected for execution, must request the same execution environment (e.g. 'ThreadingModel') and must be executed successfully.
EndGroup: WEX::TestExecution::Examples::ExecutionDependencyExample::Test3 [Blocked]
Non-passing Tests:
WEX::TestExecution::Examples::ExecutionDependencyExample::Test2 [Failed]
WEX::TestExecution::Examples::ExecutionDependencyExample::Test3 [Blocked]
Summary: Total=3, Passed=1, Failed=1, Blocked=1, Not Run=0, Skipped=0
請注意,如預測的那樣,Test1 通過,Test2 失敗,而 Test3 被阻止。 使用 Test3 時,TAEF 會記錄一則訊息,指出 Test3 屬於執行群組,而且先前的測試未成功執行。
此錯誤訊息也指出,在執行目前測試之前,應該選取屬於相同 ExecutionGroup 的所有測試。 換句話說,如果您嘗試在運行時使用選擇條件只運行 Test2,您會發現 Test2 將被阻止,因為它的執行依賴於 Test1,是同一 ExecutionGroup 的一部分。
te Examples\CPP.ExecutionDependency.Example.dll /name:*Test2*
Test Authoring and Execution Framework v2.9.3k for x86
StartGroup: WEX::TestExecution::Examples::ExecutionDependencyExample::Test2
Blocked: This test belongs to an execution group and depends on the previous test being executed in the same environment successfully. The dependent test must be selected for execution, must request the same execution environment (e.g. 'ThreadingModel') and must be executed successfully.
EndGroup: WEX::TestExecution::Examples::ExecutionDependencyExample::Test2 [Blocked]
Summary: Total=1, Passed=0, Failed=0, Blocked=1, Not Run=0, Skipped=0
不過,如果您選取 Test1,這是 ExecutionGroup 中的第一個測試,它將會成功執行。
te Examples\CPP.ExecutionDependency.Example.dll /name:*Test1*
Test Authoring and Execution Framework v2.9.3k for x86
StartGroup: WEX::TestExecution::Examples::ExecutionDependencyExample::Test1
Test1 passes.
EndGroup: WEX::TestExecution::Examples::ExecutionDependencyExample::Test1 [Passed]
Summary: Total=1, Passed=1, Failed=0, Blocked=0, Not Run=0, Skipped=0
此外,如果您有不屬於 ExecutionGroup 的測試,則無論 ExecutionGroup 內繼續進行測試的執行結果為何,都會執行這些測試。 類別內也可以有多個 ExecutionGroup。 不過請注意,ExecutionGroup 無法跨越類別。 如果您這麼做,它們將會被視為兩個獨立的 ExecutionGroups,每個類別之中各有一個。
該消息還說 Test3 應該在與 Test2 相同的環境中運行。 讓我們嘗試更詳細地了解這方面。 由於成為 ExecutionGroup 的一部分實際上意味著成為基於場景的測試的一部分,因此所有的測試必須在相同的環境中提出請求並執行,這一點變得至關重要。 例如,如果執行緒模型在 ExecutionGroup 內變更,您會看到封鎖的測試。 例如,如果在上述範例中,Test2 設計為成功執行,但將 'ThreadingModel' 屬性設定為 'MTA',則 Test3 仍會遭到封鎖。
讓我們考慮另一個範例:Examples\TAEF\CSharp\ExecutionDependentGroupsExample (請參閱最新的 TAEF 版本分享)
1 [TestClass]
2 public class CSharpExecutionDependentGroupsExample
3 {
4 //First Execution Group: Test1, Test2
5 [TestMethod]
6 [TestProperty("ExecutionGroup", "First Execution Group")]
7 public void Test1()
8 {
9 Log.Comment("Part of First Execution Group");
10 }
11 [TestMethod]
12 [TestProperty("ExecutionGroup", "First Execution Group")]
13 public void Test2()
14 {
15 Log.Comment("Part of First Execution Group");
16 }
17
18 //Second Execution Group: Test3, Test4. Test4 fails
19 [TestMethod]
20 [TestProperty("ExecutionGroup", "Second Execution Group")]
21 public void Test3()
22 {
23 Log.Comment("Part of Second Execution Group");
24 }
25 [TestMethod]
26 [TestProperty("ExecutionGroup", "Second Execution Group")]
27 public void Test4()
28 {
29 Log.Comment("Part of Second Execution Group - last in group fails");
30 Verify.IsTrue(false);
31 }
32
33 //Third Execution Group: Test5, Test6, Test7. Test6 fails, Test7 will be blocked.
34 [TestMethod]
35 [TestProperty("ExecutionGroup", "Third Execution Group")]
36 public void Test5()
37 {
38 Log.Comment("Part of Third Execution Group");
39 }
40 [TestMethod]
41 [TestProperty("ExecutionGroup", "Third Execution Group")]
42 public void Test6()
43 {
44 Log.Comment("Part of Third Execution Group - middle in this set of 3 fails");
45 Verify.IsTrue(false);
46 }
47 [TestMethod]
48 [TestProperty("ExecutionGroup", "Third Execution Group")]
49 public void Test7()
50 {
51 Log.Comment("Part of Third Execution Group");
52 }
53
54 //Fourth Execution Group: Test8, Test9
55 [TestMethod]
56 [TestProperty("ExecutionGroup", "Fourth Execution Group")]
57 public void Test8()
58 {
59 Log.Comment("Part of Fourth Execution Group");
60 }
61 [TestMethod]
62 [TestProperty("ExecutionGroup", "Fourth Execution Group")]
63 public void Test9()
64 {
65 Log.Comment("Part of Fourth Execution Group");
66 }
67 }
此範例有 4 個不同的執行群組:
- 「第一個執行群組」包含 Test1、Test2;這兩者都應該成功通過。
- 「第二個執行群組」包含 Test3 和 Test4。 Test4 是此 ExecutionGroup 中的最後一個測試,而且會失敗。
- 「第三執行組」包含Test5、Test6及Test7。 Test5 會執行並成功通過,雖然前一個 ExecutionGroup 中的 Test4 失敗。 Test6 被設計為失敗,這將導致 Test7 被封鎖。
- 「第四執行組」包含Test8和Test9。 再一次,儘管由於 Test6 失敗,來自上一個 ExecutionGroup 的 Test7 被阻止,但 Test8 將成功執行,Test9 也將成功執行。
為了更好地理解此範例中的 ExecutionGroups,讓我們列出此範例中的屬性。
te Examples\CSharp.ExecutionDependentGroups.Example.dll /listproperties
Test Authoring and Execution Framework v2.9.3k for x86
F:\ \Examples\CSharp.ExecutionDependentGroups.Example.dll
WEX.Examples.CSharpExecutionDependentGroupsExample
WEX.Examples.CSharpExecutionDependentGroupsExample.Test1
Property[ExecutionGroup] = First Execution Group
WEX.Examples.CSharpExecutionDependentGroupsExample.Test2
Property[ExecutionGroup] = First Execution Group
WEX.Examples.CSharpExecutionDependentGroupsExample.Test3
Property[ExecutionGroup] = Second Execution Group
WEX.Examples.CSharpExecutionDependentGroupsExample.Test4
Property[ExecutionGroup] = Second Execution Group
WEX.Examples.CSharpExecutionDependentGroupsExample.Test5
Property[ExecutionGroup] = Third Execution Group
WEX.Examples.CSharpExecutionDependentGroupsExample.Test6
Property[ExecutionGroup] = Third Execution Group
WEX.Examples.CSharpExecutionDependentGroupsExample.Test7
Property[ExecutionGroup] = Third Execution Group
WEX.Examples.CSharpExecutionDependentGroupsExample.Test8
Property[ExecutionGroup] = Fourth Execution Group
WEX.Examples.CSharpExecutionDependentGroupsExample.Test9
Property[ExecutionGroup] = Fourth Execution Group
當您執行上述測試時,下列輸出會確認預測的執行順序。
te Examples\CSharp.ExecutionDependentGroups.Example.dll
Test Authoring and Execution Framework v2.9.3k for x86
StartGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test1
Part of First Execution Group
EndGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test1 [Passed]
StartGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test2
Part of First Execution Group
EndGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test2 [Passed]
StartGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test3
Part of Second Execution Group
EndGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test3 [Passed]
StartGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test4
Part of Second Execution Group - last in group fails
Error: Verify: IsTrue [File: Need_Symbols, Function: Test4, Line: 0]
Error: [HRESULT: 0x80131604]. Operation failed: 'WEX.Examples.CSharpExecutionDependentGroupsExample.Test4'.
EndGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test4 [Failed]
StartGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test5
Part of Third Execution Group
EndGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test5 [Passed]
StartGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test6
Part of Third Execution Group - middle in this set of 3 fails
Error: Verify: IsTrue [File: Need_Symbols, Function: Test6, Line: 0]
Error: [HRESULT: 0x80131604]. Operation failed: 'WEX.Examples.CSharpExecutionDependentGroupsExample.Test6'.
EndGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test6 [Failed]
Error: WEX.Examples.CSharpExecutionDependentGroupsExample.Test7 belongs to an execution group and depends
on the previous test being executed in the same environment successfully.
Error: Please make sure that the dependent test is selected for execution, requests the same execution .
environment metadata(e.g. 'ThreadingModel') and that it executed successfully.
StartGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test7
Blocked EndGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test7 [Blocked]
StartGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test8
Part of Fourth Execution Group
EndGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test8 [Passed]
StartGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test9
Part of Fourth Execution Group
EndGroup: WEX.Examples.CSharpExecutionDependentGroupsExample.Test9 [Passed]
Failed Tests:
WEX.Examples.CSharpExecutionDependentGroupsExample.Test4
WEX.Examples.CSharpExecutionDependentGroupsExample.Test6
Summary: Total=9, Passed=6, Failed=2, Blocked=1, Not Run=0, Skipped=0
請注意,測試執行順序如預期般。