天天看點

【TestNG】使用代碼方式調用TestNG用例執行

TestNG的用例除了直接運作之外,還可以使用代碼來調用,這樣做的好處在于我們可以将其嵌入其他代碼中,來執行這些TestNG用例,方法如下:

1、直接調用用例類

範例:

定義了兩個測試用例類為

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
===============================================
           

2、調用用例的xml檔案

方法如下:

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: [email protected] 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:[email protected]]
    @Test FactoryTest.logout()[pri:0, instance:[email protected]]
    @Test FactoryTest.login()[pri:0, instance:[email protected]]
    @Test FactoryTest.login()[pri:0, instance:[email protected]]
======
[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:[email protected]] 1552787810
    FactoryTest.logout()[pri:0, instance:[email protected]] 1552787810
    FactoryTest.login()[pri:0, instance:[email protected]] 1785210046
    FactoryTest.logout()[pri:0, instance:[email protected]] 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中定義了日志詳細等級;

3、自定義XmlSuite

除了直接書寫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();
           

4、設定group

那麼如果想要隻運作其中的幾個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名稱,以

,

分隔;

5、設定輸出目錄

如果想要修改報告輸出目錄,則可以用如下代碼來設定:

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就會把報告輸出到此檔案夾下;

6、調用jar包中的xml

如果我們把帶有TestNG用例的工程打成了jar包,然後要在其他工程裡調用這個jar包中的用例,可以使用如下方法:

TestNG testNG = new TestNG();
testNG.setTestJar(jarPath);//jar包的路徑
testNG.setXmlPathInJar(xmlPathInJar);//jar包中xml檔案的路徑
           

7、其他方法

除了上面所說的那些之外,

TestNG

對象還有其他很多方法,譬如設定

verbose

,設定并發數等;