Best Testng code snippet using org.testng.reporters.EmailableReporter.generateSuiteSummaryReport
Source:EmailableReporter.java
...47 System.out.println("output file"+e);48 return;49 }50 startHtml(m_out);51 generateSuiteSummaryReport(suites);52 generateMethodSummaryReport(suites);53 generateMethodDetailReport(suites);54 endHtml(m_out);55 m_out.flush();56 m_out.close();57// SendMail.szFileName=outdir;58//// m_out.println("<h1>" + SendMail.path + "</h1>");59 }60 protected PrintWriter createWriter(String outdir) throws IOException {61 new File(outdir).mkdirs();62 return new PrintWriter(new BufferedWriter(new FileWriter(new File(outdir,63 "emailable-report2.html"))));64 }65 /** Creates a table showing the highlights of each test method with links to the method details */66 protected void generateMethodSummaryReport(List<ISuite> suites) {67 m_methodIndex = 0;68 startResultSummaryTable("methodOverview");69 int testIndex = 1;70 for (ISuite suite : suites) {71 if(suites.size()>1) {72 titleRow(suite.getName(), 5);73 }74 Map<String, ISuiteResult> r = suite.getResults();75 for (ISuiteResult r2 : r.values()) {76 ITestContext testContext = r2.getTestContext();77 String testName = testContext.getName();78 m_testIndex = testIndex;79 resultSummary(suite, testContext.getFailedConfigurations(), testName,80 "failed", " (configuration methods)");81 resultSummary(suite, testContext.getFailedTests(), testName, "failed",82 "");83 resultSummary(suite, testContext.getSkippedConfigurations(), testName,84 "skipped", " (configuration methods)");85 resultSummary(suite, testContext.getSkippedTests(), testName,86 "skipped", "");87 resultSummary(suite, testContext.getPassedTests(), testName, "passed",88 "");89 testIndex++;90 }91 }92 m_out.println("</table>");93 }94 /** Creates a section showing known results for each method */95 protected void generateMethodDetailReport(List<ISuite> suites) {96 m_methodIndex = 0;97 for (ISuite suite : suites) {98 Map<String, ISuiteResult> r = suite.getResults();99 for (ISuiteResult r2 : r.values()) {100 ITestContext testContext = r2.getTestContext();101 if (r.values().size() > 0) {102 m_out.println("<h1>" + testContext.getName() + "</h1>");103 }104 resultDetail(testContext.getFailedConfigurations());105 resultDetail(testContext.getFailedTests());106 resultDetail(testContext.getSkippedConfigurations());107 resultDetail(testContext.getSkippedTests());108 resultDetail(testContext.getPassedTests());109 }110 }111 }112 /**113 * @param tests114 */115 private void resultSummary(ISuite suite, IResultMap tests, String testname, String style,116 String details) {117 if (tests.getAllResults().size() > 0) {118 StringBuffer buff = new StringBuffer();119 String lastClassName = "";120 int mq = 0;121 int cq = 0;122 for (ITestNGMethod method : getMethodSet(tests, suite)) {123 m_row += 1;124 m_methodIndex += 1;125 ITestClass testClass = method.getTestClass();126 String className = testClass.getName();127 if (mq == 0) {128 String id = (m_testIndex == null ? null : "t" + Integer.toString(m_testIndex));129 titleRow(testname + " — " + style + details, 5, id);130 m_testIndex = null;131 }132 if (!className.equalsIgnoreCase(lastClassName)) {133 if (mq > 0) {134 cq += 1;135 m_out.print("<tr class=\"" + style136 + (cq % 2 == 0 ? "even" : "odd") + "\">" + "<td");137 if (mq > 1) {138 m_out.print(" rowspan=\"" + mq + "\"");139 }140 m_out.println(">" + lastClassName + "</td>" + buff);141 }142 mq = 0;143 buff.setLength(0);144 lastClassName = className;145 }146 Set<ITestResult> resultSet = tests.getResults(method);147 long end = Long.MIN_VALUE;148 long start = Long.MAX_VALUE;149 for (ITestResult testResult : tests.getResults(method)) {150 if (testResult.getEndMillis() > end) {151 end = testResult.getEndMillis();152 }153 if (testResult.getStartMillis() < start) {154 start = testResult.getStartMillis();155 }156 }157 mq += 1;158 if (mq > 1) {159 buff.append("<tr class=\"" + style + (cq % 2 == 0 ? "odd" : "even")160 + "\">");161 }162 String description = method.getDescription();163 String testInstanceName = resultSet.toArray(new ITestResult[]{})[0].getTestName();164 buff.append("<td><a href=\"#m" + m_methodIndex + "\">"165 + qualifiedName(method)166 + " " + (description != null && description.length() > 0167 ? "(\"" + description + "\")"168 : "")169 + "</a>" + (null == testInstanceName ? "" : "<br>(" + testInstanceName + ")")170 + "</td>"171 + "<td class=\"numi\">" + resultSet.size() + "</td>"172 + "<td>" + start + "</td>"173 + "<td class=\"numi\">" + (end - start) + "</td>"174 + "</tr>");175 }176 if (mq > 0) {177 cq += 1;178 m_out.print("<tr class=\"" + style + (cq % 2 == 0 ? "even" : "odd")179 + "\">" + "<td");180 if (mq > 1) {181 m_out.print(" rowspan=\"" + mq + "\"");182 }183 m_out.println(">" + lastClassName + "</td>" + buff);184 }185 }186 }187 /** Starts and defines columns result summary table */188 private void startResultSummaryTable(String style) {189 tableStart(style, "summary");190 m_out.println("<tr><th>Class</th>"191 + "<th>Method</th><th># of<br/>Scenarios</th><th>Start</th><th>Time<br/>(ms)</th></tr>");192 m_row = 0;193 }194 private String qualifiedName(ITestNGMethod method) {195 StringBuilder addon = new StringBuilder();196 String[] groups = method.getGroups();197 int length = groups.length;198 if (length > 0 && !"basic".equalsIgnoreCase(groups[0])) {199 addon.append("(");200 for (int i = 0; i < length; i++) {201 if (i > 0) {202 addon.append(", ");203 }204 addon.append(groups[i]);205 }206 addon.append(")");207 }208 return "<b>" + method.getMethodName() + "</b> " + addon;209 }210 private void resultDetail(IResultMap tests) {211 for (ITestResult result : tests.getAllResults()) {212 ITestNGMethod method = result.getMethod();213 m_methodIndex++;214 String cname = method.getTestClass().getName();215 m_out.println("<h2 id=\"m" + m_methodIndex + "\">" + cname + ":"216 + method.getMethodName() + "</h2>");217 Set<ITestResult> resultSet = tests.getResults(method);218 generateForResult(result, method, resultSet.size());219 m_out.println("<p class=\"totop\"><a href=\"#summary\">back to summary </a></p>");220 }221 }222 private void generateForResult(ITestResult ans, ITestNGMethod method, int resultSetSize) {223 Object[] parameters = ans.getParameters();224 boolean hasParameters = parameters != null && parameters.length > 0;225 if (hasParameters) {226 tableStart("result", null);227 m_out.print("<tr class=\"param\">");228 for (int x = 1; x <= parameters.length; x++) {229 m_out.print("<th>Parameter #" + x + "</th>");230 }231 m_out.println("</tr>");232 m_out.print("<tr class=\"param stripe\">");233 for (Object p : parameters) {234 m_out.println("<td>" + Utils.escapeHtml(Utils.toString(p,null)) + "</td>");235 }236 m_out.println("</tr>");237 }238 List<String> msgs = Reporter.getOutput(ans);239 boolean hasReporterOutput = msgs.size() > 0;240 Throwable exception=ans.getThrowable();241 boolean hasThrowable = exception!=null;242 if (hasReporterOutput||hasThrowable) {243 if (hasParameters) {244 m_out.print("<tr><td");245 if (parameters.length > 1) {246 m_out.print(" colspan=\"" + parameters.length + "\"");247 }248 m_out.println(">");249 }250 else {251 m_out.println("<div>");252 }253 if (hasReporterOutput) {254 if(hasThrowable) {255 m_out.println("<h3>Test Messages</h3>");256 }257 for (String line : msgs) {258 m_out.println(line + "<br/>");259 }260 }261 if(hasThrowable) {262 boolean wantsMinimalOutput = ans.getStatus()==ITestResult.SUCCESS;263 if(hasReporterOutput) {264 m_out.println("<h3>"265 +(wantsMinimalOutput?"Expected Exception":"Failure")266 +"</h3>");267 }268 generateExceptionReport(exception,method);269 }270 if (hasParameters) {271 m_out.println("</td></tr>");272 }273 else {274 m_out.println("</div>");275 }276 }277 if (hasParameters) {278 m_out.println("</table>");279 }280 }281 protected void generateExceptionReport(Throwable exception,ITestNGMethod method) {282 m_out.print("<div class=\"stacktrace\">");283 m_out.print(Utils.stackTrace(exception, true)[0]);284 m_out.println("</div>");285 }286 /**287 * Since the methods will be sorted chronologically, we want to return288 * the ITestNGMethod from the invoked methods.289 */290 private Collection<ITestNGMethod> getMethodSet(IResultMap tests, ISuite suite) {291 List<IInvokedMethod> r = Lists.newArrayList();292 List<IInvokedMethod> invokedMethods = suite.getAllInvokedMethods();293 for (IInvokedMethod im : invokedMethods) {294 if (tests.getAllMethods().contains(im.getTestMethod())) {295 r.add(im);296 }297 }298 Arrays.sort(r.toArray(new IInvokedMethod[r.size()]), new TestSorter());299 List<ITestNGMethod> result = Lists.newArrayList();300 // Add all the invoked methods301 for (IInvokedMethod m : r) {302 result.add(m.getTestMethod());303 }304 // Add all the methods that weren't invoked (e.g. skipped) that we305 // haven't added yet306 for (ITestNGMethod m : tests.getAllMethods()) {307 if (!result.contains(m)) {308 result.add(m);309 }310 }311 return result;312 }313 public void generateSuiteSummaryReport(List<ISuite> suites) {314 tableStart("testOverview", null);315 m_out.print("<tr>");316 tableColumnStart("Test");317 tableColumnStart("Methods<br/>Passed");318 tableColumnStart("# Passed");319 tableColumnStart("# skipped");320 tableColumnStart("# failed");321 tableColumnStart("# Passed<br/>rate");322 tableColumnStart("# skipped<br/>rate");323 tableColumnStart("# failed<br/>rate");324 tableColumnStart("Total<br/>Time");325 tableColumnStart("Included<br/>Groups");326 tableColumnStart("Excluded<br/>Groups");327 m_out.println("</tr>");...
Source:EmailableSummaryReporter.java
...53 L.error("output file", e);54 return;55 }56 startHtml(m_out);57 generateSuiteSummaryReport(suites);58 // generateMethodSummaryReport(suites);59 // generateMethodDetailReport(suites);60 endHtml(m_out);61 m_out.flush();62 m_out.close();63 }64 protected PrintWriter createWriter(String outdir) throws IOException {65 new File(outdir).mkdirs();66 return new PrintWriter(new BufferedWriter(new FileWriter(new File(outdir,67 "emailable-report2.html"))));68 }69 /** Creates a table showing the highlights of each test method with links to the method details */70 protected void generateMethodSummaryReport(List<ISuite> suites) {71 m_methodIndex = 0;72 startResultSummaryTable("methodOverview");73 int testIndex = 1;74 for (ISuite suite : suites) {75 if(suites.size()>1) {76 titleRow(suite.getName(), 5);77 }78 Map<String, ISuiteResult> r = suite.getResults();79 for (ISuiteResult r2 : r.values()) {80 ITestContext testContext = r2.getTestContext();81 String testName = testContext.getName();82 m_testIndex = testIndex;83 resultSummary(suite, testContext.getFailedConfigurations(), testName,84 "failed", " (configuration methods)");85 resultSummary(suite, testContext.getFailedTests(), testName, "failed",86 "");87 resultSummary(suite, testContext.getSkippedConfigurations(), testName,88 "skipped", " (configuration methods)");89 resultSummary(suite, testContext.getSkippedTests(), testName,90 "skipped", "");91 resultSummary(suite, testContext.getPassedTests(), testName, "passed",92 "");93 testIndex++;94 }95 }96 m_out.println("</table>");97 }98 /** Creates a section showing known results for each method */99 protected void generateMethodDetailReport(List<ISuite> suites) {100 m_methodIndex = 0;101 for (ISuite suite : suites) {102 Map<String, ISuiteResult> r = suite.getResults();103 for (ISuiteResult r2 : r.values()) {104 ITestContext testContext = r2.getTestContext();105 if (r.values().size() > 0) {106 m_out.println("<h1>" + testContext.getName() + "</h1>");107 }108 resultDetail(testContext.getFailedConfigurations());109 resultDetail(testContext.getFailedTests());110 resultDetail(testContext.getSkippedConfigurations());111 resultDetail(testContext.getSkippedTests());112 resultDetail(testContext.getPassedTests());113 }114 }115 }116 /**117* @param tests118*/119 private void resultSummary(ISuite suite, IResultMap tests, String testname, String style,120 String details) {121 if (tests.getAllResults().size() > 0) {122 StringBuffer buff = new StringBuffer();123 String lastClassName = "";124 int mq = 0;125 int cq = 0;126 for (ITestNGMethod method : getMethodSet(tests, suite)) {127 m_row += 1;128 m_methodIndex += 1;129 ITestClass testClass = method.getTestClass();130 String className = testClass.getName();131 if (mq == 0) {132 String id = (m_testIndex == null ? null : "t" + Integer.toString(m_testIndex));133 titleRow(testname + " — " + style + details, 5, id);134 m_testIndex = null;135 }136 if (!className.equalsIgnoreCase(lastClassName)) {137 if (mq > 0) {138 cq += 1;139 m_out.print("<tr class=\"" + style140 + (cq % 2 == 0 ? "even" : "odd") + "\">" + "<td");141 if (mq > 1) {142 m_out.print(" rowspan=\"" + mq + "\"");143 }144 m_out.println(">" + lastClassName + "</td>" + buff);145 }146 mq = 0;147 buff.setLength(0);148 lastClassName = className;149 }150 Set<ITestResult> resultSet = tests.getResults(method);151 long end = Long.MIN_VALUE;152 long start = Long.MAX_VALUE;153 for (ITestResult testResult : tests.getResults(method)) {154 if (testResult.getEndMillis() > end) {155 end = testResult.getEndMillis();156 }157 if (testResult.getStartMillis() < start) {158 start = testResult.getStartMillis();159 }160 }161 mq += 1;162 if (mq > 1) {163 buff.append("<tr class=\"" + style + (cq % 2 == 0 ? "odd" : "even")164 + "\">");165 }166 String description = method.getDescription();167 String testInstanceName = resultSet.toArray(new ITestResult[]{})[0].getTestName();168 buff.append("<td><a href=\"#m" + m_methodIndex + "\">"169 + qualifiedName(method)170 + " " + (description != null && description.length() > 0171 ? "(\"" + description + "\")"172 : "")173 + "</a>" + (null == testInstanceName ? "" : "<br>(" + testInstanceName + ")")174 + "</td>"175 + "<td class=\"numi\">" + resultSet.size() + "</td>"176 + "<td>" + start + "</td>"177 + "<td class=\"numi\">" + (end - start) + "</td>"178 + "</tr>");179 }180 if (mq > 0) {181 cq += 1;182 m_out.print("<tr class=\"" + style + (cq % 2 == 0 ? "even" : "odd")183 + "\">" + "<td");184 if (mq > 1) {185 m_out.print(" rowspan=\"" + mq + "\"");186 }187 m_out.println(">" + lastClassName + "</td>" + buff);188 }189 }190 }191 /** Starts and defines columns result summary table */192 private void startResultSummaryTable(String style) {193 tableStart(style, "summary");194 m_out.println("<tr><th>Class</th>"195 + "<th>Method</th><th># of<br/>Scenarios</th><th>Start</th><th>Time<br/>(ms)</th></tr>");196 m_row = 0;197 }198 private String qualifiedName(ITestNGMethod method) {199 StringBuilder addon = new StringBuilder();200 String[] groups = method.getGroups();201 int length = groups.length;202 if (length > 0 && !"basic".equalsIgnoreCase(groups[0])) {203 addon.append("(");204 for (int i = 0; i < length; i++) {205 if (i > 0) {206 addon.append(", ");207 }208 addon.append(groups[i]);209 }210 addon.append(")");211 }212 return "<b>" + method.getMethodName() + "</b> " + addon;213 }214 private void resultDetail(IResultMap tests) {215 for (ITestResult result : tests.getAllResults()) {216 ITestNGMethod method = result.getMethod();217 m_methodIndex++;218 String cname = method.getTestClass().getName();219 m_out.println("<h2 id=\"m" + m_methodIndex + "\">" + cname + ":"220 + method.getMethodName() + "</h2>");221 Set<ITestResult> resultSet = tests.getResults(method);222 generateForResult(result, method, resultSet.size());223 m_out.println("<p class=\"totop\"><a href=\"#summary\">back to summary</a></p>");224 }225 }226 private void generateForResult(ITestResult ans, ITestNGMethod method, int resultSetSize) {227 Object[] parameters = ans.getParameters();228 boolean hasParameters = parameters != null && parameters.length > 0;229 if (hasParameters) {230 tableStart("result", null);231 m_out.print("<tr class=\"param\">");232 for (int x = 1; x <= parameters.length; x++) {233 m_out.print("<th>Parameter #" + x + "</th>");234 }235 m_out.println("</tr>");236 m_out.print("<tr class=\"param stripe\">");237 // for (Object p : parameters) {238 // m_out.println("<td>" + Utils.escapeHtml(Utils.toString(p)) + "</td>");239 // }240 m_out.println("</tr>");241 }242 List<String> msgs = Reporter.getOutput(ans);243 boolean hasReporterOutput = msgs.size() > 0;244 Throwable exception=ans.getThrowable();245 boolean hasThrowable = exception!=null;246 if (hasReporterOutput||hasThrowable) {247 if (hasParameters) {248 m_out.print("<tr><td");249 if (parameters.length > 1) {250 m_out.print(" colspan=\"" + parameters.length + "\"");251 }252 m_out.println(">");253 }254 else {255 m_out.println("<div>");256 }257 if (hasReporterOutput) {258 if(hasThrowable) {259 m_out.println("<h3>Test Messages</h3>");260 }261 for (String line : msgs) {262 m_out.println(line + "<br/>");263 }264 }265 if(hasThrowable) {266 boolean wantsMinimalOutput = ans.getStatus()==ITestResult.SUCCESS;267 if(hasReporterOutput) {268 m_out.println("<h3>"269 +(wantsMinimalOutput?"Expected Exception":"Failure")270 +"</h3>");271 }272 generateExceptionReport(exception,method);273 }274 if (hasParameters) {275 m_out.println("</td></tr>");276 }277 else {278 m_out.println("</div>");279 }280 }281 if (hasParameters) {282 m_out.println("</table>");283 }284 }285 protected void generateExceptionReport(Throwable exception,ITestNGMethod method) {286 m_out.print("<div class=\"stacktrace\">");287 m_out.print(Utils.stackTrace(exception, true)[0]);288 m_out.println("</div>");289 }290 /**291* Since the methods will be sorted chronologically, we want to return292* the ITestNGMethod from the invoked methods.293*/294 private Collection<ITestNGMethod> getMethodSet(IResultMap tests, ISuite suite) {295 List<IInvokedMethod> r = Lists.newArrayList();296 List<IInvokedMethod> invokedMethods = suite.getAllInvokedMethods();297 for (IInvokedMethod im : invokedMethods) {298 if (tests.getAllMethods().contains(im.getTestMethod())) {299 r.add(im);300 }301 }302 Arrays.sort(r.toArray(new IInvokedMethod[r.size()]), new TestSorter());303 List<ITestNGMethod> result = Lists.newArrayList();304 // Add all the invoked methods305 for (IInvokedMethod m : r) {306 result.add(m.getTestMethod());307 }308 // Add all the methods that weren't invoked (e.g. skipped) that we309 // haven't added yet310 for (ITestNGMethod m : tests.getAllMethods()) {311 if (!result.contains(m)) {312 result.add(m);313 }314 }315 return result;316 }317 public void generateSuiteSummaryReport(List<ISuite> suites) {318 tableStart("testOverview", null);319 m_out.print("<tr>");320 tableColumnStart("Test");321 tableColumnStart("Methods<br/>Passed");322 tableColumnStart("Scenarios<br/>Passed");323 tableColumnStart("# skipped");324 tableColumnStart("# failed");325 tableColumnStart("Total<br/>Time");326 tableColumnStart("Included<br/>Groups");327 tableColumnStart("Excluded<br/>Groups");328 m_out.println("</tr>");329 NumberFormat formatter = new DecimalFormat("#,##0.0");330 int qty_tests = 0;331 int qty_pass_m = 0;...
Source:EmailReport.java
...45 L.error("output file", e);46 return;47 }48 startHtml(m_out);49 generateSuiteSummaryReport(suites);50 generateMethodSummaryReport(suites);51 generateMethodDetailReport(suites);52 endHtml(m_out);53 m_out.flush();54 m_out.close();55 }56 protected PrintWriter createWriter(String outdir) throws IOException {57 new File(outdir).mkdirs();58 String jvmArg = System.getProperty(JVM_ARG);59 if (jvmArg != null && !jvmArg.trim().isEmpty()) {60 fileName = jvmArg;61 }62 return new PrintWriter(new BufferedWriter(new FileWriter(new File(outdir, fileName))));63 }64 /**65 * Creates a table showing the highlights of each test method with links to the method details66 */67 protected void generateMethodSummaryReport(List<ISuite> suites) {68 m_methodIndex = 0;69 startResultSummaryTable();70 int testIndex = 1;71 for (ISuite suite : suites) {72 if (suites.size() > 1) {73 titleRow(suite.getName(), 5);74 }75 Map<String, ISuiteResult> r = suite.getResults();76 for (ISuiteResult r2 : r.values()) {77 ITestContext testContext = r2.getTestContext();78 String testName = testContext.getName();79 m_testIndex = testIndex;80 resultSummary(suite, testContext.getFailedConfigurations(), testName,81 "failed", " (configuration methods)");82 resultSummary(suite, testContext.getFailedTests(), testName, "failed",83 "");84 resultSummary(suite, testContext.getSkippedConfigurations(), testName,85 "skipped", " (configuration methods)");86 resultSummary(suite, testContext.getSkippedTests(), testName,87 "skipped", "");88 resultSummary(suite, testContext.getPassedTests(), testName, "passed",89 "");90 testIndex++;91 }92 }93 m_out.println("</table>");94 }95 /**96 * Creates a section showing known results for each method97 */98 protected void generateMethodDetailReport(List<ISuite> suites) {99 m_methodIndex = 0;100 for (ISuite suite : suites) {101 Map<String, ISuiteResult> r = suite.getResults();102 for (ISuiteResult r2 : r.values()) {103 ITestContext testContext = r2.getTestContext();104 if (r.values().size() > 0) {105 m_out.println("<h1>" + testContext.getName() + "</h1>");106 }107 resultDetail(testContext.getFailedConfigurations());108 resultDetail(testContext.getFailedTests());109 resultDetail(testContext.getSkippedConfigurations());110 resultDetail(testContext.getSkippedTests());111 resultDetail(testContext.getPassedTests());112 }113 }114 }115 /**116 * @param tests117 */118 private void resultSummary(ISuite suite, IResultMap tests, String testname, String style,119 String details) {120 if (!tests.getAllResults().isEmpty()) {121 StringBuilder buff = new StringBuilder();122 String lastClassName = "";123 int mq = 0;124 int cq = 0;125 for (ITestNGMethod method : getMethodSet(tests, suite)) {126 m_row += 1;127 m_methodIndex += 1;128 ITestClass testClass = method.getTestClass();129 String className = testClass.getName();130 if (mq == 0) {131 String id = (m_testIndex == null ? null : "t" + Integer.toString(m_testIndex));132 titleRow(testname + " — " + style + details, 5, id);133 m_testIndex = null;134 }135 if (!className.equalsIgnoreCase(lastClassName)) {136 if (mq > 0) {137 cq += 1;138 m_out.print("<tr class=\"" + style139 + (cq % 2 == 0 ? "even" : "odd") + "\">" + "<td");140 if (mq > 1) {141 m_out.print(" rowspan=\"" + mq + "\"");142 }143 m_out.println(">" + lastClassName + "</td>" + buff);144 }145 mq = 0;146 buff.setLength(0);147 lastClassName = className;148 }149 Set<ITestResult> resultSet = tests.getResults(method);150 long end = Long.MIN_VALUE;151 long start = Long.MAX_VALUE;152 for (ITestResult testResult : tests.getResults(method)) {153 if (testResult.getEndMillis() > end) {154 end = testResult.getEndMillis();155 }156 if (testResult.getStartMillis() < start) {157 start = testResult.getStartMillis();158 }159 }160 mq += 1;161 if (mq > 1) {162 buff.append("<tr class=\"").append(style).append(cq % 2 == 0 ? "odd" : "even")163 .append("\">");164 }165 String description = method.getDescription();166 String testInstanceName = resultSet.toArray(new ITestResult[]{})[0].getTestName();167 buff.append("<td><a href=\"#m").append(m_methodIndex).append("\">")168 .append(qualifiedName(method))169 .append(" ");170 if (description != null && !description.isEmpty()) {171 buff.append("(\"").append(description).append("\")");172 }173 buff.append("</a>");174 if (testInstanceName != null) {175 buff.append("<br>(").append(testInstanceName).append(")");176 }177 buff.append("</td>")178 .append("<td class=\"numi\">").append(resultSet.size()).append("</td>")179 .append("<td>").append(start).append("</td>")180 .append("<td class=\"numi\">").append(end - start).append("</td>")181 .append("</tr>");182 }183 if (mq > 0) {184 cq++;185 m_out.print("<tr class=\"" + style + (cq % 2 == 0 ? "even" : "odd")186 + "\">" + "<td");187 if (mq > 1) {188 m_out.print(" rowspan=\"" + mq + "\"");189 }190 m_out.println(">" + lastClassName + "</td>" + buff);191 }192 }193 }194 /**195 * Starts and defines columns result summary table196 */197 private void startResultSummaryTable() {198 tableStart("methodOverview", "summary");199 m_out.println("<tr><th>Class</th>"200 + "<th>Method</th><th># of<br/>Scenarios</th><th>Start</th><th>Time<br/>(ms)</th></tr>");201 m_row = 0;202 }203 private String qualifiedName(ITestNGMethod method) {204 StringBuilder addon = new StringBuilder();205 String[] groups = method.getGroups();206 int length = groups.length;207 if (length > 0 && !"basic".equalsIgnoreCase(groups[0])) {208 addon.append("(");209 for (int i = 0; i < length; i++) {210 if (i > 0) {211 addon.append(", ");212 }213 addon.append(groups[i]);214 }215 addon.append(")");216 }217 return "<b>" + method.getMethodName() + "</b> " + addon;218 }219 private void resultDetail(IResultMap tests) {220 for (ITestResult result : tests.getAllResults()) {221 ITestNGMethod method = result.getMethod();222 m_methodIndex++;223 String cname = method.getTestClass().getName();224 m_out.println("<h2 id=\"m" + m_methodIndex + "\">" + cname + ":"225 + method.getMethodName() + "</h2>");226 generateForResult(result, method);227 m_out.println("<p class=\"totop\"><a href=\"#summary\">back to summary</a></p>");228 }229 }230 private void generateForResult(ITestResult ans, ITestNGMethod method) {231 Object[] parameters = ans.getParameters();232 boolean hasParameters = parameters != null && parameters.length > 0;233 if (hasParameters) {234 tableStart("result", null);235 m_out.print("<tr class=\"param\">");236 for (int x = 1; x <= parameters.length; x++) {237 m_out.print("<th>Parameter #" + x + "</th>");238 }239 m_out.println("</tr>");240 m_out.print("<tr class=\"param stripe\">");241 for (Object p : parameters) {242 m_out.println("<td>" + Utils.escapeHtml(Utils.toString(p)) + "</td>");243 }244 m_out.println("</tr>");245 }246 List<String> msgs = Reporter.getOutput(ans);247 boolean hasReporterOutput = !msgs.isEmpty();248 Throwable exception = ans.getThrowable();249 boolean hasThrowable = exception != null;250 if (hasReporterOutput || hasThrowable) {251 if (hasParameters) {252 m_out.print("<tr><td");253 if (parameters.length > 1) {254 m_out.print(" colspan=\"" + parameters.length + "\"");255 }256 m_out.println(">");257 } else {258 m_out.println("<div>");259 }260 if (hasReporterOutput) {261 if (hasThrowable) {262 m_out.println("<h3>Test Messages</h3>");263 }264 for (String line : msgs) {265 m_out.println(line + "<br/>");266 }267 }268 if (hasThrowable) {269 boolean wantsMinimalOutput = ans.getStatus() == ITestResult.SUCCESS;270 if (hasReporterOutput) {271 m_out.println("<h3>"272 + (wantsMinimalOutput ? "Expected Exception" : "Failure")273 + "</h3>");274 }275 generateExceptionReport(exception, method);276 }277 if (hasParameters) {278 m_out.println("</td></tr>");279 } else {280 m_out.println("</div>");281 }282 }283 if (hasParameters) {284 m_out.println("</table>");285 }286 }287 protected void generateExceptionReport(Throwable exception, ITestNGMethod method) {288 m_out.print("<div class=\"stacktrace\">");289 m_out.print(Utils.shortStackTrace(exception, true));290 m_out.println("</div>");291 }292 /**293 * Since the methods will be sorted chronologically, we want to return294 * the ITestNGMethod from the invoked methods.295 */296 private Collection<ITestNGMethod> getMethodSet(IResultMap tests, ISuite suite) {297 List<IInvokedMethod> r = Lists.newArrayList();298 List<IInvokedMethod> invokedMethods = suite.getAllInvokedMethods();299 for (IInvokedMethod im : invokedMethods) {300 if (tests.getAllMethods().contains(im.getTestMethod())) {301 r.add(im);302 }303 }304 Arrays.sort(r.toArray(new IInvokedMethod[r.size()]), new EmailReport.TestSorter());305 List<ITestNGMethod> result = Lists.newArrayList();306 // Add all the invoked methods307 for (IInvokedMethod m : r) {308 result.add(m.getTestMethod());309 }310 // Add all the methods that weren't invoked (e.g. skipped) that we311 // haven't added yet312 for (ITestNGMethod m : tests.getAllMethods()) {313 if (!result.contains(m)) {314 result.add(m);315 }316 }317 return result;318 }319 public void generateSuiteSummaryReport(List<ISuite> suites) {320 tableStart("testOverview", null);321 m_out.print("<tr>");322 tableColumnStart("Test");323 tableColumnStart("Methods<br/>Passed");324 tableColumnStart("Scenarios<br/>Passed");325 tableColumnStart("# skipped");326 tableColumnStart("# failed");327 tableColumnStart("Total<br/>Time");328 tableColumnStart("Included<br/>Groups");329 tableColumnStart("Excluded<br/>Groups");330 m_out.println("</tr>");331 NumberFormat formatter = new DecimalFormat("#,##0.0");332 int qty_tests = 0;333 int qty_pass_m = 0;...
generateSuiteSummaryReport
Using AI Code Generation
1import org.testng.reporters.EmailableReporter;2import org.testng.reporters.SuiteHTMLReporter;3import org.testng.xml.XmlSuite;4import java.io.File;5import java.io.IOException;6import java.util.ArrayList;7import java.util.List;8public class GenerateSuiteSummaryReport {9 public static void main(String[] args) throws IOException {10 XmlSuite suite = new XmlSuite();11 suite.setName("TestNG Suite");12 List<String> files = new ArrayList<String>();13 files.add("path/to/testng-results.xml");14 suite.setSuiteFiles(files);15 SuiteHTMLReporter reporter = new SuiteHTMLReporter(suite);16 reporter.generateSuiteSummaryReport(files, new File("path/to/testng-results.html"));17 }18}
generateSuiteSummaryReport
Using AI Code Generation
1import org.testng.Reporter;2import org.testng.TestNG;3import org.testng.reporters.EmailableReporter;4import java.util.List;5import java.util.ArrayList;6public class TestNGEmailableReport {7 public static void main(String[] args) {8 TestNG testNG = new TestNG();9 testNG.setOutputDirectory("test-output");10 List<String> suites = new ArrayList<String>();11 suites.add("testng.xml");12 testNG.setTestSuites(suites);13 testNG.run();14 EmailableReporter.generateSuiteSummaryReport();15 }16}
generateSuiteSummaryReport
Using AI Code Generation
1import org.testng.reporters.EmailableReporter;2import org.testng.xml.XmlSuite;3import java.io.File;4import java.util.List;5import java.util.Map;6import java.util.HashMap;7import java.util.ArrayList;8import java.util.Collection;9import java.util.Iterator;10import java.util.Set;11import java.util.HashSet;12import java.io.IOException;13import java.io.PrintWriter;14import java.io.StringWriter;15import java.io.FileWriter;16import java.io.BufferedWriter;17import java.io.BufferedReader;18import java.io.FileReader;19import java.io.InputStreamReader;20import java.io.FileInputStream;21import java.io.FileOutputStream;22import java.io.OutputStreamWriter;23import java.io.Writer;24import java.io.OutputStream;25import java.io.InputStream;26import java.io.InputStreamReader;27import java.io.BufferedInputStream;28import java.io.BufferedOutputStream;29import java.util.zip.ZipEntry;30import java.util.zip.ZipOutputStream;31import org.apache.commons.io.IOUtils;32import org.apache.commons.io.FileUtils;33import org.apache.commons.io.FilenameUtils;34import org.apache.commons.io.filefilter.TrueFileFilter;35import org.apache.commons.io.filefilter.SuffixFileFilter;36import org.apache.commons.io.filefilter.WildcardFileFilter;37import org.apache.commons.io.filefilter.IOFileFilter;38import org.apache.commons.io.filefilter.DirectoryFileFilter;39import org.apache.commons.io.filefilter.FileFilterUtils;40import org.apache.commons.io.filefilter.EmptyFileFilter;41import org.apache.commons.io.filefilter.AndFileFilter;42import org.apache.commons.io.filefilter.NotFileFilter;43import org.apache.commons.io.filefilter.OrFileFilter;44import org.apache.commons.io.filefilter.PrefixFileFilter;45import org.apache.commons.io.filefilter.SuffixFileFilter;46import org.apache.commons.io.filefilter.NameFileFilter;47import org.apache.commons.io.filefilter.RegexFileFilter;48import org.apache.commons.io.filefilter.AgeFileFilter;49import org.apache.commons.io.filefilter.SizeFileFilter;50import org.apache.commons.io.filefilter.CanReadFileFilter;51import org.apache.commons.io.filefilter.CanWriteFileFilter;52import org.apache.commons.io.filefilter.CanExecuteFileFilter;53import org.apache.commons.io.filefilter.HiddenFileFilter;54import org.apache.commons.io.filefilter.TrueFileFilter;55import org.apache.commons.io.filefilter.FalseFileFilter;56import java.text.SimpleDateFormat;57import java.util.Date;58import java.util.Locale;59import java.util.TimeZone;60import java.util.Calendar;61import java.util.GregorianCalendar;62import java.util
generateSuiteSummaryReport
Using AI Code Generation
1public class TestNGReportGenerator {2 public static void main(String[] args) throws Exception {3 String outputDirectory = "C:\\Users\\User\\Desktop\\TestNGReports";4 String reportName = "TestNGReport.html";5 String suiteName = "TestNGReportSuite";6 String[] suiteFiles = {"C:\\Users\\User\\Desktop\\testng.xml"};7 generateSuiteSummaryReport(suiteName, suiteFiles, outputDirectory, reportName);8 }9}
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!