运行时参数

TAEF 提供用于将运行时参数传递给其执行的测试的功能。

用法

若要将参数传递给测试,请按以下形式将参数作为命令行参数提供给 te.exe:

Te.exe /p:ParameterName1=ParameterValue1  /p:ParameterName2=ParameterValue2

如果参数值包含空格,请围绕参数名称和参数值放置引号:

Te.exe /p:"ParameterName3=The quick brown fox jumps over the lazy dog"

内置参数

TAEF 对以下运行时参数具有内置支持:

  • TestDeploymentDir:测试二进制目录。
  • TestName:当前正在运行的测试的名称。
  • FullTestName:测试的完整名称,包括变体限定符。 这是 TAEF 记录 StartGroup 和 EndGroup 的名称。
  • TestResult:此清理函数范围内执行的测试的聚合最差情况结果。 从最佳到最差的顺序:通过、未运行、已跳过、被阻止、失败。 例如,如果类中至少有一个测试被阻止,但没有测试失败,则结果将“被阻止”(仅在 Cleanup 函数中可用)。

从测试访问运行时参数

本机测试

运行时参数在安装程序、清理和测试方法中可用。 使用 RuntimeParameters::TryGetValue API 获取它们:

String value;
VERIFY_SUCCEEDED(RuntimeParameters::TryGetValue(L"ParameterName3", value));

注意:若要从测试请求运行时参数,需要链接到 Te.Common.lib 库。

托管测试

运行时参数在设置和测试方法中可用。 若要获取它们,请使用类的 TestContext 属性。

示例(类或程序集配置):

[ClassInitialize]

public static void ClassSetup(TestContext context)
{
    String parameterName3  = context.Properties["ParameterName3"];

}

同样,在测试中:

[TestMethod]

public void VerifyRuntimeParametersTest()
{
    String parameterName3  = m_testContext.Properties["ParameterName3"].ToString());
}

// Note, that to work with runtime parameters, as well as with your tests,  you need to add
// definition of test context property to your class

private TestContext m_testContext;

public TestContext TestContext
{
    get { return m_testContext; }
    set { m_testContext = value; }
}

脚本测试

运行时参数在设置、清理和测试方法中可用。 若要检索运行时参数,请从 Te.Common 定义和实例化 RuntimeParameters 对象:

<object id="RuntimeParameters" progid="Te.Common.RuntimeParameters" />

实例化 RuntimeParameters 对象后,可以使用 RuntimeParameters.Contains(“<runtime 参数名称>”)方法来查询是否提供了运行时参数并可供测试使用。 如果返回 true,则可以使用 RuntimeParameters.GetValue(“<runtime 参数名称>”)来检索它。 请注意,如果运行时参数不可用,RuntimeParameters.GetValue(...) 将引发。 以下示例来自 VBScript 示例:

<script language="VBScript">
    <![CDATA[
        Function TestOne()
            dim param
            param = "No runtime param"
            If RuntimeParameters.Contains("param") Then
                    param = RuntimeParameters.GetValue("param")
            End If
            Log.Comment("Param is " + param)

            dim testDir
            If RuntimeParameters.Contains("testDir") Then
                testDir = RuntimeParameters.GetValue("TestDir")
            End If
            Log.Comment("The test harness is running in " + testDir)
        End Function
    ]] >
</script>