【问题标题】:Run all test with same testdata使用相同的测试数据运行所有测试
【发布时间】:2019-04-20 20:45:00
【问题描述】:

从 excel 文件中读取测试数据。 要求:我首先希望我的所有测试都应该在相同的测试数据上运行,即。 excel 行,然后用另一行全部测试。

分辨率: 尝试将@Factory 与我的@DataProvider 一起使用

问题: 如果我在 @dataProvider 中使用硬核值,它就可以正常工作。 但是当从excel中动态获取时,如果给我错误:[ERROR] Caused by: java.lang.NullPointerException

package com;

import java.util.HashMap;
import utils.*;
import org.testng.annotations.*;

public class TestFile extends BaseClass {


public HashMap<String, String> values = new HashMap<String, String>();

@Test()
private void Test_01() {

    System.out.println(values.get("Number"));
}

@Factory(dataProvider = "getDataSet")
public TestFile(HashMap<String, String> values) {
    this.values = values;

}


@DataProvider
private static Object[] getDataSet() {
    String sheetName = mContext.getCurrentXmlTest().getSuite().getParameters().get(Constants.BU_LEISURE_SHEET_NAME);

    int i = 0;
    Object[] dataSet = null;
    HashMap<String, HashMap<String, String>> sheet = testData.get(sheetName);
    dataSet = new Object[sheet.size()][1];
    for (String row : sheet.keySet()) {

        HashMap<String, String> rowValuesMap = new HashMap<>();
        for (String columnName : sheet.get(row).keySet()) {
            rowValuesMap.put(columnName, sheet.get(row).get(columnName));
        }

        dataSet[i] = rowValuesMap;

        i++;
    }
    return dataSet;

}

}

基类

public class BaseClass extends DataReader {
@BeforeSuite
public static void setup(ITestContext context) throws IOException {
    mContext = context;
 // other code
}
}

读取excel文件的Datareader类

public class DataReader {
public static HashMap<String, HashMap<String, HashMap<String, String>>> testData = new HashMap<String, HashMap<String, HashMap<String, String>>>();

// other code
}

错误日志

[ERROR] 未能执行目标 org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test 项目 ATGDMAP 上的(默认测试):执行目标的默认测试 org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test failed: 分叉进程中出现错误 [错误] java.lang.RuntimeException: java.lang.NullPointerException [错误] 在 org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:49) [错误] 在 org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:131) [错误] 在 org.testng.internal.Parameters.handleParameters(Parameters.java:706) [错误] 在 org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:151) [错误] 在 org.testng.internal.TestNGClassFinder.processFactory(TestNGClassFinder.java:223) [错误] 在 org.testng.internal.TestNGClassFinder.processMethod(TestNGClassFinder.java:179) [错误] 在 org.testng.internal.TestNGClassFinder.processClass(TestNGClassFinder.java:171) [错误] 在 org.testng.internal.TestNGClassFinder.(TestNGClassFinder.java:121) [错误] 在 org.testng.TestRunner.initMethods(TestRunner.java:370) [错误] 在 org.testng.TestRunner.init(TestRunner.java:271) [错误] 在 org.testng.TestRunner.init(TestRunner.java:241) [错误] 在 org.testng.TestRunner.(TestRunner.java:167) [错误] 在 org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:663) [错误] 在 org.testng.SuiteRunner.init(SuiteRunner.java:260) [错误] 在 org.testng.SuiteRunner.(SuiteRunner.java:198) [错误] 在 org.testng.TestNG.createSuiteRunner(TestNG.java:1295) [错误] 在 org.testng.TestNG.createSuiteRunners(TestNG.java:1273) [错误] 在 org.testng.TestNG.runSuitesLocally(TestNG.java:1128) [错误] 在 org.testng.TestNG.runSuites(TestNG.java:1049) [错误] 在 org.testng.TestNG.run(TestNG.java:1017) [错误] 在 org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:295) [错误] 在 org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:84) [错误] 在 org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:90) [错误] 在 org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203) [错误] 在 org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155) [错误] 在 org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) [错误] 引起:java.lang.NullPointerException [错误] 在 com.TestFile.getDataSet(TestFile.java:27) [错误] 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [错误] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [错误] 在 java.lang.reflect.Method.invoke(Method.java:498) [错误] 在 org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) [错误] 在 org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74) [错误] 在 org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:45) [ERROR] ... 25 个 [ERROR] -> [Help 1] [ERROR] [ERROR] 查看 错误的完整堆栈跟踪,使用 -e 开关重新运行 Maven。 [错误] 使用 -X 开关重新运行 Maven 以启用完整的调试日志记录。

