testng 教程


Testng 簡介:

  Testng是一套開源測試框架,是從Junit繼承而來,testng意為test next generation,主要有以下特性:

 

  •  annotations  注釋,如 @test @BeforeMethod 
  • 支持多線程執行case
  • 支持數據驅動 dataProvider
  • 支持參參數
  • 能夠作為eclipse的插件
  • 能夠(配合reportng)生產客觀的測試報告
  • 可通過testng.xml管理執行case和suite

那么好的測試框架,怎么使用?

這里我們使用eclipse插件方式 安裝詳見:http://testng.org/doc/eclipse.html


testng使用


首先了解一下testng 的annotations

常見的有以下:

@BeforeClass: 該annotation在class激活之前執行

@BeforeMethod: 該annotation會在每個執行的方法之前執行

@Test ,該annotation 是你要執行測試的方法

@AfterMethod,該annotation在每個測試方法執行之后運行

@AfterClass 該annotation會在所有測試方法之后運行

具體生命周期如下圖:

這里是所有的annotation

@BeforeSuite
@AfterSuite
@BeforeTest
@AfterTest
@BeforeGroups
@AfterGroups
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
Configuration information for a TestNG class: 

@BeforeSuite: The annotated method will be run before all tests in this suite have run. 
@AfterSuite: The annotated method will be run after all tests in this suite have run. 
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. 
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. 
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. 
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. 
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked. 
@AfterClass: The annotated method will be run after all the test methods in the current class have been run. 
@BeforeMethod: The annotated method will be run before each test method. 
@AfterMethod: The annotated method will be run after each test method.

實例:我們驗證一下testng annotation 執行順序,這個case里有兩個 測試  ,執行順序為beforeClass->beforeMethod->test1->afterMethod->beforeMethod->

test2->afterMethod->afterClass.

package com.dbyl.tests;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
 * This is to verify testng annotation execute 
 * @author Young
 *
 */
public class TestngExample {
    private int a;

    @BeforeMethod(alwaysRun=true)
    public void beforeMethod() {
        a = 2;
        System.out.println("This is beforeMethod method. The Value of a is: "
                + a);
    }

    @BeforeClass
    public void beforeClass() {
        a = 1;
        System.out.println("This is beforeClass method .The Value of a is: "
                + a);
    }

    @Test(groups = "TestngExample")
    public void testExample1() {
        a = 3;
        System.out.println("This is Test  method1 .The Value of a is: " + a);
    }

    @Test(groups = "TestngExample")
    public void testExample2() {
        a = 4;
        System.out.println("This is Test  method2 .The Value of a is: " + a);
    }

    @AfterClass
    public void afterClass() {
        a = 5;
        System.out.println("This is AfterClass Method .The Value of a is: " + a);

    }
    
    @AfterMethod
    public void afterMethod()
    {
        a = 6;
        System.out.println("This is AfterMethod Method .The Value of a is: " + a);
    }

}

 

 所以執行結果為:

 

This is beforeClass method .The Value of a is: 1
This is beforeMethod method. The Value of a is: 2
This is Test method1 .The Value of a is: 3
This is AfterMethod Method .The Value of a is: 6
This is beforeMethod method. The Value of a is: 2
This is Test method2 .The Value of a is: 4
This is AfterMethod Method .The Value of a is: 6
This is AfterClass Method .The Value of a is: 5
PASSED: testExample1
PASSED: testExample2

  當然,還有BeforeSuite 等,不再做深入研究.

annotation后面可以加一些參數,比如alwaysRun=true/false,dependsOnMethods=""

alwaysRun控制是否每次都執行,dependsOnMethods是一種依賴,依賴某個方法執行

之前有提到testng數據驅動,使用dataProvider,dataProvider可以導入text ,excel等數據,

執行case時會從DataProvider依次拿出數據執行,同一個測試方法,會被執行相應的次數

package com.dbyl.tests;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


public class Case1 {

    @DataProvider
    public Object[][] testData1() {
        return new Object[][] { { 1, 2, 3 }, { 1, 2, 4 }, { 1, 3, 4 },
                { -1, 3, 2 } };
    }
    

    @DataProvider
    public Object[][] testData2() {
        return new Object[][] { { 5, 2, 3 }, { 1, 2, 4 }, { 1, -3, 4 },
                { 6, 3, 2 } };
    }

    public static int add(int a, int b) {
        return a + b;
    }
    
    public static int minus(int a, int b) {
        return a - b;
    }

    @BeforeClass
    public void beforeClass()
    {
        System.out.println("This is Before Class");
    }
    @Test(groups = { "add" }, dataProvider = "testData1")
    public void addTest(int a, int b, int c) {
        System.out.println("This is test add method.  "+a+" + "+ b+" = "+c);
        Assert.assertEquals(add(a, b), c);
    }
    
    @Test(groups = { "minus" }, dataProvider = "testData2")
    public void minusTest(int a, int b, int c) {
        System.out.println("This is test minus method.  "+a+" - "+ b+" = "+c);
        Assert.assertEquals(minus(a, b), c);
    }

    @BeforeMethod
    public void beforeMethod()
    {
        System.out.println("This is Before Method");
    }
    
    @AfterMethod
    public void afterMethod()
    {
        System.out.println("This is After Method");
    }
    
    @AfterClass
    public void afterClass()
    {
        System.out.println("This is After Class");
    }
}

 

執行結果如下: 

        

This is Before Class
This is Before Method
This is test add method.  1 + 2 = 3
This is After Method
This is Before Method
This is test add method.  1 + 2 = 4
This is After Method
This is Before Method
This is test add method.  1 + 3 = 4
This is After Method
This is Before Method
This is test add method.  -1 + 3 = 2
This is After Method
This is Before Method
This is test minus method.  5 - 2 = 3
This is After Method
This is Before Method
This is test minus method.  1 - 2 = 4
This is After Method
This is Before Method
This is test minus method.  1 - -3 = 4
This is After Method
This is Before Method
This is test minus method.  6 - 3 = 2
This is After Method
This is After Class
PASSED: addTest(1, 2, 3)
PASSED: addTest(1, 3, 4)
PASSED: addTest(-1, 3, 2)
PASSED: minusTest(5, 2, 3)
PASSED: minusTest(1, -3, 4)
FAILED: addTest(1, 2, 4)
View Code

 


由於天色已晚,明天繼續總結.

接下來:testng.xml的配置和使用,已經參數傳遞

           testng配合reportng 生成測試報告

           testng 配合ant

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM