How to Run TestNG xml Programmatically

We normally run TestNG projects in eclipse by executing the testng xml file. If it’s a maven project, you can run it using mvn test command. In this quick post, we will see how a testng project can be executed programmatically.

Why we need to run testng.xml programmatically?

  • If the project has to be packaged and deployed on some server
  • If the test execution by testng is just a component of our test execution framework where execution parameters are determined dynamically.

Run testng xml file Programmatically

The main reason we find it confusing to run testng programmatically is that we execute the testng.xml file to trigger the test. In reality, we are executing TestNG’s main class and passing our testng.xml file as a parameter.

This can be changed as shown below.

We create a Class which acts as the starting point for execution.

In the main method of our class, we initialize a TestNG object. We add the testng.xml file to the test suite and add it to the testng object and trigger the execution.

SEE ALSO:   Complex Operations with Selenium WebDriver Actions API

You may also like...

4 Responses

  1. Mukesh says:

    how to automate Mobile Testing any ready project or example how to start this. For UI testing or Functional testing

  2. Mukesh says:

    I am trying to automate comparing two website one is old website and other one is updated website. So, how to automate these using selenium? how to check UI or functional, content checking comparing the other site.Need guidence

    • Amal Bose says:

      In the case of comparing two sites, for each testcase, you can run the scenario for old site first as a baseline and then run the same again for the new site and then compare the values.
      If all your testcases are in this manner, you can modify your framework in such a way that it runs the scenario on these two sites and do the comparison.

  3. Prakash says:

    Could anyone please help me how to run this class file from command line?

Leave a Reply

Your email address will not be published. Required fields are marked *