是否可以将所有失败的测试用例存储在 HashMap 中,然后在类的末尾调用存储在映射中的所有值?
变量:
private HashMap<String, Integer> serverStatusMap = new HashMap<String, Integer>();
方法代码之后:
@AfterMethod
public void trackServerStatus(ITestResult testResult) {
if (testResult.getStatus() == ITestResult.FAILURE) {
try {
String testName = this.getClass().getSimpleName().toString();
int serverStatus = ServerStatus.getResponseCode(basePage.getCurrentURL());
int i = 0;
while(i < serverStatusMap.size()) {
serverStatusMap.put(testName, serverStatus);
i++;
}
//serverStatusMap.put(testName, serverStatus);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
课后调用 map 中存储的值:
@AfterClass
public void sendEmailBasedOnFailure(ITestContext context) throws WebDriverException, Exception {
String tempTime = new SimpleDateFormat("hh.mm.ss").format(new Date());
if(context.getFailedTests().size() > 0) {
SendEmailFile.sendEmailReport(
"TIME: " + tempTime + " | " + this.getClass().getPackage().toString(),
"TIME: " + tempTime + " | " + this.getClass().getPackage().toString() + " | " + "CLASS NAME: "
+ this.getClass().getSimpleName().toString() + "\n\n" +
"TOTAL NUMBER FAILED TESTS: " + context.getFailedTests().size() + "\n\n" +
"FAILED TEST CASES: " + context.getFailedTests().getAllMethods().toString() + "\n\n" +
serverStatusMap.toString());
}
看最后一行代码:'serverStatusMap.toString()'
map 的当前输出: {}
最佳答案
关于java - 每次测试执行后(TestNG 类)将 'Data' 存储在 HashMap 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41829545/