【问题标题】:API Testing / REST Assured Automation Testing Advice and or suggestionsAPI 测试 / REST 保证自动化测试建议和/或建议
【发布时间】:2020-04-08 07:24:01
【问题描述】:

真的在寻找一些实用的建议和一般指导。 下面是目前的情景。 我有一个 excel 文档,每一行都将被视为带有输入的测试。 如果不是数千行,也会有数百行。 例如,假设 Row1 看起来像

Col1----------|Col2----------------|Col3
TestingUser|TestingSurname|1980/01/01

这需要我映射到 JSON 对象,然后发送/POST 到 API 端点。 然后我需要断言返回的数据以确保它是正确的值。 我看过的工具是:

ReadyAPI
放心.io

您会推荐任何其他工具或框架来进行此类测试吗? 如果你曾经做过一些事情,你可以提供一个很好的例子。

【问题讨论】:

  • RestAssured 是很好的选择,我使用 Java + JUnit + Rest Assured + Maven 组合完成了类似的任务。您可以使用 jxlapache-poi 读取 excel 数据,将数据转换为带有 Rest Assured 的 JSON 对象,并从 JUnit 中返回 @Parameters 注释中的 JSON 对象。 @Parameters注解方法返回的参数,将单独测试。您还可以与maven-surefire-plugin<parallel>classes</parallel> 并行运行它们
  • 在 java 中有没有办法像 json 模板一样,因为只有一些项目会被映射,而其他所需的 JSON 对象将保持静态。所以我要说的是有一个 JSON 模板,然后将 excel 数据映射到特定的 JSON 路径。
  • 您可以创建代表 JSON 对象的 POJO 类。将静态数据放入其中,并使用 excel 中的数据更新其余数据
  • @Flenters 尝试执行以下步骤: 1. 通过 maven 导入 Rest Assured 库或导入 .jar 文件 2. 准备好使用 Rest Assured 的 POJO 并从 excel 表中读取数据 3. 创建代表 JSON 的类对象,为 excel 中的其他数据添加静态数据和 setter 和 getter 4. 使用 Rest Assured 的 Object Mapper 构建 JSON 字符串 5. 尝试使用生成的 JSON 6 创建对 API 的简单请求。阅读 @DataProvider 或 @987654330 @,取决于您使用的测试框架 7. 将它们组合在一起。这基本上就是我会做的,这将是完整的解决方案
  • 您也可以提出个别问题,以便我们限制您可能遇到的特定问题。如果您愿意,也可以在 StackOverflow 之外与我联系

标签: json testing automated-tests rest-assured web-api-testing


【解决方案1】:

我无法提供建议,因为我没有在 RestAssured 上工作过。但以下是 ReadyAPI 的一些优点:

  1. 学习曲线较浅,任何测试人员都可以构建测试 case 不依赖于任何编程语言。 ReadyAPI 有 内置功能可从不同数据源(DB、XML、 json、csv、excel 等)并通过传递这些来调用 REST 端点 字段到端点的 Header、query 和 Json Body。 可以使用 DataSink 选项将每个调用的响应转储到文件中 为文件中的记录发出的每个请求调用的测试步骤。

  2. 工具的结构可以轻松构建具有多个测试的测试用例 脚步。它更像是拖放来构建您的测试用例。层次结构 是项目 -> 测试套件 -> 测试用例 -> 测试步骤。

  3. 使用 testRunner 与广泛的 Jenkins CI/CD 管道轻松集成 多种测试报告功能。测试报告可作为 Allure, jasper 报告,junit 样式报告。

  4. 对于更多需要更多控制的技术测试人员可以使用 Groovy,javascript 构建框架的语言。

  5. VirtServer 和 LoadUI 是 SmartBear 的其他工具,可用于模拟 服务并根据需要运行性能测试。

我在这里有一个重要的评论,如果文件很大(甚至 1000 行),我已经看到 Ready API 正在苦苦挣扎,因为该工具在后面进行了繁重的工作。因此建议使用利用 Java API 的 groovy 脚本进行任何文件操作。

