Need to run testng from java main class.

7,137 views
Skip to first unread message

Abhishek Bisht

unread,
Sep 20, 2013, 5:59:52 AM9/20/13
to testng...@googlegroups.com
Hi all ,

I need to run my testng.xml file from main class , but dont have any idea about it as in testng we don't have any main method .
It would be really helpful if some one can guide me or give an idea on how to call xml file and allow it run as defined in testng xml file.

Regards
Abhishek Bisht

Tomek Kaczanowski

unread,
Sep 20, 2013, 6:05:23 AM9/20/13
to testng-users
Hi,

please have a look at
http://testng.org/doc/documentation-main.html#running-testng-programmatically

2013/9/20 Abhishek Bisht <abhishek....@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "testng-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to testng-users...@googlegroups.com.
> To post to this group, send email to testng...@googlegroups.com.
> Visit this group at http://groups.google.com/group/testng-users.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Regards / Pozdrawiam
Tomek Kaczanowski
http://practicalunittesting.com

Abhishek Bisht

unread,
Sep 23, 2013, 5:11:59 AM9/23/13
to testng...@googlegroups.com
Hi Tomek,

I have tried with given way

    TestListenerAdapter tla = new TestListenerAdapter();
            TestNG testng = new TestNG();
             Class[] classes = new Class[]{
                     mypackage.myclass1.class,
                     mypackage.myclass2.class};
             testng.setTestClasses(classes);
             testng.addListener(tla);
             testng.run();

But when i m trying to run this main method @beforesuite is working fine but it only loads class method unable to execute any of them. The error i am getting is
Command line suite
Total tests run: 9, Failures: 7, Skips: 2
Configuration Failures: 1, Skips: 2
of these i am unable to figure out the configuration failure reason , if possible can you please provide some help on this.

Thanks
Reagards
Abhishek

Nikesh Jauhari

unread,
Sep 23, 2013, 12:25:08 PM9/23/13
to testng...@googlegroups.com
                List<String> suitesList = new ArrayList<String>();
TestListener listener = new TestListener();
TestNG testng = new TestNG();
testng.setOutputDirectory("outputfoldername");
suitesList.add("testng.xml");
testng.setTestSuites(suitesList);
testng.addListener(listener);
testng.run(); 

Tomek Kaczanowski

unread,
Sep 23, 2013, 2:24:29 PM9/23/13
to testng-users
Hi,

> of these i am unable to figure out the configuration failure reason
But that is what you need to figure out! Browse the files created by
TestNG when running - you should find some hints there. Or debug and
see in runtime what is going wrong in your configuration methods.

Cheers!

Abhishek Bisht

unread,
Sep 24, 2013, 1:13:34 AM9/24/13
to testng...@googlegroups.com
Thanks Nikesh its working fine now with this way

@Tomek still not able to figure out why it was giving error.

But thanks to you guys for giving valuable suggestions.

wariss Sheikh

unread,
Dec 30, 2014, 7:43:38 AM12/30/14
to testng...@googlegroups.com
I have used above proposed solutions but I am unable to execute testsuite.
When I m trying to run this main method @beforesuite is not working It only loads class method but unable to execute any method The error i am getting is 
Command line suite

I am unable to figure out the configuration failure reason , if possible can you please provide some help on this.

Scenario 1 :

List<String> suites = Lists.newArrayList();
        
        TestListenerAdapter tla = new TestListenerAdapter();
            TestNG testng = new TestNG();
            Class[] classes = new Class[]{
                    com.Donatesystem.AgencyController.ManageDonateAgency.class};
             testng.setTestClasses(classes);
            testng.addListener(tla);
             testng.run();
       
             
        Scenario 2 :
        List<String> suites = Lists.newArrayList();
        
        TestListenerAdapter tla = new TestListenerAdapter();
            TestNG testng = new TestNG();
suites.add("testng.xml");
testng.setTestSuites(suites);
testng.addListener(tla);
             testng.run();


testng.xml


   <test name="Login">
        <classes>
            <class name="com.Donatesystem.AgencyController.ManageDonatesAgency"/> 
                  </classes> 
  </test>
 
    <suite name="Example Donate Sysetm" verbose='1'>

     <test name="Agency" >
    <packages>
      <package name="com.Donatesystem.AgencyController" />
   </packages>
 </test>
</suite>   

Reply all
Reply to author
Forward
0 new messages