我有一个测试方法,它调用两个子测试方法。这两个子方法都是从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
}如果全部运行,将有2个方法通过,1个失败。如果我单独运行,SaveData_Json_LocalStorage不会传递,总是将TestContext.DataRow作为null。打电话给里面的多个方法可以吗。编写链接测试用例的最佳方法是什么。
发布于 2015-05-18 20:41:56
只有当一个人必须有不可重建的数据时,才应该进行链接.否则,让每个测试成为一个不同的测试。
从XML文件驱动的数据。
考虑将只读Xml放在一个属性中,该属性在ClassInitialization方法的t测试之前运行一次。然后测试各个操作,然后是"Main“操作,每个操作作为一个单独的可测试单元。
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 ... );
}否则,可以根据正在执行的测试或来自特定调用的数据来模拟数据,shim怎么样?请看我的文章Shim在一个棘手的单元测试环境中保存了一天。
https://stackoverflow.com/questions/30312154
复制相似问题