在继续执行本部分之前,请确保熟悉 TAEF 的基本操作,并了解如何用它 编写测试。 你可能还需要浏览用户指南中列出的一些数据驱动测试示例演练。
使用 TAEF 进行Scenario-Based 测试
在谈到方案级测试时,你实际上正在讨论一系列测试,只有在方案中的上一个测试成功时,执行下一个测试才有意义。 在某些情况下,如果上一个测试失败,甚至可能没有执行下一个测试所需的所有信息。 为此,在将执行单元保留为测试方法并允许测试方案的同时,TAEF 支持所谓的“ExecutionGroup”。 您可以在 TAEF 中使用基于场景的测试,而不影响具有数据驱动测试等其他功能。 如果设计方案以利用数据驱动测试,则可以使用 TAEF 提供的数据驱动类功能在类级别应用数据驱动支持。 通过在类级别应用数据驱动支持,可以按顺序为每个行执行类中的所有测试。
本页将重点介绍如何将类中的测试序列指定为“ExecutionGroup”。
执行组
在讨论执行组之前,请务必注意并记住,在 TAEF 中,类中测试的执行顺序,取决于在本机代码情况下,将其限定为 TEST_METHOD(...) 的顺序,或在托管代码情况下,在方法之前添加 [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,每个类各有一个。
消息还显示 Test3 应与 Test2 在同一环境中运行。 让我们尝试更详细地了解这一方面。 由于成为 ExecutionGroup 的一部分实际上意味着成为情景驱动的测试的一部分,因此所有测试必须在同一环境中请求并执行,这一点至关重要。 例如,如果 Threading Model 在 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。 再次,尽管上一个 ExecutionGroup 中的 Test7 由于 Test6 失败而被阻止,但 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
请注意,测试执行顺序按预期排列。