1 回答

TA貢獻1895條經驗 獲得超3個贊
我想到了兩種解決方案。
運行設置
第一個是與 Visual Studio 和 MSTest 的RunSettings文件更加集成的解決方案。在以 XML 結構定義的 RunSettings 文件中,您預定義了<TestRunParameters>
可以在由 MSTest 裝飾器(例如 、 等)裝飾的方法或類中訪問和設置的[AssemblyInitialize]
文件[TestClass]
。在這些修飾的方法或類中,您當然可以訪問一個TestContext
對象,并且在該TestContext
對象中,您可以<TestRunParameters>
使用TestContext.Properties
.
例如,假設您有一個 RunSettings 文件,
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
? ? <TestRunParameters>
? ? ? ? <Parameter name="Name" value="Dan" />
? ? </TestRunParameters>
</RunSettings>
您可以在測試類中執行此操作:
[TestClass]
public class Configuration?
{
? ? [ClassInitialize]
? ? public static void ClassInitialize(TestContext testContext)?
? ? {
? ? ? ? Console.Write(testContext.Properties["Name"]); // Outputs "Dan"
? ? ? ? // The TestContext object will be modified and the updated value?
? ? ? ? // will be ready the next time it's retrieved
? ? ? ? testContext.Properties["Name"] = "John";?
? ? }
}
靜態類
或者,您可以有一個靜態類,其唯一目的是初始化和存儲字符串。
- 1 回答
- 0 關注
- 98 瀏覽
添加回答
舉報