[TestInitialize()]
public void MyTestInitialize()
{
XmlTextWriter writer = new XmlTextWriter("DataFile.xml", Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteProcessingInstruction("xml", "version='1.0' encoding='utf-8'");
writer.WriteStartElement("TestCases");
DirectoryInfo dir = new DirectoryInfo("Metadata");
foreach (FileInfo file in dir.GetFiles())
{
writer.WriteElementString("TestCase", file.Name);
}
writer.Close();
}
[TestMethod()]
[DeploymentItem("FunctionalTestsProject\\TestData")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
"|DataDirectory|\\DataFile.xml",
"TestCase", DataAccessMethod.Sequential)]
public void MyTestMethod()
{
I want to use DataFile created by MyTestInitialize method.
}
但是这里MyTestInitialize方法没有执行,因为MyTestMethod试图与DataFile.xml建立数据连接并且失败了。我想在我的数据驱动测试中使用DataFile.xml文件作为数据文件,它应该在运行时创建。如果有任何其他解决方法,请帮助我。
答案 0 :(得分:1)
将[TestInitialize()]
更改为[AssemblyInitialize()]
或[ClassInitialize()]
时是否有效?