首先来看一个非常简单的例子,代码如下:
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class SimpleTest {
@BeforeClass
public void setUp() {
System.out.println("init()");
}
@Test(groups = { "fast" })
public void aFastTest() {
System.out.println("Fast test");
}
@Test(groups = { "slow" })
public void aSlowTest() {
System.out.println("Slow test");
}
}
在安装有TestNG插件的Eclipse开发环境下,运行结果如下:
引用
init()
Fast test
Slow test
PASSED: aFastTest
PASSED: aSlowTest
===============================================
example1.SimpleTest
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
testNG
Total tests run: 2, Failures: 0, Skips: 0
===============================================
至于红条绿条界面的截图就不截上来的,因为例子比较简单啦。在该例子中setUp()方法会任何的test方法前被调用,这一点和JUnit是一模一样的。
创建一个TestNG测试用例:
1、不需要继承任何类或实现特定的接口
2、虽然上面的例子采用了JUnit的一些特定的习惯,但我们可以为测试的方法起任意的名称,如果你的喜欢的话,因为利用annotations就告诉TestNG哪一些方法就是指定的测试的方法(见代码中的@Test)
3、一个方法可以属于一个或多个group.我们可以任意指定哪一些group的测试方法被触发,如果不指定,默认情况下全部的测试方法都会被执行到.
例如下面,我们不在eclipse运行上面的测试用例,首先,写一个ant的执行脚本,类似:
<project default="test">
<path id="cp">
<pathelement location="lib/testng-testng-4.4-jdk15.jar"/>
<pathelement location="build"/>
</path>
<taskdef name="testng" classpathref="cp"
classname="org.testng.TestNGAntTask" />
<target name="test">
<testng classpathref="cp" groups="fast">
<classfileset dir="build" includes="example1/*.class"/>
</testng>
</target>
</project>
运行结果如下:
引用
c:> ant
Buildfile: build.xml
test:
[testng] Fast test
[testng] ===============================================
[testng] Suite for Command line test
[testng] Total tests run: 1, Failures: 0, Skips: 0
[testng] ===============================================
BUILD SUCCESSFUL
Total time: 4 seconds
这时,我们可以清楚地看到"slow"组的group测试方法就没有被调用,也是group存在的一个原因.
分享到:
评论