TestNG的启动有两种方式:
1、命令行模式,在IDEA中也是通过该方式执行的
java org.testng.TestNg ***.xml
2、new TestNG() 的实例,通过run()方法执行,使用该方式执行需要注意并不会执行configure方法,所以要求在run之前设置好参数,有些是私有参数就需要通过反射等方式进行设置,此方式扩展性更高,需要对TestNG做扩展或二次开发时,可以有效控制TestNG
TestNG的启动
/**
* The TestNG entry point for command line execution.
*
* @param argv the TestNG command line parameters.
*/
public static void main(String[] argv) {
TestNG testng = privateMain(argv, null);
System.exit(testng.getStatus());
}
命令行启动会找到org.testng.TestNg类中的main方法启动。
在main方法中调用了privateMain方法。
/**
* <B>Note</B>: 此方法不是公共 API 的一部分,仅供内部使用.
*/
public static TestNG privateMain(String[] argv, ITestListener listener){
....
//
// Parse the arguments
//
try {
CommandLineArgs cla = new CommandLineArgs();
m_jCommander = new JCommander(cla);
m_jCommander.parse(argv);
validateCommandLineParameters(cla);
result.configure(cla);
} catch (ParameterException ex) {
exitWithError(ex.getMessage());
}
....
}
privateMain方法中主要做的一个事情就是使用了jcommander对命令行的解析校验和配置。
在这里从CommandLineArgs类中罗列一下Testng支持的命令行参数:
/**
testng 启动suite的文件,可填写多个
*/
@Parameter(description = "The XML suite