---
title: "TestNG系列2:TestNG初接触"
author: "dingtao"
date: "2008-06-25T18:24:53Z"
tags: ["Ant", "Eclipse", "XML", "junit", "脚本"]
slug: "testng-2-testng"
draft: false
lastmod: "2008-06-25T18:24:53Z"
id: 207979
url: "https://dingtao.iteye.com/blog/207979"
---

首先来看一个非常简单的例子,代码如下：

Java代码 [![复制代码](http://lighter.iteye.com/images/icon_copy.gif)](http://lighter.iteye.com/blog/184231 "复制代码")

01. **import** org.testng.annotations.BeforeClass;
02. **import** org.testng.annotations.Test;
03. **public** **class** SimpleTest {
04. @BeforeClass
05. **public** **void** setUp() {
06. System.out.println("init()");
07. }
08. @Test(groups = { "fast" })
09. **public** **void** aFastTest() {
10. System.out.println("Fast test");
11. }
12. @Test(groups = { "slow" })
13. **public** **void** aSlowTest() {
14. System.out.println("Slow test");
15. }
16. }

```
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的测试方法被触发,如果不指定,默认情况下全部的测试方法都会被执行到.

Java代码 [![复制代码](http://lighter.iteye.com/images/icon_copy.gif)](http://lighter.iteye.com/blog/184231 "复制代码")

01. 例如下面，我们不在eclipse运行上面的测试用例，首先，写一个ant的执行脚本，类似：
02. &lt;project **default**="test"&gt;
03. &lt;path id="cp"&gt;
04. &lt;pathelement location="lib/testng-testng-4.4-jdk15.jar"/&gt;
05. &lt;pathelement location="build"/&gt;
06. &lt;/path&gt;
07. &lt;taskdef name="testng" classpathref="cp"
08. classname="org.testng.TestNGAntTask" /&gt;
09. &lt;target name="test"&gt;
10. &lt;testng classpathref="cp" groups="fast"&gt;
11. &lt;classfileset dir="build" includes="example1/\*.class"/&gt;
12. &lt;/testng&gt;
13. &lt;/target&gt;
14. &lt;/project&gt;

```
例如下面，我们不在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:&gt; 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存在的一个原因.