Run TestNG Suite In Java Main

Here is a simple demo about how to run test suite with java program:

package com.test.main;

import org.testng.TestListenerAdapter;
import org.testng.TestNG;

import java.util.ArrayList;
import java.util.List;

public class TestRunner {
    public static void main(String[] args) {
        System.out.println("Running tests!");

        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG runner = new TestNG();
        // Create a list of String
        List<String> suiteFiles=new ArrayList<>();
        // Add xml file which you have to execute
        //suiteFiles.add(args[0]);
        suiteFiles.add("C:\\test\\testsuite.xml");
        // now set xml file for execution
        runner.setTestSuites(suiteFiles);
        runner.addListener(tla);
        runner.run();
        System.exit(0);
    }
}

Here we can go ahead to run this main method.

Also, if we want to run a TestNG test with a jar, then we can export the program as a jar with Eclipse. Here is the refer to export runnable jar with Eclipse:
http://blog.csdn.net/xiaoguaihai/article/details/42462761

If we want the test suite files as a param in java -jar command, then can modify the program as below:
suiteFiles.add(args[0]);

Run java command in CMD:
java -jar TestRunner.jar C:\\test\\testsuite.xml

Note: You'd better export the jar file in the same folder with source code to avoid there may be some depends for the test running cannnot be found out.

猜你喜欢

转载自pengyao0305.iteye.com/blog/2388737
今日推荐