koushick
koushick

Reputation: 497

Issue in Generating TestNG.xml File

i try to Generate TestNG.xml through java code but it is working when i run it as java application, but it is not generating the XML File Seperately.May i know Why, Code Works Fine But can't able to Generate the Xml File Seperatly .

My Code :

package Testcases;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class GenerateTestng
{
    @SuppressWarnings("deprecation")
    public void runTestNGTest() {

        //Create an instance on TestNG
         TestNG myTestNG = new TestNG();

        //Create an instance of XML Suite and assign a name for it.
         XmlSuite mySuite = new XmlSuite();
         mySuite.setName("MySuite");

        //Create an instance of XmlTest and assign a name for it.
         XmlTest myTest = new XmlTest(mySuite);
         myTest.setName("Wepaythemaxx");


        //Create a list which can contain the classes that you want to run.
         List<XmlClass> myClasses = new ArrayList<XmlClass> ();
         myClasses.add(new XmlClass("Testcases.FinalTest"));

        //Assign that to the XmlTest Object created earlier.
         myTest.setXmlClasses(myClasses);

        //Create a list of XmlTests and add the Xmltest you created earlier to it.
         List<XmlTest> myTests = new ArrayList<XmlTest>();
         myTests.add(myTest);

        //add the list of tests to your Suite.
         mySuite.setTests(myTests);

        //Add the suite to the list of suites.
         List<XmlSuite> mySuites = new ArrayList<XmlSuite>();
         mySuites.add(mySuite);

        //Set the list of Suites to the testNG object you created earlier.
         myTestNG.setXmlSuites(mySuites);

         TestListenerAdapter tla = new TestListenerAdapter();
         myTestNG.addListener(tla);

        //invoke run() - this will run your class.
         myTestNG.run();
        }
    public static void main(String[] args)
    {
        GenerateTestng dt = new GenerateTestng();
         dt.runTestNGTest();
    }

}

i have attached my code above, please verify, Where i done mistake i can't figure it out.

Upvotes: 1

Views: 928

Answers (2)

Neagu V
Neagu V

Reputation: 480

I created this method that will save the file:

public void createXmlFile(String saveFilePath, XmlSuite suiteName) {
    File file = new File(saveFilePath);
    FileWriter writer = null;
    try {
        writer = new FileWriter(file);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        writer.write(suiteName.toXml());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(suiteName.toXml());
    try {
        writer.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Where

  • saveFilePath = the path where the file will be saved e.g. '.testNGxml.xml'
  • suiteName = your suite name, in your case mySuite

Upvotes: 3

user2304843
user2304843

Reputation:

Eclipse has a TestNG extension that creates the xml on its own. You can then edit the xml to change the order the tests are run on(so that you can create mock data, edit said data and delete it without getting an error for example).

I am sure most IDEs have similar approaches.

Any reason you are not using an IDE?.

Upvotes: 0

Related Questions