【讨论】:

    【解决方案2】:

    好的,所以我创建了一个使用速度作为 json 模板引擎的类。

    我创建了一个测试,在该测试中我有一个正常的 java 循环。 这将遍历整个 xls、映射值并发布到 API。

    这一切都按预期工作。 问题是跑步者显示 默认套件 运行的测试总数:1,通过:0,

    但是,循环确实运行了 x 次。 我如何在执行测试时更新它,它显示总测试运行 10 或与循环相同的数量。

    希望这是有道理的

    @Test
    public void generatePostData() throws IOException {
        Workbook wb = WorkbookFactory.create(new File("data\\sc1.xlsx"));
        Sheet sheet = wb.getSheetAt(0);
    
        for (int i = 1; i < 10; i++) {
            //Get Excel Data
            Cell testNumber = sheet.getRow(i).getCell(1);
            System.out.println(testNumber.getNumericCellValue());
            //Velocity
            VelocityEngine ve = new VelocityEngine();
            ve.init();
            //get the template
            Template t = ve.getTemplate("post.json");
            //create context and add data
            VelocityContext context = new VelocityContext();
            //map data
            context.put("tpltestNumber", testNumber);
            //render to stringWriter
            StringWriter writer = new StringWriter();
            t.merge(context, writer);
    
            baseURI = "someURL";
            Response response =
                    given()
                            .contentType("application/json")
                            .body(String.valueOf(writer))
                            .when()
                            .post()
                            .then()
                            .assertThat()
                            .statusCode(200)
                            .extract()
                            .response();
    
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      这是主要问题的记者在回答部分提出的问题的答案。 (如何获取执行的excel行数,总执行的测试用例数)

      为此,您必须使用带有 DataProvider 注释的方法来传递数据。

      TestNG documentation

      DataProvider in TestNG

      @DataProvider(name = "dp")
      private Object[][] dataProvider() {
          Workbook wb;
          Sheet sheet = null;
          Object[][] excelRowArray = new Object[10][]; //this 10 the row count in the excel file
      
          try {
              wb = WorkbookFactory.create(new File("data\\sc1.xlsx"));
              sheet = wb.getSheetAt(0);
          } catch (IOException e) {
              e.printStackTrace();
          }
      
          for (int i = 1; i < 10; i++) {// Here 10 is the row count in the excel sheet
              //Get Excel Data row by row
              Cell testNumber = sheet.getRow(i).getCell(1);
              System.out.println(testNumber.getNumericCellValue());
      
              // Create a object array with the values taken from a singe excel row
              Object[] excelRow = new Object[]{testNumber};
      
              // Add the created object array to the 'excelRowArray'
              excelRowArray[i - 1] = excelRow;
      
          }
          return excelRowArray;
      }
      
      @Test(dataProvider = "dp")
      public void generatePostData(Object[] excelRow) {
          // Here single excelRow will be passed each time.
          // And this will run till all object[] in excelRowArray are finished.
          // Total tests executed will be the number of 'excelRow' object arrays in 
          // excelRowArray. (Or excel row count in the sheet)
      
          //Velocity
          VelocityEngine ve = new VelocityEngine();
          ve.init();
          //get the template
          Template t = ve.getTemplate("post.json");
          //create context and add data
          VelocityContext context = new VelocityContext();
          //map data
          context.put("tpltestNumber", excelRow); // Here excelRow is used as the value
          //render to stringWriter
          StringWriter writer = new StringWriter();
          t.merge(context, writer);
      
          String baseURI = "someURL";
          Response response =
                  given()
                          .contentType("application/json")
                          .body(String.valueOf(writer))
                          .when()
                          .post()
                          .then()
                          .assertThat()
                          .statusCode(200)
                          .extract()
                          .response();
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-09-30
        • 1970-01-01
        • 2020-02-07
        • 1970-01-01
        • 1970-01-01
        • 2010-09-05
        • 1970-01-01
        • 2011-06-10
        相关资源
        最近更新 更多