API测试/ REST确保自动化测试的建议和或建议

问题描述 投票:0回答:3

真的在寻找一些实用的建议和一般指导。以下是当前方案。我有一个Excel文档,每行都将被视为带有输入的测试。如果没有几千行,将有几百行。例如,假设Row1看起来像

Col1 ---------- | Col2 ----------------- | Col3TestingUser | TestingSurname | 1980/01/01

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

您是否会推荐其他任何工具或框架用于此类测试。如果您使用过某些东西,可以提供一个很好的例子。

json testing automated-tests rest-assured web-api-testing
3个回答
0
投票

我无法使用RestAssured,因此无法提供推荐。但是,ReadyAPI的一些优点如下:

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

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

  3. 使用testRunner与Jenkins CI / CD管道轻松集成各种测试报告功能。测试报告以“魅力”的形式提供,碧玉报告,junit样式报告。

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

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

我要在这里发表重要的评论,如果文件很大(甚至1000行),我已经看到Ready API处于挣扎状态,因为该工具在后面进行了繁重的工作。因此,建议对任何文件操作使用使用Java API的常规脚本。


0
投票

好,所以我使用Velocity作为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();

    }

}

0
投票

这是主要问题的报告者在答案部分提出的问题的答案。 (如何获得已执行的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();

}
© www.soinside.com 2019 - 2024. All rights reserved.