我正在为我的Testng Suite使用自定义记者。我从在线教程中借了基本代码,并进行了一些调整。但是,我想以任何失败的测试包含一个堆栈跟踪,就像内构建的报告一样(可elighable-report.html(。只需将本报告中看到的内容附加到我的底部。
谁能给我任何关于如何实现这一目标的指示?我不知道如何访问堆栈跟踪。
这是GenerateReport方法(我可以显示更多但可能没有相关的内容(:
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
try {
// Get content data in TestNG report template file.
String customReportTemplateStr = this.readEmailableReportTemplate();
// Create custom report title.
String customReportTitle = this.getCustomReportTitle(Base.Client + " Regression Suite Report");
// Create test suite summary data.
String customSuiteSummary = this.getTestSuiteSummary(suites);
// Create test methods summary data.
String customTestMethodSummary = this.getTestMethodSummary(suites);
// Replace report title place holder with custom title.
customReportTemplateStr = customReportTemplateStr.replaceAll("\$TestNG_Custom_Report_Title\$",
customReportTitle);
// Replace test suite place holder with custom test suite summary.
customReportTemplateStr = customReportTemplateStr.replaceAll("\$Test_Case_Summary\$", customSuiteSummary);
// Replace test methods place holder with custom test method summary.
customReportTemplateStr = customReportTemplateStr.replaceAll("\$Test_Case_Detail\$",
customTestMethodSummary);
// Write replaced test report content to custom-emailable-report.html.
File targetFile = new File(outputDirectory + "/custom-emailable-report.html");
FileWriter fw = new FileWriter(targetFile);
fw.write(customReportTemplateStr);
fw.flush();
fw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
...这是HTML模板:
<body>
<table>
<tr><center><font size="5" face="verdana">
<b>$TestNG_Custom_Report_Title$</b>
</font></center></tr>
<p></p>
<thead>
<tr>
<th># Total Method</th>
<th># Passed</th>
<th># Skipped</th>
<th># Failed</th>
<th>Start Time</th>
<th>End Time</th>
<th>Execute Time (hh:mm:ss)</th>
</tr>
</thead>
$Test_Case_Summary$
</table>
<table id="summary">
<thead>
<tr>
<th>Class</th>
<th>Method</th>
<th>Start Time</th>
<th>Execution Time (hh:mm:ss)</th>
<th>Browser</th>
<th>Screenshot</th>
</tr>
</thead>
$Test_Case_Detail$
</table>
</body>
import org.testng.internal.Utils;
private void generateResult(ITestResult ans){
ITestResult ans;
List<String> msgs = Reporter.getOutput(ans);
Throwable exception = ans.getThrowable();
boolean hasThrowable = exception != null;
if (hasThrowable) {
String stackTraceMsg = Utils.stackTrace(exception, true)[0];
}
}