testng入门教程14数据驱动(不同文件)

本文介绍了一种使用TestNG框架进行数据驱动测试的方法。通过定义数据提供者与测试用例结合的方式,实现了测试数据与测试脚本的分离,提高了测试效率及可维护性。文章详细展示了如何创建数据提供者并将其应用于具体的测试场景中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据与test脚本不在同一文件时,下面是数据:

package data_driver_two_parts;

import org.testng.annotations.DataProvider;

public class TestDataProvider {
    @DataProvider
    public static Object[][] DataMethodNoName(){
        return new Object[][]{
            {"data with no name 1"},
            {"data with no name 2"},
            {"data with no name 3"}
        };
    }    



    @DataProvider(name="datamethodwithname")
    public static Object[][] DataMethodwithName(){
        return new Object[][]{
            {"data with name 4"},
            {"data with name 5"},
            {"data with name 6"}
        };
    }       
    }

下面是test脚本:

package data_driver_two_parts;

import org.testng.annotations.Test;

public class testcase_for_driver {
    
    @Test(dataProvider="DataMethodNoName",dataProviderClass=data_driver_two_parts.TestDataProvider.class)
    public void testDataDriven_1(String a){
        System.out.println("this is : "+a);
        }


    @Test(dataProvider="datamethodwithname",dataProviderClass=data_driver_two_parts.TestDataProvider.class)
    public void testDataDriven_2(String b){
        System.out.println("l am "+b);
    }
}

右键----->RunAs----->TestNG test   运行结果如下:

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

this is : data with no name 1
this is : data with no name 2
this is : data with no name 3
l am data with name 4
l am data with name 5
l am data with name 6
PASSED: testDataDriven_1("data with no name 1")
PASSED: testDataDriven_1("data with no name 2")
PASSED: testDataDriven_1("data with no name 3")
PASSED: testDataDriven_2("data with name 4")
PASSED: testDataDriven_2("data with name 5")
PASSED: testDataDriven_2("data with name 6")

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


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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值