Run TestNG project From Jenkins with BAT file

Jenkins

Run TestNG project From Jenkins with BAT file only

1. First Create a sample Java Project
2. Create “lib” folder and put testng jar on it.
3. Add TestNG library from build path

4. The Sample.java may contain this sample code for testing purpose –

package org.demo;

import org.testng.annotations.Test;

public class Sample {
    @Test
    public void doTest1(){
        System.out.println("Hello from Test 1");
    }
    @Test
    public void doTest2(){
        System.out.println("Hello from Test 2");
    }
    @Test
    public void doTest3(){
        System.out.println("Hello from Test 3");
    }
    @Test
    public void doTest4(){
        System.out.println("Hello from Test 4");
    }
    @Test
    public void doTest5(){
        System.out.println("Hello from Test 5");
    }    
}

5. And the sample execution XML file (here sampleTest.xml ) should contain –

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite">
  <test verbose="10" name="Default test">
    <classes>
      <class name="org.demo.Sample">
        <methods>
          <include name="doTest"/>
        </methods>
      </class> <!-- org.demo.Sample -->
    </classes>
  </test> <!-- Default test -->
</suite> <!-- Default suite -->

6. Now write a bat file to execute it through command prompt –

set myProjectpath=C:\my\workspace\location\Basic
cd %myProjectpath%
set classpath=%myProjectpath%\bin;%myProjectpath%\lib\*
java org.testng.TestNG %myProjectpath%\sampleTest.xml

7. To see the output we need to add “pause” at the end of the bat script. Double click on bat file and you will get the output –

8. Now start Jenkins server.( If you don’t know how to setup or start jenkins server then please follow my previous blog)
9. Click on “New Item” and create a freestyle project

10. Now it will open configure page, leave that page for now click on build now and navigate to Workspace option

11. Now goto “.jenkins\workspace” folder and you will see a new folder has been created (with project name)
12. Put all your project here
13. And now your workspace will looks like –

14. Now go to the configure tab and click on build menu

15. Select batch command and provide the bat file name and save it

16. Now click on “Build Now” button from left side
17. Goto console and you will see the output like –

That’s it . We are able to execute testng from bat file in Jenkins.

Thank you 🙂

Related posts:

Leave a Reply

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