how to call testng.xml from java main method?

前端 未结 5 1495
深忆病人
深忆病人 2020-12-10 15:40

I have testng.xml file created. Is there any way to run this file from java main method?

Something like -

Class test {
  public static void main ( St         


        
相关标签:
5条回答
  • 2020-12-10 16:19

    This work for me. More details here.

    // Create object of TestNG Class
    TestNG runner=new TestNG();
    
    // Create a list of String 
    List<String> suitefiles=new ArrayList<String>();
    
    // Add xml file which you have to execute
    suitefiles.add("C:\\Automation Test\\Git\\vne_automation\\testng.xml");
    
    // now set xml file for execution
    runner.setTestSuites(suitefiles);
    
    // finally execute the runner using run method
    runner.run();
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-10 16:20

    You can run testng directly from commandline, probably make a bat file on top of it or use jenkins to trigger it. Refer here

    or

    If you want it in main, then you can try

    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    List<String> suites = Lists.newArrayList();
    suites.add("c:/tests/testng1.xml");//path to xml..
    suites.add("c:/tests/testng2.xml");
    testng.setTestSuites(suites);
    testng.run();
    
    0 讨论(0)
  • 2020-12-10 16:29

    All the answers above works like a charm but there is one limitation that it would run unless and untill you remove the scope tag in pom.xml

    In pom.xml there would be maven dependancy for testNg and in there there would be one scope tag which limits our jar to only upto test

    Just remove that one

    0 讨论(0)
  • 2020-12-10 16:32

    Please try below code and make sure you have testNG jar added in your jar manifest file.

    public static void main(String[] args)
    {
        org.testng.TestNG.main(args);
    }
    

    now you can pass all the parameters to your jar which are same for testNG jar file.

    e.g.

    java -jar yourjar.jar testng.xml

    or

    java -jar yourjar.jar -testclass org.test.xyz.java

    0 讨论(0)
  • 2020-12-10 16:35

    I didn't get any error and XML path is correct. But It just end execution with failed test cases. It does not open firefox browser. I am using testng annotation (i.e. @Before Suite,@Test, @group etc ) inside classes whereas it successfully execute using Testng Suite in eclipse.

    <!--  suite name="Example"  parallel="methods" thread-count="2" parallel="false" preserve-order="true"> -->
    <suite name="Example Donate System" verbose='1'>
    
        <test name="Agency"   >
        <packages>
          <package name="com.Donatesystem.AgencyController" />
       </packages>
     </test>
    </suite>
    
    0 讨论(0)
提交回复
热议问题