运行时参数

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 函数范围内执行的测试的聚合最差情况结果。 按从最佳到最差的顺序:已通过、NotRun、跳过、阻止、失败。 例如,如果类中至少有一个测试被阻止,但没有测试失败,则结果将为“已阻止” (仅在清理函数) 中可用。

从测试访问运行时参数

本机测试

运行时参数在安装程序、清理和测试方法中可用。 使用 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 (“<运行时参数名称>”) 方法来查询运行时参数是否已提供并且可供测试使用。 如果返回 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>