roc*_*cky 5 c# unit-testing testcontext
我有一个测试方法,它调用2个子测试方法.子方法都是从XML文件驱动的数据.如果我运行每个子方法,它们运行良好且成功.但是,当我运行主测试方法(两个子方法的调用者)时,它发现TestContext.DataConnection和TestContext.DataRow为null.
private TestContext testContext;
public TestContext TestContext
{
get { return testContext; }
set { testContext = value; }
}
[TestMethod]
public void SaveEmpty_Json_LocalStorage()
{
// Testing JSON Type format export and save
SetWindowsUsers();
// Add Network Information
SetWifiInformation();
// More logic and assertions here.
// More logic and assertions here.
// More logic and assertions here.
}
[TestMethod]
[DeploymentItem("input.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
"input.xml",
"User",
DataAccessMethod.Sequential)]
public void SetWindowsUsers()
{
Console.WriteLine(TestContext.DataRow["UserName"].ToString())
// MORE LOGIC and Asserts
}
[TestMethod]
[DeploymentItem("input.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
"input.xml",
"WifiList",
DataAccessMethod.Sequential)]
public void SetWifiInformation()
{
Console.WriteLine(TestContext.DataRow["SSID"].ToString())
// MORE LOGIC and Asserts
}
Run Code Online (Sandbox Code Playgroud)
如果我全部运行,则2个方法通过,1个失败.如果我单独运行,SaveData_Json_LocalStorage不通过,总是将TestContext.DataRow作为null.可以在里面调用多个方法吗?编写链式测试用例的最佳方法是什么.
仅当必须具有不可重新创建的数据时才应进行链接。否则,使每个测试成为不同的测试。
由 XML 文件驱动的数据。
考虑将只读Xml 放入一个属性中,该属性在方法中进行测试之前运行一次ClassInitialization。然后测试各个操作,然后是“主”操作,每个操作作为一个单独的可测试单元。
public static XDocument Xml { get; set; }
[DeploymentItem("input.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
"input.xml",
"User",
DataAccessMethod.Sequential)]
[ClassInitialize()]
public static void ClassInit(TestContext context)
{ // This is done only once and used by other tests.
Xml = ...
Assert.IsTrue(Xml.Node ... );
}
Run Code Online (Sandbox Code Playgroud)
否则,考虑根据正在执行的测试来模拟数据,或者如果它来自特定的调用,那么如何shim?请参阅我的文章Shim Saves The Day in A Tricky Unit Test Situation。
| 归档时间: |
|
| 查看次数: |
886 次 |
| 最近记录: |