如何编写TestNG来传递excel表中的值



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

伙计们,请不要把这个标记为重复!!提前感谢

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:UsersEffitracAppDataLocalTempTestNGTestCases
test:
BUILD SUCCESSFUL (total time: 6 seconds)

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

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;
    }
}

相关内容

  • 没有找到相关文章

最新更新