【问题讨论】:

  • 在你调用mContext之前,你能添加更多关于它的代码吗?并请添加日志...
  • 请立即查看@JonathanJohx
  • 问题出在这一行 String sheetName = mContext.getCurrentXmlTest().getSuite().getParameters().get(Constants.BU_LEISURE_SHEET_NAME);我想运行这个项目,但我不确定你是否可以分享它..或者你看看调试模式这个方法抛出什么 null..
  • mContext.getCurrentXmlTest().getSuite().getParameters().get(Constants.BU_LEISURE_SHEET_NAME);很好,因为如果我不使用工厂和静态数据提供程序,那么一切都可以作为一种魅力。 @Jonathan Johx

标签: java selenium testng dataprovider


【解决方案1】:

问题在于您的测试代码。

您通过@BeforeSuite 注释方法获取ITestContext 对象。但是当您使用与数据提供者耦合的工厂时,TestNG 首先找到与您的工厂方法关联的数据提供者(本例中为构造函数)。一旦找到数据提供者方法,它就会调用它来获取所有用于迭代和创建测试类实例的数据集。在您的情况下,数据提供者指的是一个上下文对象(在这种情况下为mContext),只有在调用@BeforeSuite 方法时才能初始化该对象。 @BeforeSuite 方法不会被调用,除非实例被创建(这是工厂方法试图做的)

这是正确的做法。

我的示例中的 excel 电子表格有两张表格,即,

  1. 员工
  2. 学生

员工表中包含以下数据

+--------+--------------------+
|  name  |       email        |
+--------+--------------------+
| John   | john@example.com   |
| Rajeev | rajeev@example.com |
| Jack   | jack@example.com   |
+--------+--------------------+

student表中有以下数据。

+-------+--------+
| name  | rollNo |
+-------+--------+
| Surya |      1 |
| Rajni |      2 |
| Kamal |      3 |
+-------+--------+

使用工厂的测试类和读取上述电子表格的数据提供者

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class TestclassSample {

  private Map<String, String> data;
  private static final String SHEET = "sheet";
  private static final String FILENAME = "filename";

  @Factory(dataProvider = "dp")
  public TestclassSample(Map<String, String> data) {
    this.data = data;
  }

  @Test
  public void dataIsNotEmpty() {
    System.err.println("Ensuring that " + data + " is not empty");
    Assert.assertFalse(data.isEmpty());
  }

  @Test
  public void dataHasAtleastTwoColumns() {
    System.err.println("Ensuring that " + data + " has atleast 2 entries");
    Assert.assertTrue(data.size() >= 2);
  }

  @DataProvider(name = "dp")
  public static Object[][] getData(ITestContext context)
      throws IOException, InvalidFormatException {
    String filename = context.getCurrentXmlTest().getParameter(FILENAME);
    if (filename == null || filename.trim().isEmpty()) {
      throw new IllegalArgumentException("File name was not present as a parameter");
    }
    Workbook workbook = WorkbookFactory.create(new File(filename));
    String sheet = context.getCurrentXmlTest().getParameter(SHEET);
    if (sheet == null || sheet.trim().isEmpty()) {
      throw new IllegalArgumentException("Sheet name was not present as a parameter");
    }
    Sheet worksheet = workbook.getSheet(sheet);
    DataFormatter dataFormatter = new DataFormatter();
    Row headingRow = worksheet.getRow(0);
    List<String> heading = new ArrayList<>();
    for (Cell cell : headingRow) {
      heading.add(dataFormatter.formatCellValue(cell));
    }
    List<Map<String, String>> information = new ArrayList<>();
    for (int i = 1; i < worksheet.getLastRowNum(); i++) {
      Map<String, String> data = new HashMap<>();
      int index = 0;
      for (Cell cell : worksheet.getRow(i)) {
        data.put(heading.get(index++), dataFormatter.formatCellValue(cell));
      }
      information.add(data);
    }

    Object[][] data = new Object[information.size()][1];
    for (int i = 0; i < information.size(); i++) {
      data[i] = new Object[] {information.get(i)};
    }
    workbook.close();
    return data;
  }
}

testng 套件 xml 如下所示

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="53358567_suite" parallel="false" verbose="2" group-by-instances="true">
    <parameter name="sheet" value="student"/>
    <parameter name="filename" value="src/test/resources/53358567.xlsx"/>
    <test name="53358567_test">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn53358567.TestclassSample"/>
        </classes>
    </test>
</suite>

输出如下

...
... TestNG 7.0.0-beta1 by Cédric Beust (cedric@beust.com)
...
Ensuring that {name=Rajni, rollNo=2} has atleast 2 entries
Ensuring that {name=Rajni, rollNo=2} is not empty
Ensuring that {name=Surya, rollNo=1} has atleast 2 entries
Ensuring that {name=Surya, rollNo=1} is not empty
PASSED: dataHasAtleastTwoColumns
PASSED: dataIsNotEmpty
PASSED: dataHasAtleastTwoColumns
PASSED: dataIsNotEmpty

===============================================
    53358567_test
    Tests run: 4, Failures: 0, Skips: 0
===============================================

===============================================
53358567_suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

【讨论】:

    猜你喜欢
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2013-07-29
    • 1970-01-01
    • 2022-12-15
    相关资源
    最近更新 更多