TestNG 的示例代碼
TestNG 提供了從命令行運(yùn)行測(cè)試用例的方法。下面將首先從命令行運(yùn)行測(cè)試用例。假設(shè)有如下的測(cè)試用例組:
列表 1. TestNG 示例代碼
package example1;
import org.testng.annotations.*;
public class SimpleTest {
@Configuration(beforeTestClass = true)
public void setUp() {
// code that will be invoked when this test is instantiated
}
@Test(groups = { "HelloWorld" })
public void helloWorldTest() {
System.out.println("Hello World");
throw new Error();
}
@Test(threadPoolSize = 10, invocationCount = 5,
timeOut = 1000, groups = { "multiple" })
public void multiThreadTest() {
System.out.println("MultiThread test");
}
@Test(groups = { "HelloNature" })
public void helloNatureTest() {
System.out.println("Hello Nature");
throw new Error();
}
}
運(yùn)行 TestNG 的 Ant 腳本
為了運(yùn)行這組測(cè)試用例,構(gòu)建了如下的 Ant 運(yùn)行腳本:
列表 2. 運(yùn)行測(cè)試用例組的 Ant 腳本 build.xml 文件
<project default="test">
<path id="cp">
<pathelement location=
"c:/spark/eclipse/plugins/org.testng.eclipse_4.7.0.0/lib/testng-jdk15.jar"/>
<pathelement location="c:"/>
</path>
<taskdef name="testng" classpathref="cp"
classname="org.testng.TestNGAntTask" />
<target name="test">
<testng classpathref="cp" groups="HelloWorld, HelloNature">
<classfileset dir="./" includes="example1/*.class"/>
</testng>
</target>
</project>
運(yùn)行之后的結(jié)果如下:
圖 1. 命令行運(yùn)行 TestNG 的結(jié)果
運(yùn)行完成之后,會(huì)在運(yùn)行目錄下生成一個(gè) test-output 目錄。如圖 2 所示:
圖 2. 生成的 test-output 目錄
該目錄中包含有 html 形式的運(yùn)行結(jié)果的報(bào)告,通過命令 start test-outputindex.html 可以查看生成的測(cè)試報(bào)告。