Source:Testng terminal report
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
Best Testng code snippet using org.testng.reporters.TextReporter
Source:TextReporter.java
...11 *12 * @author <a href="mailto:cedric@beust.com">Cedric Beust</a>13 * @author <a href='mailto:the_mindstorm@evolva.ro'>Alexandru Popescu</a>14 */15public class TextReporter extends TestListenerAdapter {16 private int m_verbose = 0;17 private String m_testName = null;18 public TextReporter(String testName, int verbose) {19 m_testName = testName;20 m_verbose = verbose;21 }22 @Override23 public void onFinish(ITestContext context) {24 if (m_verbose >= 2) {25 logResults();26 }27 }28 private ITestNGMethod[] resultsToMethods(List<ITestResult> results) {29 ITestNGMethod[] result = new ITestNGMethod[results.size()];30 int i = 0;31 for (ITestResult tr : results) {32 result[i++] = tr.getMethod();33 }34 return result;35 }36 private void logResults() {37 //38 // Log Text39 //40 for(Object o : getConfigurationFailures()) {41 ITestResult tr = (ITestResult) o;42 Throwable ex = tr.getThrowable();43 String stackTrace= "";44 if (ex != null) {45 if (m_verbose >= 2) {46 stackTrace= Utils.stackTrace(ex, false)[0];47 }48 }49 logResult("FAILED CONFIGURATION",50 Utils.detailedMethodName(tr.getMethod(), false),51 tr.getMethod().getDescription(),52 stackTrace,53 tr.getParameters(),54 tr.getMethod().getMethod().getParameterTypes()55 );56 }57 for(Object o : getConfigurationSkips()) {58 ITestResult tr = (ITestResult) o;59 logResult("SKIPPED CONFIGURATION",60 Utils.detailedMethodName(tr.getMethod(), false),61 tr.getMethod().getDescription(),62 null,63 tr.getParameters(),64 tr.getMethod().getMethod().getParameterTypes()65 );66 }67 for(Object o : getPassedTests()) {68 ITestResult tr = (ITestResult) o;69 logResult("PASSED", tr, null);70 }71 for(Object o : getFailedTests()) {72 ITestResult tr = (ITestResult) o;73 Throwable ex = tr.getThrowable();74 String stackTrace= "";75 if (ex != null) {76 if (m_verbose >= 2) {77 stackTrace= Utils.stackTrace(ex, false)[0];78 }79 }80 logResult("FAILED", tr, stackTrace);81 }82 for(Object o : getSkippedTests()) {83 ITestResult tr = (ITestResult) o;84 Throwable throwable = tr.getThrowable();85 logResult("SKIPPED", tr, throwable != null ? Utils.stackTrace(throwable, false)[0] : null);86 }87 ITestNGMethod[] ft = resultsToMethods(getFailedTests());88 StringBuffer logBuf= new StringBuffer("\n===============================================\n");89 logBuf.append(" ").append(m_testName).append("\n");90 logBuf.append(" Tests run: ").append(Utils.calculateInvokedMethodCount(getAllTestMethods()))91 .append(", Failures: ").append(Utils.calculateInvokedMethodCount(ft))92 .append(", Skips: ").append(Utils.calculateInvokedMethodCount(resultsToMethods(getSkippedTests())));93 int confFailures= getConfigurationFailures().size();94 int confSkips= getConfigurationSkips().size();95 if(confFailures > 0 || confSkips > 0) {96 logBuf.append("\n").append(" Configuration Failures: ").append(confFailures)97 .append(", Skips: ").append(confSkips);98 }99 logBuf.append("\n===============================================\n");100 logResult("", logBuf.toString());101 }102 private String getName() {103 return m_testName;104 }105 private void logResult(String status, ITestResult tr, String stackTrace) {106 logResult(status, tr.getName(), tr.getMethod().getDescription(), stackTrace,107 tr.getParameters(), tr.getMethod().getMethod().getParameterTypes());108 }109 private void logResult(String status, String message) {110 StringBuffer buf= new StringBuffer();111 if(isStringNotBlank(status)) {112 buf.append(status).append(": ");113 }114 buf.append(message);115 System.out.println(buf);116 }117 private void logResult(String status, String name,118 String description, String stackTrace,119 Object[] params, Class[] paramTypes) {120 StringBuffer msg= new StringBuffer(name);121 if(null != params && params.length > 0) {122 msg.append("(");123 // The error might be a data provider parameter mismatch, so make124 // a special case here125 if (params.length != paramTypes.length) {126 msg.append(name + ": Wrong number of arguments were passed by " +127 "the Data Provider: found " + params.length + " but " +128 "expected " + paramTypes.length129 + ")");130 }131 else {132 for(int i= 0; i < params.length; i++) {133 if(i > 0) {134 msg.append(", ");135 }136 msg.append(Utils.toString(params[i], paramTypes[i]));137 }138 msg.append(")");139 }140 }141 if (! Utils.isStringEmpty(description)) {142 msg.append("\n");143 for (int i = 0; i < status.length() + 2; i++) {144 msg.append(" ");145 }146 msg.append(description);147 }148 if ( ! Utils.isStringEmpty(stackTrace)) {149 msg.append("\n").append(stackTrace);150 }151 logResult(status, msg.toString());152 }153 public void ppp(String s) {154 System.out.println("[TextReporter " + getName() + "] " + s);155 }156}...
Source:GATKTextReporter.java
...22* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR23* THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*/25package org.broadinstitute.gatk.utils;26import org.testng.reporters.TextReporter;27/**28 * HACK: Create a variant of the TestNG TextReporter that can be run with no29 * arguments, and can therefore be added to the TestNG listener list.30 *31 * @author hanna32 * @version 0.133 */34public class GATKTextReporter extends TextReporter {35 public GATKTextReporter() {36 super("GATK test suite",2);37 }38}...
Source:TestNGMainRunner.java
...9 org.testng.TestNG testng = new org.testng.TestNG(false);10 testng.setTestClasses(new Class<?>[] {testClass});11 testng.setVerbose(verbose);12 // Deprecated API used because it works on Android unlike the recommended one.13 testng.addListener(new org.testng.reporters.TextReporter(testClass.getName(), verbose));14 try {15 testng.run();16 System.out.print("Tests result in " + testClass.getName() + ": ");17 if (testng.hasFailure()) {18 System.out.println("FAILURE");19 } else {20 System.out.println("SUCCESS");21 }22 } catch (RuntimeException | Error e) {23 System.out.print("Tests result in " + testClass.getName() + ": ");24 System.out.println("ERROR");25 e.printStackTrace();26 }27 }...
Source:TestNGTest1.java
...5import org.testng.TestNG;6import org.testng.annotations.AfterMethod;7import org.testng.annotations.BeforeMethod;8import org.testng.annotations.Test;9import org.testng.reporters.TextReporter;10import org.unitils.UnitilsTestNG;11public class TestNGTest1 extends UnitilsTestNG {12 @BeforeMethod13 public void before(Method m) {14 System.out.println("BeforeMethod " + m.getName());15 }16 17 @Test public void test() {18 System.out.println("executing");19 Assert.assertEquals("test", "test");20 }21 22 @Test public void test1() {23 System.out.println("executing");24 Assert.assertEquals("test", "test1");25 }26 27 @AfterMethod28 public void after(Method m) {29 System.out.println("AfterMethod " + m.getName());30 }31 32 public static void main(String[] args) {33 TestListenerAdapter tla = new TextReporter("test", 3);34 TestNG testng = new TestNG();35 testng.setTestClasses(new Class[] { TestNGTest.class});36 testng.addListener(tla);37 testng.run();38 }39}...
Source:TestNGTest.java
...5import org.testng.TestNG;6import org.testng.annotations.AfterMethod;7import org.testng.annotations.BeforeMethod;8import org.testng.annotations.Test;9import org.testng.reporters.TextReporter;10import org.unitils.UnitilsTestNG;11public class TestNGTest extends UnitilsTestNG {12 @BeforeMethod13 public void before(Method m) {14 System.out.println("BeforeMethod " + m.getName());15 }16 17 @Test public void test() {18 System.out.println("executing");19 Assert.assertEquals("test", "test");20 }21 22 @Test public void test1() {23 System.out.println("executing");24 Assert.assertEquals("test", "test1");25 }26 27 @AfterMethod28 public void after(Method m) {29 System.out.println("AfterMethod " + m.getName());30 }31 32 public static void main(String[] args) {33 TestListenerAdapter tla = new TextReporter("test", 3);34 TestNG testng = new TestNG();35 testng.setTestClasses(new Class[] { TestNGTest.class});36 testng.addListener(tla);37 testng.run();38 }39}...
Source:StingTextReporter.java
1package org.broadinstitute.sting;2import org.testng.reporters.TextReporter;3/**4 * HACK: Create a variant of the TestNG TextReporter that can be run with no5 * arguments, and can therefore be added to the TestNG listener list.6 *7 * @author hanna8 * @version 0.19 */10public class StingTextReporter extends TextReporter {11 public StingTextReporter() {12 super("Ant suite",2);13 }14}...
TextReporter
Using AI Code Generation
1import org.testng.reporters.TextReporter;2import org.testng.reporters.XMLReporter;3import org.testng.TestNG;4TestNG testNG = new TestNG();5testNG.setOutputDirectory("test-output");6testNG.addListener(new TextReporter());7testNG.addListener(new XMLReporter());8testNG.setTestSuites(Arrays.asList("testng.xml"));9testNG.run();
TextReporter
Using AI Code Generation
1package org.testng.reporters;2import org.testng.ITestResult;3import org.testng.Reporter;4import org.testng.TestListenerAdapter;5public class TextReporter extends TestListenerAdapter {6 public void onTestStart(ITestResult tr) {7 Reporter.log("onTestStart: " + tr.getName());8 }9 public void onTestSuccess(ITestResult tr) {10 Reporter.log("onTestSuccess: " + tr.getName());11 }12 public void onTestFailure(ITestResult tr) {13 Reporter.log("onTestFailure: " + tr.getName());14 }15 public void onTestSkipped(ITestResult tr) {16 Reporter.log("onTestSkipped: " + tr.getName());17 }18 public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {19 Reporter.log("onTestFailedButWithinSuccessPercentage: " + tr.getName());20 }21 public void onStart(ITestContext testContext) {22 Reporter.log("onStart: " + testContext.getName());23 }24 public void onFinish(ITestContext testContext) {25 Reporter.log("onFinish: " + testContext.getName());26 }27}28package org.testng.reporters;29import org.testng.annotations.Test;30public class TextReporterTest {31 public void test1() {32 System.out.println("test1");33 }34 public void test2() {35 System.out.println("test2");36 }37}38package org.testng.reporters;39import org.testng.annotations.Test;40public class TextReporterTest {41 public void test1() {42 System.out.println("test1");43 }
TextReporter
Using AI Code Generation
1import org.testng.Reporter;2import org.testng.annotations.Test;3import org.testng.reporters.TextReporter;4public class TextReporterTest {5 public void test() {6 Reporter.log("This is a test", true);7 TextReporter reporter = new TextReporter();8 reporter.generateReport(null, null, null, null, null);9 }10}
TextReporter
Using AI Code Generation
1 at java.net.URLClassLoader$1.run(URLClassLoader.java:366)2 at java.net.URLClassLoader$1.run(URLClassLoader.java:355)3 at java.security.AccessController.doPrivileged(Native Method)4 at java.net.URLClassLoader.findClass(URLClassLoader.java:354)5 at java.lang.ClassLoader.loadClass(ClassLoader.java:425)6 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)7 at java.lang.ClassLoader.loadClass(ClassLoader.java:358)8 at org.testng.internal.ClassHelper.forName(ClassHelper.java:23)9 at org.testng.internal.ClassHelper.forName(ClassHelper.java:14)10 at org.testng.TestNG.createReporter(TestNG.java:1008)11 at org.testng.TestNG.addListener(TestNG.java:1018)12 at org.testng.TestNG.addListener(TestNG.java:1001)13 at org.testng.TestNG.addListener(TestNG.java:995)14 at org.testng.TestNG.addListener(TestNG.java:990)15 at org.testng.TestNG.run(TestNG.java:1011)16 at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:283)17 at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:84)18 at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:90)19 at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)20 at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)21 at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)22 at java.net.URLClassLoader$1.run(URLClassLoader.java:366)23 at java.net.URLClassLoader$1.run(URLClassLoader.java:355)24 at java.security.AccessController.doPrivileged(Native Method)25 at java.net.URLClassLoader.findClass(URLClassLoader.java:354)26 at java.lang.ClassLoader.loadClass(ClassLoader.java:425)27 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308
Source: Testng terminal report
1@Configuration2@EnableWebSocketMessageBroker3public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {4
1import io.netty.channel.EventLoopGroup;2import org.springframework.messaging.Message;3import org.springframework.messaging.simp.stomp.Reactor2StompCodec;4import org.springframework.messaging.simp.stomp.StompDecoder;5import org.springframework.messaging.simp.stomp.StompEncoder;6import org.springframework.messaging.tcp.reactor.Reactor2TcpClient;7import reactor.Environment;8import reactor.core.config.ReactorConfiguration;9import reactor.io.net.NetStreams;10import reactor.io.net.Spec;11import reactor.io.net.config.SslOptions;12import reactor.io.net.impl.netty.NettyClientSocketOptions;1314public class StompTcpFactory implements NetStreams.TcpClientFactory<Message<byte[]>, Message<byte[]>> {1516 private final Environment environment;17 private final EventLoopGroup eventLoopGroup;18 private final String host;19 private final int port;20 private final boolean ssl;2122 public StompTcpFactory(String host, int port, boolean ssl) {23 this.host = host;24 this.port = port;25 this.ssl = ssl;26 this.environment = new Environment(() -> new ReactorConfiguration(emptyList(), "sync", new Properties()));27 this.eventLoopGroup = Reactor2TcpClient.initEventLoopGroup();28 }2930 @Override31 public Spec.TcpClientSpec<Message<byte[]>, Message<byte[]>> apply(Spec.TcpClientSpec<Message<byte[]>, Message<byte[]>> tcpClientSpec) {32 return tcpClientSpec33 .env(environment)34 .options(new NettyClientSocketOptions().eventLoopGroup(eventLoopGroup))35 .codec(new Reactor2StompCodec(new StompEncoder(), new StompDecoder()))36 .ssl(ssl ? new SslOptions() : null)37 .connect(host, port);38 }3940}41
1import lombok.RequiredArgsConstructor;2import org.springframework.context.annotation.Configuration;3import org.springframework.messaging.simp.config.MessageBrokerRegistry;4import org.springframework.messaging.simp.stomp.StompReactorNettyCodec;5import org.springframework.messaging.tcp.reactor.ReactorNettyTcpClient;6import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;7import org.springframework.web.socket.config.annotation.StompEndpointRegistry;8import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;910@Configuration11@EnableWebSocketMessageBroker12@RequiredArgsConstructor13class WebsocketConfiguration implements WebSocketMessageBrokerConfigurer {1415 private final WebsocketProperties properties;1617 @Override18 public void registerStompEndpoints(StompEndpointRegistry registry) {19 registry.addEndpoint("/ws").setAllowedOrigins("*");20 registry.addEndpoint("/ws").withSockJS();21 }2223 @Override24 public void configureMessageBroker(MessageBrokerRegistry registry) {2526 ReactorNettyTcpClient<byte[]> tcpClient = new ReactorNettyTcpClient<>(configurer -> configurer27 .host(properties.getRelayHost())28 .port(properties.getRelayPort())29 .secure(), new StompReactorNettyCodec());3031 registry.enableStompBrokerRelay("/queue", "/topic")32 .setAutoStartup(true)33 .setSystemLogin(properties.getClientLogin())34 .setSystemPasscode(properties.getClientPasscode())35 .setClientLogin(properties.getClientLogin())36 .setClientPasscode(properties.getClientPasscode())37 .setTcpClient(tcpClient);3839 registry.setApplicationDestinationPrefixes("/app");40 }41}42
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!!