ITeye博客

TestNG系列2:TestNG初接触

dingtao · 2008-06-25 18:24 · Markdown

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

Java代码 复制代码

  1. import org.testng.annotations.BeforeClass;
  2. import org.testng.annotations.Test;
  3. public class SimpleTest {
  4. @BeforeClass
  5. public void setUp() {
  6. System.out.println("init()");
  7. }
  8. @Test(groups = { "fast" })
  9. 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代码 复制代码

  1. 例如下面,我们不在eclipse运行上面的测试用例,首先,写一个ant的执行脚本,类似:
  2. <project default="test">
  3. <path id="cp">
  4. <pathelement location="lib/testng-testng-4.4-jdk15.jar"/>
  5. <pathelement location="build"/>
  6. </path>
  7. <taskdef name="testng" classpathref="cp"
  8. classname="org.testng.TestNGAntTask" />
  9. <target name="test">
  10. <testng classpathref="cp" groups="fast">
  11. <classfileset dir="build" includes="example1/*.class"/>
  12. </testng>
  13. </target>
  14. </project>
例如下面,我们不在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存在的一个原因.

评论(1)

  1. #1 楼 · jacky9692 · 2008-12-26 17:41

    dfdfdfdfdfdfdf

评论功能维护中,当前仅展示历史评论。