选择

测试编写和执行框架 (TAEF) 提供了一种机制,可根据提供的元数据信息有选择地运行或省略某些测试。 下文将举例说明如何在 TE.exe 中使用这种选择机制。

可以从命令提示符窗口运行 TE.exe。

TE <test_binaries> [/select:<selection criteria>]

本部分介绍 TE.exe /select:selection criteria 选项。 有关 TE.exe 的详细信息,请参阅 TE.exe 命令选项

选择条件将全面应用于命令提示符中提到的所有测试二进制文件。 让我们考虑两个 test_binaries:Examples\CPP.SelectionCriteria1.Example.dllExamples\CPP.SelectionCriteria2.Example.dll。 以下示例显示了在这些 test_binaries 中指定的各级属性或元数据。 它也可以通过在命令提示符窗口中指定 /listproperties 选项来获取。

CPP.SelectionCriteria1.Example.dll (Owner="C1", Priority=3)
class11 (Owner="C2")
method111(Priority=1)

method112 (BackwardsCompatibility="Windows 2000")
class12
method121
CPP.SelectionCriteria2.Example.dll (Owner="WEX")
class21 (Owner="C1", Priority=2, BackwardsCompatibility="Windows XP")
method211 (Owner="C2")
class22 (Owner="U3")
method221

换句话说,在每一个 test_binaries 上分别使用 /listproperties 后,就可以得到:

F:\fsd1.binaries.x86chk\WexTest\C1\TestExecution>te Examples\CPP.SelectionCriteria1.Example.dll /listproperties
Test Authoring and Execution Framework v2.2 Build 6.1.7689.0 (release.091218-1251) for x86

F:\fsd1.binaries.x86chk\WexTest\C1\TestExecution\Examples\CPP.SelectionCriteria1.Example.dll
        Property[Owner] = C1
        Property[Priority] = 3
    WEX::TestExecution::Examples::Class11
            Property[Owner] = C2
        WEX::TestExecution::Examples::Class11::Method111
                Property[Priority] = 1
        WEX::TestExecution::Examples::Class11::Method112
                Property[BackwardsCompatibility] = Windows2000

    WEX::TestExecution::Examples::Class12
        WEX::TestExecution::Examples::Class12::Method121

以及:

F:\fsd1.binaries.x86chk\WexTest\C1\TestExecution>te Examples\CPP.SelectionCriteria2.Example.dll /listproperties
Test Authoring and Execution Framework v2.2 Build 6.1.7689.0 (release.091218-1251) for x86

F:\fsd1.binaries.x86chk\WexTest\C1\TestExecution\Examples\CPP.SelectionCriteria2.Example.dll
        Property[Owner] = WEX
    WEX::TestExecution::Examples::Class21
            Property[BackwardsCompatibility] = Windows XP
            Property[Owner] = C1
            Property[Priority] = 2
        WEX::TestExecution::Examples::Class21::Method211
                Property[Owner] = C2

    WEX::TestExecution::Examples::Class22
            Property[Owner] = U3
        WEX::TestExecution::Examples::Class22::Method221

在此需要注意的是,测试用例文件会连同其完整路径一起列出,而类名在本地测试用例文件中则以“<Namespace>::<ClassName>”的形式列出,在托管测试用例文件中则以“<Namespace>.<ClassName>”的形式列出。 同样,在本地 test_binaries 中,测试方法名称以“<Namespace>::<ClassName>::<TestMethodName>”的形式列出;在托管测试用例中,测试方法名称以“<Namespace>.<ClassName>.<TestMethodName>”的形式列出。

换句话说,任何名称或函数的完全限定名称都是保存在 te 中的内容。 这样做是为了能够唯一地区分任何方法。 例如,如果两个类有相同的方法名称,类限定有助于唯一选择感兴趣的方法。 为此,选择条件有助于只运行在给定的 test_binaries 中符合条件的测试。

在上述示例中,例如在 Examples\Cpp.SelectionCriteria1.Example.dll 中,可以通过以下任一选择条件选择“Method111”:

Te.exe Examples\CPP.SelectionCriteria1.Example.dll /select:"@Name='WEX::TestExecution::Examples::Class11::Method111'"
Te.exe Examples\CPP.SelectionCriteria1.Example.dll /select:"@Name='*Class11::Method111'"
Te.exe Examples\CPP.SelectionCriteria1.Example.dll /select:"@Name='*Method111'"

通过运行以下内容,可以选择运行所有标记为“优先级”小于 2 的测试:

Te.exe Examples\CPP.SelectionCriteria1.Example.dll Examples\CPP.SelectionCriteria2.Example.dll /select:"@Priority < 2"

这样将只运行 Examples\CPP.SelectionCriteria1.Example.dll - 我们示例中的“class11::method111”。

如果要运行 class11 下的所有测试,则可以使用限定的“Name”属性和通配符匹配来进行选择,如下所示:

Te.exe Examples\CPP.SelectionCriteria1.Example.dll Examples\CPP.SelectionCriteria2.Example.dll
                                                               /select:"@Name='*::class11::*'"

在使用选择条件时,需要注意一些有用的事项:

  • and”、“not”和“or”是保留字,并且不区分大小写
  • 元数据属性名称和值不区分大小写,例如,示例中的“C2”将与“c2”和“C2”匹配。 因此,如果有一个元数据为“property”的函数和另一个元数据为“Property”的函数,而选择条件是查找“PROPERTY”,那么这两个函数都会匹配。
  • 选择查询字符串中的字符串值应包含在单引号中。 在选择查询的字符串值中,“?”表示单个通配符,“*”表示 0 个或多个通配符。
  • 在命令提示符下使用引号时,要注意复制选择查询时的智能引号。 如果从 Outlook 电子邮件中复制选择查询,则可能会不小心带有智能引号,而 TAEF 可能无法对其进行解析。 请改为输入引号。

