我正在从Excel中读取数据,并使用Dataproviders将数据传递给测试。我有5个参数,但是Test方法显得笨拙。因此,我想传递一个参数而不是多个参数。我尝试这样做,但是测试失败并显示以下错误:
java.lang.ClassCastException: com.utils.ExcelUtil$TestData cannot be cast to [Ljava.lang.Object;
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1225)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Test.java
@DataProvider
public Iterator<TestData> getData() throws IOException {
List<TestData> data = excelUtil().getDataFromExcel();
return data.iterator();
}
@Test(dataProvider = "getData")
public void verify(TestData testData) {
System.out.println("Value of t1 : " + testData.t1);
}
ExcelUtil.java
public ExcelUtil(String path) throws IOException {
stream = new FileInputStream(path);
workbook = WorkbookFactory.create(stream);
workSheet = workbook.getSheetAt(0);
}
public List<TestData> getDataFromExcel() {
DataFormatter dataFormatter = new DataFormatter();
List<TestData> data = new ArrayList<TestData>();
Iterator<Row> rowIterator = workSheet.iterator();
TestData testResourceData = new TestData();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
if (row.getRowNum() == 0) {
continue;
}
resourceData.t1 = row.getCell(0).getStringCellValue();
resourceData.t2 = row.getCell(1).getStringCellValue();
Double value = Double.parseDouble(dataFormatter.formatCellValue(row.getCell(2)));
Long longValue = value.longValue();
resourceData.t3 = String.valueOf(longValue.toString());
resourceData.t4 = row.getCell(3).getStringCellValue();
resourceData.t5 = row.getCell(4).getStringCellValue();
data.add(resourceData);
}
return data;
}
public class TestData {
public String t1;
public String t2;
public String t3;
public String t4;
public String t5;
}
请提出将数据作为单个参数传递的最佳方法。 谢谢。
答案 0 :(得分:0)
我建议尝试QAF - TestNG's data-provider-extension。它不仅支持动态参数,还支持在从任何外部数据源读取数据时自动complex object argument转换。例如:
@QAFDataProvider (dataFile = "resources/data/logintestdata.xls")
@Test(description = "login functionality test")
public void login(String userName, String pwd) {
//implementation goes here
}
@QAFDataProvider (dataFile = "resources/data/logintestdata.xls")
@Test(description = "login functionality test")
public void login(LoginBean user) {
//implementation goes here
}
@QAFDataProvider (dataFile = "resources/data/mytestdata.xls")
@Test(description = "login functionality test")
public void myTest(LoginBean user, Item item) {
//implementation goes here
}