Pritam Karmakar
Pritam Karmakar

Reputation: 2801

How to create DataSource file outside main test method

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

Upvotes: 0

Views: 1571

Answers (1)

Marc
Marc

Reputation: 1898

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

Upvotes: 1

Related Questions