现在举几个简单的例子来说明复合选择标准及其执行方式。

Te.exe Examples\CPP.SelectionCriteria1.Example.dll Examples\CPP.SelectionCriteria2.Example.dll \
/select:"@Owner='C2' AND @Priority=2"

将运行:

  • Examples\CPP.SelectionCriteria2.Example.dll - class21::method211
Te.exe Examples\CPP.SelectionCriteria1.Example.dll Examples\CPP.SelectionCriteria2.Example.dll \
/select:"@Owner='C2' AND @Priority=3"

将运行:

  • Examples\CPP.SelectionCriteria1.Example.dll - class11::method112
Te.exe Examples\CPP.SelectionCriteria1.Example.dll Examples\CPP.SelectionCriteria2.Example.dll \
/select:"@Owner='U3' oR @Priority=1"

将运行:

  • Examples\CPP.SelectionCriteria1.Example.dll - class11::method111
  • Examples\CPP.SelectionCriteria2.Example.dll - class22::method221
Te.exe Examples\CPP.SelectionCriteria1.Example.dll Examples\CPP.SelectionCriteria2.Example.dll \
/select:"not(@BackwardsCompatibility=*)"

将运行所有未指定 BackwardsCompatibility 值的测试。 (查看以下各项。)

  • Examples\CPP.SelectionCriteria1.Example.dll - class11::method111, class12::method121
  • Examples\CPP.SelectionCriteria2.Example.dll - class22::method221
Te.exe Examples\CPP.SelectionCriteria1.Example.dll Examples\CPP.SelectionCriteria2.Example.dll \
/select:"@Owner='C*'"

将运行所有 Owner 值以“C”开头的测试(不区分大小写)。 因此,上一条命令将运行 Examples\CPP.SelectionCriteria1.Example.dll 中的所有测试,以及 Examples\CPP.SelectionCriteria2.Example.dll 中 class21 下的所有测试(即 method211)。

Te.exe Examples\CPP.SelectionCriteria1.Example.dll Examples\CPP.SelectionCriteria2.Example.dll \
/select:"not(@BackwardsCompatibility=*) OR (@Owner='C*' AND @BackwardsCompatibility='*XP*')"

将运行所有未指定 BackwardsCompatibility 或所有者名称以“C”开头且 BackwardsCompatibilty 值包含 XP 的测试。 注意括号“(”和“)”是如何用于指定优先顺序的。

在此示例中,这将有选择地运行:

  • Examples\CPP.SelectionCriteria1.Example.dll - class11::method111, class12::method121,
  • Examples\CPP.SelectionCriteria2.Example.dll - class21::method211, class22::method221
Te.exe Examples\CPP.SelectionCriteria1.Example.dll Examples\CPP.SelectionCriteria2.Example.dll /select:"@Owner='???'"

只会运行属性所有者值仅包含 3 个字符的测试。

在我们的示例中,这将匹配“C”并仅运行:

  • Examples\CPP.SelectionCriteria1.Example.dll - class12::method121
Te.exe Examples\CPP.SelectionCriteria1.Example.dll Examples\CPP.SelectionCriteria2.Example.dll /select:"@Priority>=1"

注意

这是一个很好的示例,说明如何使用“>=”、“<=”、“>”和“<”,其中 propertyvalue 是 floatvalue。

在本示例中,这样将运行除 Examples\CPP.SelectionCriteria2.Example.dll - class22::method221 以外的所有方法,其中没有指定优先级。 换言之,这样将运行:

  • Examples\CPP.SelectionCriteria1.Example.dll - class11::method111, class11::method112, class12::method121
  • Examples\CPP.SelectionCriteria2.Example.dll - class21::method211。

请注意,“/select”可与其他命令选项(如“/list”、“/listproperties”等)结合使用。

智能引号

如果将选择条件从 Outlook 或 Word 文档复制到命令提示符,则可能会在选择条件中遇到智能引号。 有关智能引号的详细信息,请参阅智能引号:供计算机使用的文本中隐藏的灾难

要避免使用智能引号并不容易,最好的办法是将选择条件中的 " 双引号和 ' 单引号复制到命令提示符后全部删除,然后重新输入查询的引号部分。

在 Outlook 中创建邮件时,有一个选项设置可将其关闭。 在 Outlook 帮助框中输入“智能引号”即可找到该内容。

基于名称的快速选择

TAEF 允许使用“/name”命令行参数在命令提示符下根据名称进行快速选择:

/name:<test name with wildcards>

等效于:

/select:@Name='<test name with wildcards>'

换言之,现在可以根据名称提供类似的选择查询:

Te.exe Examples\CPP.SelectionCriteria1.Example.dll Examples\CPP.SelectionCriteria2.Example.dll \
/select:"@Name='*::class11::*'"

通过像这样使用 /name 可以变得更快:

Te.exe Examples\CPP.SelectionCriteria1.Example.dll Examples\CPP.SelectionCriteria2.Example.dll /name=*::class11::*

请注意,如果在命令提示符下同时提供 /name/select,则 /name 将被忽略而 /select 优先。