如何在主要测试方法之外创建DataSource文件

时间:2009-11-11 08:46:16

标签: c# datasource data-driven-tests

    [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文件作为数据文件,它应该在运行时创建。如果有任何其他解决方法,请帮助我。

1 个答案:

答案 0 :(得分:1)

[TestInitialize()]更改为[AssemblyInitialize()][ClassInitialize()]时是否有效?