Best Testng code snippet using org.testng.TestRunner.getFailedButWithinSuccessPercentageTests
Source:TestHTMLReporter.java
...49 getConfigurationSkips(),50 getPassedTests(),51 getFailedTests(),52 getSkippedTests(),53 getFailedButWithinSuccessPercentageTests());54 }55 //56 // implements ITestListener57 /////5859 private static String getOutputFile(ITestContext context) {60 return context.getName() + ".html";61 }6263 public static void generateTable(PrintWriter pw, String title,64 Collection<ITestResult> tests, String cssClass, Comparator<ITestResult> comparator)65 {66 pw.append("<table width='100%' border='1' class='invocation-").append(cssClass).append("'>\n")67 .append("<tr><td colspan='4' align='center'><b>").append(title).append("</b></td></tr>\n")68 .append("<tr>")69 .append("<td><b>Test method</b></td>\n")70 .append("<td width=\"30%\"><b>Exception</b></td>\n")71 .append("<td width=\"10%\"><b>Time (seconds)</b></td>\n")72 .append("<td><b>Instance</b></td>\n")73 .append("</tr>\n");7475 if (tests instanceof List) {76 Collections.sort((List<ITestResult>) tests, comparator);77 }7879 // User output?80 String id;81 Throwable tw;8283 for (ITestResult tr : tests) {84 pw.append("<tr>\n");8586 // Test method87 ITestNGMethod method = tr.getMethod();8889 String name = method.getMethodName();90 pw.append("<td title='").append(tr.getTestClass().getName()).append(".")91 .append(name)92 .append("()'>")93 .append("<b>").append(name).append("</b>");9495 // Test class96 String testClass = tr.getTestClass().getName();97 if (testClass != null) {98 pw.append("<br>").append("Test class: ").append(testClass);99100 // Test name101 String testName = tr.getTestName();102 if (testName != null) {103 pw.append(" (").append(testName).append(")");104 }105 }106107 // Method description108 if (! Utils.isStringEmpty(method.getDescription())) {109 pw.append("<br>").append("Test method: ").append(method.getDescription());110 }111112 Object[] parameters = tr.getParameters();113 if (parameters != null && parameters.length > 0) {114 pw.append("<br>Parameters: ");115 for (int j = 0; j < parameters.length; j++) {116 if (j > 0) {117 pw.append(", ");118 }119 pw.append(parameters[j] == null ? "null" : parameters[j].toString());120 }121 }122123 //124 // Output from the method, created by the user calling Reporter.log()125 //126 {127 List<String> output = Reporter.getOutput(tr);128 if (null != output && output.size() > 0) {129 pw.append("<br/>");130 // Method name131 String divId = "Output-" + tr.hashCode();132 pw.append("\n<a href=\"#").append(divId).append("\"")133 .append(" onClick='toggleBox(\"").append(divId).append("\", this, \"Show output\", \"Hide output\");'>")134 .append("Show output</a>\n")135 .append("\n<a href=\"#").append(divId).append("\"")136 .append(" onClick=\"toggleAllBoxes();\">Show all outputs</a>\n")137 ;138139 // Method output140 pw.append("<div class='log' id=\"").append(divId).append("\">\n");141 for (String s : output) {142 pw.append(s).append("<br/>\n");143 }144 pw.append("</div>\n");145 }146 }147148 pw.append("</td>\n");149150151 // Exception152 tw = tr.getThrowable();153 String stackTrace;154 String fullStackTrace;155156 id = "stack-trace" + tr.hashCode();157 pw.append("<td>");158159 if (null != tw) {160 String[] stackTraces = Utils.stackTrace(tw, true);161 fullStackTrace = stackTraces[1];162 stackTrace = "<div><pre>" + stackTraces[0] + "</pre></div>";163164 pw.append(stackTrace);165 // JavaScript link166 pw.append("<a href='#' onClick='toggleBox(\"")167 .append(id).append("\", this, \"Click to show all stack frames\", \"Click to hide stack frames\")'>")168 .append("Click to show all stack frames").append("</a>\n")169 .append("<div class='stack-trace' id='").append(id).append("'>")170 .append("<pre>").append(fullStackTrace).append("</pre>")171 .append("</div>")172 ;173 }174175 pw.append("</td>\n");176177 // Time178 long time = (tr.getEndMillis() - tr.getStartMillis()) / 1000;179 String strTime = Long.toString(time);180 pw.append("<td>").append(strTime).append("</td>\n");181182 // Instance183 Object instance = tr.getInstance();184 pw.append("<td>").append(Objects.toString(instance)).append("</td>");185186 pw.append("</tr>\n");187 }188189 pw.append("</table><p>\n");190191 }192193 private static String arrayToString(String[] array) {194 StringBuilder result = new StringBuilder();195 for (String element : array) {196 result.append(element).append(" ");197 }198199 return result.toString();200 }201202 private static final String HEAD =203 "\n<style type=\"text/css\">\n" +204 ".log { display: none;} \n" +205 ".stack-trace { display: none;} \n" +206 "</style>\n" +207 "<script type=\"text/javascript\">\n" +208 "<!--\n" +209 "function flip(e) {\n" +210 " current = e.style.display;\n" +211 " if (current == 'block') {\n" +212 " e.style.display = 'none';\n" +213 " return 0;\n" +214 " }\n" +215 " else {\n" +216 " e.style.display = 'block';\n" +217 " return 1;\n" +218 " }\n" +219 "}\n" +220 "\n" +221 "function toggleBox(szDivId, elem, msg1, msg2)\n" +222 "{\n" +223 " var res = -1;" +224 " if (document.getElementById) {\n" +225 " res = flip(document.getElementById(szDivId));\n" +226 " }\n" +227 " else if (document.all) {\n" +228 " // this is the way old msie versions work\n" +229 " res = flip(document.all[szDivId]);\n" +230 " }\n" +231 " if(elem) {\n" +232 " if(res == 0) elem.innerHTML = msg1; else elem.innerHTML = msg2;\n" +233 " }\n" +234 "\n" +235 "}\n" +236 "\n" +237 "function toggleAllBoxes() {\n" +238 " if (document.getElementsByTagName) {\n" +239 " d = document.getElementsByTagName('div');\n" +240 " for (i = 0; i < d.length; i++) {\n" +241 " if (d[i].className == 'log') {\n" +242 " flip(d[i]);\n" +243 " }\n" +244 " }\n" +245 " }\n" +246 "}\n" +247 "\n" +248 "// -->\n" +249 "</script>\n" +250 "\n";251252 public static void generateLog(ITestContext testContext,253 String host,254 String outputDirectory,255 Collection<ITestResult> failedConfs,256 Collection<ITestResult> skippedConfs,257 Collection<ITestResult> passedTests,258 Collection<ITestResult> failedTests,259 Collection<ITestResult> skippedTests,260 Collection<ITestResult> percentageTests)261 {262 //try (PrintWriter writer = new PrintWriter(Utils.openWriter(outputDirectory, getOutputFile(testContext)))) {263 try (PrintWriter writer = new PrintWriter(outputDirectory, getOutputFile(testContext))) {264 writer.append("<html>\n<head>\n")265 .append("<title>TestNG: ").append(testContext.getName()).append("</title>\n")266 .append(HtmlHelper.getCssString())267 .append(HEAD)268 .append("</head>\n")269 .append("<body>\n");270271 Date startDate = testContext.getStartDate();272 Date endDate = testContext.getEndDate();273 long duration = (endDate.getTime() - startDate.getTime()) / 1000;274 int passed =275 testContext.getPassedTests().size() +276 testContext.getFailedButWithinSuccessPercentageTests().size();277 int failed = testContext.getFailedTests().size();278 int skipped = testContext.getSkippedTests().size();279 String hostLine = Utils.isStringEmpty(host) ? "" : "<tr><td>Remote host:</td><td>" + host280 + "</td>\n</tr>";281282 writer283 .append("<h2 align='center'>").append(testContext.getName()).append("</h2>")284 .append("<table border='1' align=\"center\">\n")285 .append("<tr>\n")286// .append("<td>Property file:</td><td>").append(m_testRunner.getPropertyFileName()).append("</td>\n")287// .append("</tr><tr>\n")288 .append("<td>Tests passed/Failed/Skipped:</td><td>").append(Integer.toString(passed)).append("/").append(Integer.toString(failed)).append("/").append(Integer.toString(skipped)).append("</td>\n")289 .append("</tr><tr>\n")290 .append("<td>Started on:</td><td>").append(testContext.getStartDate().toString()).append("</td>\n")
...
Source:BaseTest.java
...82 }83 public Map<String, List<ITestResult>> getFailedTests() {84 return getTests(m_failedTests);85 }86 public Map<String, List<ITestResult>> getFailedButWithinSuccessPercentageTests() {87 return getTests(m_failedButWithinSuccessPercentageTests);88 }89 public Map<String, List<ITestResult>> getPassedTests() {90 return getTests(m_passedTests);91 }92 public Map<String, List<ITestResult>> getSkippedTests() {93 return getTests(m_skippedTests);94 }95 public void setSkippedTests(Map m) {96 setTests(m_skippedTests, m);97 }98 public void setPassedTests(Map m) {99 setTests(m_passedTests, m);100 }101 public void setFailedTests(Map m) {102 setTests(m_failedTests, m);103 }104 public void setFailedButWithinSuccessPercentageTests(Map m) {105 setTests(m_failedButWithinSuccessPercentageTests, m);106 }107 protected void run() {108 assert null != getTest() : "Test wasn't set, maybe @Configuration methodSetUp() was never called";109 setPassedTests(new HashMap());110 setFailedTests(new HashMap());111 setSkippedTests(new HashMap());112 setFailedButWithinSuccessPercentageTests(new HashMap());113 m_suite.setVerbose(0);114 SuiteRunner suite = new SuiteRunner(m_suite, 115 m_outputDirectory,116 m_testRunnerFactory,117 new IAnnotationFinder[] {m_javadocAnnotationFinder, m_jdkAnnotationFinder}118 );119 suite.run();120 }121 122 protected void addMethodSelector(String className, int priority) {123 XmlMethodSelector methodSelector = new XmlMethodSelector();124 methodSelector.setName(className);125 methodSelector.setPriority(priority);126 getTest().getMethodSelectors().add(methodSelector);127 }128 129 protected XmlClass addClass(String className) {130 XmlClass result = new XmlClass(className);131 getTest().getXmlClasses().add(result);132 return result;133 }134 135 protected void setBeanShellExpression(String expression) {136 getTest().setBeanShellExpression(expression);137 }138 protected void addPackage(String pkgName, String[] included, String[] excluded) {139 XmlPackage pkg = new XmlPackage();140 pkg.setName(pkgName);141 pkg.getInclude().addAll(Arrays.asList(included));142 pkg.getExclude().addAll(Arrays.asList(excluded));143 getTest().getSuite().getXmlPackages().add(pkg);144 }145 private XmlClass findClass(String className) {146 for (XmlClass cl : getTest().getXmlClasses()) {147 if (cl.getName().equals(className)) {148 return cl;149 }150 }151 152 XmlClass result = addClass(className);153 return result;154 }155 public void addIncludedMethod(String className, String m) {156 XmlClass xmlClass = findClass(className);157 xmlClass.getIncludedMethods().add(m);158 getTest().getXmlClasses().add(xmlClass);159 }160 public void addExcludedMethod(String className, String m) {161 XmlClass xmlClass = findClass(className);162 xmlClass.getExcludedMethods().add(m);163 getTest().getXmlClasses().add(xmlClass);164 }165 public void addIncludedGroup(String g) {166 getTest().getIncludedGroups().add(g);167 }168 public void addExcludedGroup(String g) {169 getTest().getExcludedGroups().add(g);170 }171 public void addMetaGroup(String mg, List<String> l) {172 getTest().getMetaGroups().put(mg, l);173 }174 public void addMetaGroup(String mg, String n) {175 List<String> l = new ArrayList<String>();176 l.add(n);177 addMetaGroup(mg, l);178 }179 public void setParameter(String key, String value) {180 getTest().addParameter(key, value);181 }182 private void setTest(XmlTest test) {183 XmlTest t = m_tests.get(getId());184 if(null == t) {185 m_tests.put(getId(), t);186 }187 }188 @Configuration(beforeTestMethod = true, groups = {"init", "initTest"}) 189 public void methodSetUp() {190 m_javadocAnnotationFinder= new JDK14AnnotationFinder(m_defaultAnnotationTransformer);191 m_suite = new XmlSuite();192 m_suite.setName("Internal suite");193 m_tests.put(getId(), new XmlTest(m_suite));194 getTest().setName("Internal test, failures are expected");195 }196 private Collection computeDifferences(Map m1, Map m2) {197 List result = new ArrayList();198 for(Iterator it = m1.keySet().iterator(); it.hasNext();) {199 Object key = it.next();200 }201 return result;202 }203 private void addTest(Map<String, List<ITestResult>> tests, ITestResult t) {204 List<ITestResult> l = tests.get(t.getName());205 if(null == l) {206 l = new ArrayList<ITestResult>();207 tests.put(t.getName(), l);208 }209 l.add(t);210 }211 public void addPassedTest(ITestResult t) {212 addTest(getPassedTests(), t);213 }214 public void addFailedTest(ITestResult t) {215 addTest(getFailedTests(), t);216 }217 public void addFailedButWithinSuccessPercentageTest(ITestResult t) {218 addTest(getFailedButWithinSuccessPercentageTests(), t);219 }220 public void addSkippedTest(ITestResult t) {221 addTest(getSkippedTests(), t);222 }223 private void ppp(String s) {224 System.out.println("[BaseTest " + getId() + "] " + s);225 }226 protected Long getId() {227 return Thread.currentThread().getId();228 }229 public XmlSuite getSuite() {230 return m_suite;231 }232 /**...
getFailedButWithinSuccessPercentageTests
Using AI Code Generation
1package com.automationrhapsody.testng;2import java.lang.reflect.Method;3import java.util.ArrayList;4import java.util.List;5import java.util.Set;6import org.testng.ITestNGMethod;7import org.testng.TestListenerAdapter;8import org.testng.TestNG;9import org.testng.TestRunner;10import org.testng.annotations.Test;11import org.testng.xml.XmlClass;12import org.testng.xml.XmlSuite;13import org.testng.xml.XmlTest;14public class FailedButWithinSuccessPercentageTest {15 public static void main(String[] args) {16 TestNG testNG = new TestNG();17 testNG.addListener(new TestListenerAdapter());18 XmlSuite suite = new XmlSuite();19 suite.setName("FailedButWithinSuccessPercentageTest");20 suite.setParallel(XmlSuite.ParallelMode.METHODS);21 suite.setThreadCount(2);22 XmlTest test = new XmlTest(suite);23 test.setName("FailedButWithinSuccessPercentageTest");24 List<XmlClass> classes = new ArrayList<>();25 classes.add(new XmlClass(FailedButWithinSuccessPercentageTest.class));26 test.setXmlClasses(classes);27 testNG.setXmlSuites(List.of(suite));28 testNG.run();29 }30 public void test1() {31 System.out.println("test1");32 throw new RuntimeException("test1");33 }34 public void test2() {35 System.out.println("test2");36 throw new RuntimeException("test2");37 }38 public void test3() {39 System.out.println("test3");40 throw new RuntimeException("test3");41 }42 public void test4() {43 System.out.println("test4");44 throw new RuntimeException("test4");45 }46 public void test5() {47 System.out.println("test5");48 throw new RuntimeException("test5");49 }50 public void test6() {51 System.out.println("test6");52 throw new RuntimeException("test6");53 }54 public void test7() {55 System.out.println("test7");56 throw new RuntimeException("test7");57 }58 public void test8() {59 System.out.println("test8");60 throw new RuntimeException("test8");61 }62 public void test9() {63 System.out.println("test9");64 throw new RuntimeException("test9");65 }
getFailedButWithinSuccessPercentageTests
Using AI Code Generation
1package com.automationrhapsody.testng;2import java.util.List;3import java.util.Map;4import java.util.Map.Entry;5import java.util.Set;6import java.util.concurrent.ConcurrentHashMap;7import java.util.stream.Collectors;8import org.testng.ITestContext;9import org.testng.ITestNGMethod;10import org.testng.ITestResult;11import org.testng.TestListenerAdapter;12import org.testng.TestRunner;13import org.testng.collections.Lists;14public class TestListener extends TestListenerAdapter {15 private Map<String, List<ITestResult>> failedTests = new ConcurrentHashMap<>();16 public void onTestFailure(ITestResult result) {17 super.onTestFailure(result);18 String testClassName = getTestClassName(result.getInstanceName());19 List<ITestResult> failedTestList = failedTests.get(testClassName);20 if (failedTestList == null) {21 failedTestList = Lists.newArrayList();22 failedTests.put(testClassName, failedTestList);23 }24 failedTestList.add(result);25 }26 public void onFinish(ITestContext testContext) {27 super.onFinish(testContext);28 Map<String, ITestNGMethod> allTestMethods = testContext.getAllTestMethods();29 Set<Entry<String, List<ITestResult>>> entrySet = failedTests.entrySet();30 for (Entry<String, List<ITestResult>> entry : entrySet) {31 String testClassName = entry.getKey();32 List<ITestResult> failedTestList = entry.getValue();33 ITestNGMethod testNGMethod = allTestMethods.get(testClassName);34 TestRunner testRunner = (TestRunner) testNGMethod.getInstance();35 List<ITestResult> failedButWithinSuccessPercentageTests = testRunner.getFailedButWithinSuccessPercentageTests();36 if (failedButWithinSuccessPercentageTests != null) {37 failedTestList.addAll(failedButWithinSuccessPercentageTests);38 }39 if (!failedTestList.isEmpty()) {40 System.out.println("Failed tests for " + testClassName + ":");41 for (ITestResult failedTest : failedTestList) {42 System.out.println(failedTest.getName());43 }44 }45 }46 }47 private String getTestClassName(String instanceName) {48 String[] split = instanceName.split("\\.");49 return split[split.length - 1];50 }51}52package com.automationrhapsody.testng;53import org.testng.annotations.Test
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!!