我有一个声明值的API测试。当前,它从excel读取行,然后以普通的for循环循环。如果值匹配,则效果很好。
但是,如果断言失败,则测试将以stacktrace AssertionError结尾即使一个断言失败,有没有办法继续测试?
基本结构如下:
<code>
try {
for (int i = 1; i < 10; i++) {
//Get Excel Data
String progress = sheet.getRow(i).getCell(1).toString();
//Excel Assert Values
String valueToAssert = sheet.getRow(i).getCell(29).toString();
baseURI = "url";
Response response =
given()
//.filter(new AllureRestAssured())
.body(String.valueOf(writer))
.when()
.post()
.then()
.assertThat()
.statusCode(200)
.and()
.body("value", equalTo(valueToAssert))
}
} catch (Exception e) {
e.printStackTrace();
}catch (AssertionError k){
k.printStackTrace();
}
}
</code>
尝试在测试中不要使用for循环,因为那样会导致您遇到问题。
而不是使用参数化测试。
使用参数化测试将创建多个测试,这些测试可能会分别失败。
我同意汤姆的评论-您是出于某种理由主张,如果主张失败-测试应该失败
我无法通过使用普通JAVA想到任何其他方法,但是我当然有一种解决方法可以为您提供帮助
(或者您也可以尝试蒂姆的建议)
此方法更新了excel中的信息,只需查看它,您就可以找到返回错误的实例,以后可以重新运行这些方案了
下面是我煮熟的东西,在这里,为了清晰起见,给出了评论
以下是我的excel考试前的样子
我将完整的执行包装在for循环中,并删除了try catch块
{
FileInputStream fis = new FileInputStream("C:\\Users\\Wilfred\\Desktop\\Book.xls");
Workbook wb = new HSSFWorkbook(fis);
Sheet sheet = wb.getSheetAt(0);
for (int i = 1; i < 4; i++) {
//Excel Handling - Read
String progress = sheet.getRow(i).getCell(1).toString();
String valueToAssert = sheet.getRow(i).getCell(2).toString();
int expected = Integer.parseInt(valueToAssert); // I have used this because I am asserting the StatusCode, You can ignore this
//Rest Assured Code
RestAssured.baseURI = "https://reqres.in/api";
Response response = given().headers("Content-Type", "application/json").when().get(progress).then().extract().response();
int actual = response.getStatusCode();
// I am printing this in the console for fancy sake, You can ignore this if you want
if (actual == expected) {
System.out.println("The actual value and expected value are same");
} else
System.out.println("The Actual value is " + actual + " but the expected value is " + expected + "");
//Excel Handling - Write and Close
Cell cell = null;
cell = sheet.getRow(i).getCell(3); // I am updating my values in column D
cell.setCellValue(actual); // With the values that we fetch from the response
fis.close();
FileOutputStream output_file = new FileOutputStream(new File("C:\\Users\\Wilfred\\Desktop\\Book.xls"));
wb.write(output_file);
output_file.close();
}
}
下面是我的专业考试后的样子
下面是我的控制台输出
实际值和期望值相同
[实际值为404,但预期值为200
实际值和期望值相同