views:

265

answers:

2
    [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.
     }

But here MyTestInitialize method is not executing, as MyTestMethod is trying to make an data connection with DataFile.xml and it get failed. I want to use DataFile.xml file as a data file in my Data Driven Testing and it should be created on run time. Please help me if there is any other workaround for this.

+1  A: 

Does it work when you change the [TestInitialize()] to [AssemblyInitialize()] or [ClassInitialize()] ?

Marc
A: 

Thanks a lot. We need to use ClassInitialize instead of TestInitialize. It works for me.

Pritam Karmakar