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 parameter name>”) 方法來查詢是否已提供執行階段參數,且可供測試使用。 如果傳回 true,您可以使用 RuntimeParameters.GetValue(“<runtime parameter name>”) 來擷取它。 請注意,如果執行階段參數無法使用,則會擲回 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>