TestNG的用例除了直接运行之外,还可以使用代码来调用,这样做的好处在于我们可以将其嵌入其他代码中,来执行这些TestNG用例,方法如下:
范例:
定义了两个测试用例类为DependTest1.java
和FactoryTest.java
:
再做一个main函数来调用,代码如下:
package com.demo.test.testng;
import org.testng.TestNG;
public class Entry {
public static void main(String[] args) {
TestNG testNG = new TestNG();
Class[] classes = {DependTest1.class, FactoryTest.class};
testNG.setTestClasses(classes);
testNG.run();
}
}
执行结果如下:
[TestNG] Running:
Command line suite
dependTest1
dependTest2
dependTest4
login, host:10.10.10.1;port8080
login, host:10.10.10.2;port8080
logout, host:10.10.10.1;port8080
logout, host:10.10.10.2;port8080
===============================================
Command line suite
Total tests run: 7, Failures: 0, Skips: 0
===============================================
方法如下:
package com.demo.test.testng;
import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;
public class Entry {
public static void main(String[] args) {
TestNG testNG = new TestNG();
List<String> suites = new ArrayList<String>();
suites.add("D:\\software\\workspace\\testng\\src\\main\\java\\com\\demo\\test\\testCase\\depend1.xml");//此处为xml的绝对路径
testNG.setTestSuites(suites);
testNG.run();
}
运行结果如下:
...
... TestNG 6.10 by Cédric Beust ([email protected])
...
[TestRunner] Running the tests in 'Test' with parallel mode:none
[RunInfo] Adding method selector: org.testng.internal.XmlMethodSelector@17d10166 priority: 10
[TestClass] Creating TestClass for [ClassImpl class=com.demo.test.testng.FactoryTest]
[TestNG] Running:
D:\software\workspace\testng\src\main\java\com\demo\test\testCase\depend1.xml
[SuiteRunner] Created 1 TestRunners
[TestRunner] Running test Test on 1 classes, included groups:[] excluded groups:[]
===== Test class
com.demo.test.testng.FactoryTest
@Test FactoryTest.logout()[pri:0, instance:com.demo.test.testng.FactoryTest@6a6824be]
@Test FactoryTest.logout()[pri:0, instance:com.demo.test.testng.FactoryTest@5c8da962]
@Test FactoryTest.login()[pri:0, instance:com.demo.test.testng.FactoryTest@6a6824be]
@Test FactoryTest.login()[pri:0, instance:com.demo.test.testng.FactoryTest@5c8da962]
======
[Invoker 317983781] Invoking com.demo.test.testng.FactoryTest.login
login, host:10.10.10.2;port8080
[Invoker 317983781] Invoking com.demo.test.testng.FactoryTest.logout
logout, host:10.10.10.2;port8080
[Invoker 317983781] Invoking com.demo.test.testng.FactoryTest.login
login, host:10.10.10.1;port8080
[Invoker 317983781] Invoking com.demo.test.testng.FactoryTest.logout
logout, host:10.10.10.1;port8080
===== Invoked methods
FactoryTest.login()[pri:0, instance:com.demo.test.testng.FactoryTest@5c8da962] 1552787810
FactoryTest.logout()[pri:0, instance:com.demo.test.testng.FactoryTest@5c8da962] 1552787810
FactoryTest.login()[pri:0, instance:com.demo.test.testng.FactoryTest@6a6824be] 1785210046
FactoryTest.logout()[pri:0, instance:com.demo.test.testng.FactoryTest@6a6824be] 1785210046
=====
[[Utils]] Attempting to create D:\software\workspace\testng\test-output\SuiteDepend\Test.xml
[[Utils]] Directory D:\software\workspace\testng\test-output\SuiteDepend exists: true
Creating D:\software\workspace\testng\test-output\SuiteDepend\Test.xml
PASSED: login
PASSED: logout
PASSED: login
PASSED: logout
===============================================
Test
Tests run: 4, Failures: 0, Skips: 0
===============================================
这个例子的log要比上面那个详细很多,是因为我在xml中定义了日志详细等级;
除了直接书写xml外,还可以直接在代码中生成,方法如下:
譬如有个xml是这么写的:
<suite name="TmpSuite" >
<test name="TmpTest" >
<classes>
<class name="test.failures.Child" />
<classes>
test>
suite>
其对应的代码如下:
XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");
XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("test.failures.Child"));
test.setXmlClasses(classes) ;
备注:其他譬如设置verbose等级等都有对应的方法可以使用
这个XmlSuite可以直接加入TestNG中运行,如下为使用该XmlSuite运行的代码:
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
那么如果想要只运行其中的几个group如何设置呢,如下:
package com.demo.test.testng;
import org.testng.TestNG;
public class Entry {
public static void main(String[] args) {
TestNG testNG = new TestNG();
testNG.setVerbose(3);
testNG.setTestClasses(new Class[]{DependTest1.class, FactoryTest.class});
testNG.setGroups("dependGroup1");//此处可以设置多个group名称,以逗号分隔
testNG.run();
}
如上面的代码,只设置group名称是无法运行的,必须先加载用例,可以是class
也可以是xml
,另外根据源码可知group名称是可以添加多个的,以,
分隔即可;
如果只是某些group不想运行,则可以用方法testNG.setExcludedGroups(groups);
同理这个groups
同样是可以包含一个或者多个group名称,以,
分隔;
如果想要修改报告输出目录,则可以用如下代码来设置:
package com.demo.test.testng;
import org.testng.TestNG;
public class Entry {
public static void main(String[] args) {
TestNG testNG = new TestNG();
testNG.setVerbose(3);
testNG.setTestClasses(new Class[]{DependTest1.class, FactoryTest.class});
testNG.setGroups("dependGroup1");//此处可以设置多个group名称,以逗号分隔
testNG.setOutputDirectory("D:\\test");//设置输出目录
testNG.run();
}
这样TestNG就会把报告输出到此文件夹下;
如果我们把带有TestNG用例的工程打成了jar包,然后要在其他工程里调用这个jar包中的用例,可以使用如下方法:
TestNG testNG = new TestNG();
testNG.setTestJar(jarPath);//jar包的路径
testNG.setXmlPathInJar(xmlPathInJar);//jar包中xml文件的路径
除了上面所说的那些之外,TestNG
对象还有其他很多方法,譬如设置verbose
,设置并发数等;