TestNG参数化测试

软件测试中,经常需要测试大量的数据集。测试代码的逻辑完全一样,只是测试的参数不一样。这时我们就需要一种“传递测试参数的机制”。避免写重复的测试代码。

TestNG提供了2种传递参数的方式:

  • testng.xml:使代码和测试数据分离,方便维护
  • @DataProvider:能够提供比较复杂的参数。(也叫data-driven testing)

通过testng.xml传递参数给测试代码

ParameterizedTest.java

package me.yezhou;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParameterizedTest {

    @Test
    @Parameters("myName")
    public void parameterTest(String myName) {
        System.out.println("Parameterized value is : " + myName);
    }

    @Test
    @Parameters({ "china", "america" })
    public void parametersTest(String china, String america) {
        System.out.println("china : " + china + ", america : " + america);
    }

}

parameterized.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite">
    <parameter name="myName" value="Hello World!" />
    <test name="test">
        <parameter name="myName" value="AppBlog.CN" />
        <parameter name="myName" value="Joe.Ye" />
        <parameter name="china" value="中国" />
        <parameter name="america" value="美国" />
        <classes>
            <class name="me.yezhou.ParameterizedTest"/>
        </classes>
    </test>
</suite>

测试结果:

Parameterized value is : Joe.Ye
china : 中国, america : 美国

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

注意参数覆盖机制

TestNG对testng.xml中参数指定的值会尝试自动进行类型转换,以下是支持的类型:

  • String
  • int/Integer
  • boolean/Boolean
  • byte/Byte
  • char/Character
  • double/Double
  • float/Float
  • long/Long
  • short/Short

通过DataProvider传递参数

案例一:字符串类型传递

package me.yezhou;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ParameterizedTest {

    @DataProvider(name = "user")
    public Object[][] Users() {
        return new Object[][]{
                {"root", "passowrd"},
                {"AppBlog.CN", "123456"},
                {"Android", "iOS"}
        };
    }

    @Test(dataProvider = "user")
    public void verifyUser(String username, String password) {
        System.out.println("Username: " + username + ", Password: " + password);
    }
}

测试结果:

Username: root, Password: passowrd
Username: AppBlog.CN, Password: 123456
Username: Android, Password: iOS

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

案例二:整数和布尔类型传递

判断是否为素数类:PrimeNumberChecker.java

package me.yezhou;

public class PrimeNumberChecker {
    public Boolean validate(final Integer primeNumber) {
        for (int i = 2; i < (primeNumber / 2); i++) {
            if (primeNumber % i == 0) {
                return false;
            }
        }
        return true;
    }
}

测试类:ParameterizedTest.java

package me.yezhou;

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ParameterizedTest {

    private PrimeNumberChecker primeNumberChecker;

    @BeforeMethod
    public void initialize() {
        primeNumberChecker = new PrimeNumberChecker();
    }

    @DataProvider(name = "numbers")
    public static Object[][] primeNumbers() {
        return new Object[][]{{2, true}, {6, false}, {19, true},
                {22, false}, {23, true}};
    }

    // This test will run 5 times since we have 5 parameters defined
    @Test(dataProvider = "numbers")
    public void testPrimeNumberChecker(Integer inputNumber, Boolean expectedResult) {
        System.out.println(inputNumber + ": " + expectedResult);
        Assert.assertEquals(expectedResult, primeNumberChecker.validate(inputNumber));
    }
}

测试结果:

2: true
6: false
19: true
22: false
23: true

===============================================
Suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================

案例三:对象传递

对象Bean:Person.java

package me.yezhou;

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

测试类:ParameterizedTest.java

package me.yezhou;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ParameterizedTest {

    @DataProvider(name = "persons")
    public static Object[][] persons() {
        return new Object[][] { { new Person("Joe.Ye", 30) }, { new Person("AppBlog.CN", 2) } };
    }

    @Test(dataProvider = "persons")
    public void testMethod(Person person) {
        System.out.println(person.toString());
    }

}

测试结果:

Person{name='Joe.Ye', age=30}
Person{name='AppBlog.CN', age=2}

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/01/testng-parameterized-test/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
TestNG参数化测试
软件测试中,经常需要测试大量的数据集。测试代码的逻辑完全一样,只是测试的参数不一样。这时我们就需要一种“传递测试参数的机制”。避免写重复的测试代码。 T……
<<上一篇
下一篇>>
文章目录
关闭
目 录