从Java Main方法运行TestNG测试
TestNG tests are executed automatically when we build a maven project. We can also run TestNG tests using Eclipse plugin. What if we don’t have the TestNG plugin installed for our IDE and we want to r
TestNG tests are executed automatically when we build a maven project. We can also run TestNG tests using Eclipse plugin. What if we don’t have the TestNG plugin installed for our IDE and we want to run some specific tests without doing a complete build. In this case, we can run TestNG test classes from a java main method too.
当我们构建一个Maven项目时,TestNG测试会自动执行。 我们还可以使用Eclipse插件运行TestNG测试。 如果我们没有为IDE安装TestNG插件,而又想在不进行完整构建的情况下运行某些特定的测试,该怎么办? 在这种情况下,我们也可以从java main方法运行TestNG测试类。
Java主要方法中的TestNG测试 (TestNG Test from Java Main Method)
Let’s create a simple TestNG test class and a TestNG listener for our example.
让我们为示例创建一个简单的TestNG测试类和一个TestNG侦听器。
package com.journaldev.main;
import org.testng.annotations.Test;
public class Test5 {
@Test
public void test() {
System.out.println("Running test method");
}
}
package com.journaldev.main;
import org.testng.ISuite;
import org.testng.ISuiteListener;
public class Test5SuiteListener implements ISuiteListener {
@Override
public void onStart(ISuite suite) {
System.out.println("TestNG suite default output directory = "+suite.getOutputDirectory());
}
@Override
public void onFinish(ISuite suite) {
System.out.println("TestNG invoked methods = " +suite.getAllInvokedMethods());
}
}
Now we want to run Test5
class tests and also want to add Test5SuiteListener
listener to our TestNG test suite. We can easily do this using org.testng.TestNG
class.
现在,我们要运行Test5
类测试,还想将Test5SuiteListener
侦听器添加到我们的TestNG测试套件中。 我们可以使用org.testng.TestNG
类轻松地做到这org.testng.TestNG
。
package com.journaldev.main;
import org.testng.TestNG;
public class TestNGMainClass {
public static void main(String[] args) {
TestNG testSuite = new TestNG();
testSuite.setTestClasses(new Class[] { Test5.class });
testSuite.addListener(new Test5SuiteListener());
testSuite.setDefaultSuiteName("My Test Suite");
testSuite.setDefaultTestName("My Test");
testSuite.setOutputDirectory("/Users/pankaj/temp/testng-output");
testSuite.run();
}
}
Notice that I am also changing TestNG reporting output directory, setting the test suite and test name.
请注意,我还更改了TestNG报告输出目录,设置了测试套件和测试名称。
Just run above class as java application and it should produce following output in the console.
只需在类上作为Java应用程序运行,它就会在控制台中产生以下输出。
TestNG suite default output directory = /Users/pankaj/temp/testng-output/My Test Suite
Running test method
TestNG invoked methods = [Test5.test()[pri:0, instance:com.journaldev.main.Test5@314c508a] 827084938]
===============================================
My Test Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
You should also check for the HTML report generated by above program, it will be something like below image.
您还应该检查上面程序生成HTML报告,如下图所示。
That’s all for running TestNG tests from java main method.
这就是从java main方法运行TestNG测试的全部。
翻译自: https://www.journaldev.com/21261/running-testng-tests-from-java-main-method
更多推荐
所有评论(0)