鍍金池/ 教程/ Java/ TestNG參數(shù)測試實(shí)例
TestNG參數(shù)測試(DataProvider)
TestNG配置注解實(shí)例
TestNG預(yù)期異常測試
TestNG + Selenium負(fù)載測試
TestNG參數(shù)化測試
TestNG參數(shù)測試(XML和DataProvider)
TestNG配置注釋
TestNG套件測試
TestNG依賴測試
TestNG忽略測試
TestNG忽略測試
TestNG分組測試
TestNG預(yù)期異常測試
TestNG + Selenium
TestNG參數(shù)測試實(shí)例
TestNG超時(shí)測試
TestNG Hello World入門示例
JUnit 4 Vs TestNG比較
TestNG + Spring集成示例
TestNG分組測試
TestNG教程
TestNG超時(shí)測試
JUnit 4 Vs TestNG比較
TestNG基本注解
TestNG依賴性測試
TestNG套件測試
TestNG + Spring集成測試

TestNG參數(shù)測試實(shí)例

另一個(gè)TestNG參數(shù)測試示例,是使用@DataProvider注解。

1. CharUtil類

創(chuàng)建一個(gè)將字符轉(zhuǎn)換成ASCII或者副詞的類,如何使用TestNG來做單元測試?

打開 Eclipse 創(chuàng)建一個(gè) Maven 工程: ParameterTesting,其目錄結(jié)構(gòu)如下所示 -

類文件:CharUtils.java 的代碼如下 -

package com.yiibai;

/**
 * Character Utility class
 *
 * @author yiibai
 *
 */
public class CharUtils {
    /**
     * Convert the characters to ASCII value
     *
     * @param character character
     * @return ASCII value
     */
    public static int CharToASCII(final char character) {
        return (int) character;
    }

    /**
     * Convert the ASCII value to character
     *
     * @param ascii ascii value
     * @return character value
     */
    public static char ASCIIToChar(final int ascii) {
        return (char) ascii;
    }
}

2. TestNG @DataProvider示例

要測試它,創(chuàng)建一個(gè)接受兩個(gè)參數(shù)(字符和預(yù)期ASCII)的@Test方法,并且測試數(shù)據(jù)從數(shù)據(jù)提供者傳遞。

創(chuàng)建一個(gè)類文件:CharUtilsTest.java 的代碼如下 -

package com.yiibai;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
 * Character Utils Testing
 * @author 
 *
 */
public class CharUtilsTest {

    @DataProvider
    public Object[][] ValidDataProvider() {
        return new Object[][]{
            { 'A', 65 },{ 'a', 97 },
            { 'B', 66 },{ 'b', 98 },
            { 'C', 67 },{ 'c', 99 },
            { 'D', 68 },{ 'd', 100 },
            { 'Z', 90 },{ 'z', 122 },
            { '1', 49 },{ '9', 57 }
        };
    }

    @Test(dataProvider = "ValidDataProvider")
    public void CharToASCIITest(final char character, final int ascii) {

           int result = CharUtils.CharToASCII(character);
           Assert.assertEquals(result, ascii);

    }

    @Test(dataProvider = "ValidDataProvider")
    public void ASCIIToCharTest(final char character, final int ascii) {

           char result = CharUtils.ASCIIToChar(ascii);
           Assert.assertEquals(result, character);

    }
}

執(zhí)行上面測試類,得到結(jié)果如下 -

[TestNG] Running:
  C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-85008796\testng-customsuite.xml

PASSED: ASCIIToCharTest(A, 65)
PASSED: ASCIIToCharTest(a, 97)
PASSED: ASCIIToCharTest(B, 66)
PASSED: ASCIIToCharTest(b, 98)
PASSED: ASCIIToCharTest(C, 67)
PASSED: ASCIIToCharTest(c, 99)
PASSED: ASCIIToCharTest(D, 68)
PASSED: ASCIIToCharTest(d, 100)
PASSED: ASCIIToCharTest(Z, 90)
PASSED: ASCIIToCharTest(z, 122)
PASSED: ASCIIToCharTest(1, 49)
PASSED: ASCIIToCharTest(9, 57)
PASSED: CharToASCIITest(A, 65)
PASSED: CharToASCIITest(a, 97)
PASSED: CharToASCIITest(B, 66)
PASSED: CharToASCIITest(b, 98)
PASSED: CharToASCIITest(C, 67)
PASSED: CharToASCIITest(c, 99)
PASSED: CharToASCIITest(D, 68)
PASSED: CharToASCIITest(d, 100)
PASSED: CharToASCIITest(Z, 90)
PASSED: CharToASCIITest(z, 122)
PASSED: CharToASCIITest(1, 49)
PASSED: CharToASCIITest(9, 57)

===============================================
    Default test
    Tests run: 24, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 24, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@721e0f4f: 214 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@5e025e70: 57 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 1 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@71c7db30: 13 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@23223dd8: 21 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@6833ce2c: 80 ms