构建一个TESTNG实例

一.安裝 TestNG eclipse plugin
Eclipse
Help -> Software Updates -> Find and Install -> Search for new features to install
New Remote Site
Name: TestNG
URL: http://beust.com/eclipse
然后安裝.

二.下载 testng-5.0.2.zip
http://testng.org/doc/download.html
http://testng.org/testng-5.0.2.zip

解压缩至 D:"testng-5.0.2
D:"testng-5.0.2"testng-5.0.2-jdk15.jar 加入到 project classpath .

三.源代码

package hummer.hu.study;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

public class TestNGTest {

       @BeforeTest    // 标记指定所有测试进行前呼叫此 method

    public void beforeAllTest(){

        System.out.println("Before All Test");       

    }

   

    @BeforeMethod    // 标记指定每个测试进行前呼叫此 method

    public void setUp() {

        System.out.println("Before Each Test Method");

    }

    @Test(groups = { "group1" })    // 标记为测试程式,并为分组 group1

    public void group1Test() {

        System.out.println("Group 1");

    }

    @Test(groups = { "group2" })    // 标记为测试程式,并为分组 group2

    public void group2Test() {

        System.out.println("Group 2");

    }

   

    @Test(groups = { "group3" })    // 标记为测试程式,并为分组 group3

    public void group3Test() {

        System.out.println("Group 3");

    }

   

    @AfterMethod   // 标记指定每个测试进行后呼叫此 method

    public void tearDown(){

        System.out.println("After Each Test Method");

    }

   

    @AfterTest   // 标记指定所有测试进行后呼叫此 method

    public void afterAllTest(){

        System.out.println("After All Test");       

    }

}

四.右键点选择TestNGTest.java -> Run As -> TestNG Test

五.运行结果

 

[Parser] Running:

 E:"eclipse"workspace"First_TESTNG"temp-testng-customsuite.xml

Before All Test

Before Each Test Method

Group 2

After Each Test Method

Before Each Test Method

Group 1

After Each Test Method

Before Each Test Method

Group 3

After Each Test Method

After All Test

PASSED: group2Test

PASSED: group1Test

PASSED: group3Test

===============================================

    hummer.hu.study.TestNGTest

    Tests run: 3, Failures: 0, Skips: 0

===============================================

===============================================

First_TESTNG

Total tests run: 3, Failures: 0, Skips: 0

===============================================

 



------君临天下,舍我其谁------