如何编写 TestNG 以传递来自 excel sheet 的值

How to write TestNG to pass values from an excel sheet

我做错了什么???我不知道为什么没有传递参数!!! 我正在尝试从外部传递值 excel sheet...请帮忙!!

伙计们,请不要将此标记为重复!! 提前致谢

P.S 我尽量不使用 maven..

import Data.Bean;
import org.easetech.easytest.annotation.DataLoader;
import org.easetech.easytest.annotation.Param;
import org.easetech.easytest.loader.LoaderType;
import org.easetech.easytest.runner.DataDrivenTestRunner;
import org.junit.runner.RunWith;
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;

/**
 *
 * @author Effitrac
 */
public class TestNGTestCases {

    public TestNGTestCases() {
    }
    // TODO add test methods here.
    // The methods must be annotated with annotation @Test. For example:
    //
    // @Test
    // public void hello() {}

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @BeforeMethod
    public void setUpMethod() throws Exception {
    }

    @AfterMethod
    public void tearDownMethod() throws Exception {
    }

    /**
     *
     */
    @RunWith(DataDrivenTestRunner.class)
    @DataLoader(filePaths = {"d:/data/kishore/testdata.csv"}, loaderType = LoaderType.CSV)
    public class TestExcelDataLoader {

            Bean b = new Bean();
        @Test
        public void testwelcome(@Param(name = "name") String name, @Param(name = "custID") Integer custID) {
            System.out.print("Executing getExcelTestData :");
//            System.out.println("Name : " + name + " ID : " + custID);
            b.setName(name);
            b.setCustID(custID);
            b.doit();
            System.out.println("Name : " + b.getName() + " ID : " + b.getCustID() + " Result : " + b.getResult());
//            System.out.println("Name : " + name + " ID : " + custID + " Result : " + b.getResult());

        }
    }
}

这是我收到的输出....

[TestNG] Running:
  Command line suite

[VerboseTestNG] RUNNING: Suite: "Command line test" containing "1" Tests (config: null)
[VerboseTestNG] SKIPPED: "Command line test" - TestNGTestCases$TestExcelDataLoader.testwelcome(java.lang.String, java.lang.Integer) finished in 16 ms
[VerboseTestNG] org.testng.TestNGException: 
[VerboseTestNG] Method testwelcome requires 2 parameters but 0 were supplied in the @Test annotation.
[VerboseTestNG] 
[VerboseTestNG] ===============================================
[VerboseTestNG]     Command line test
[VerboseTestNG]     Tests run: 1, Failures: 0, Skips: 1
[VerboseTestNG] ===============================================

===============================================
Command line suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

Java Result: 2
Deleting directory C:\Users\Effitrac\AppData\Local\Temp\TestNGTestCases
test:
BUILD SUCCESSFUL (total time: 6 seconds)

不确定为什么需要使用第三方库来 运行 TestNG with DataDrivenTestRunner.class。以下适用于所有情况,没有任何问题。另外,您为什么要创建 TestNGTestCases class,因为您既没有扩展它也没有向它添加任何测试。

package practise;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

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

public class SO34677983 {
    @Test(dataProvider = "dp")
    public void f(Integer CustId, String Name) {
        System.out.println("Verifying something here");
    }

    @DataProvider
    public Object[][] dp() throws IOException {
        File csvRead = new File("someFilePath.csv");
        BufferedReader br = new BufferedReader(new FileReader(csvRead));
        String line = "";
        Object[][] data = new Object[2][2];
        int dataNum = 0;
        while ((line = br.readLine()) != null) {
            data[dataNum][0] = line.split(",")[0];
            data[dataNum][1] = line.split(",")[1];
            dataNum++;
        }
        br.close();
        return data;
    }
}