How to use addListener method of org.testng.TestNG class

Best Testng code snippet using org.testng.TestNG.addListener

Source:AnnotationTransformerTest.java Github

copy

Full Screen

...28 public void verifyAnnotationWithoutTransformer() {29 TestNG tng = create(AnnotationTransformerSampleTest.class);30 tng.setPreserveOrder(true);31 TestListenerAdapter tla = new TestListenerAdapter();32 tng.addListener(tla);33 tng.run();34 assertThat(tla.getPassedTests())35 .extracting(NAME_EXTRACTOR)36 .containsExactly(37 "five", "four", "four", "four", "four", "four", "three", "three", "three", "three",38 "three", "two", "two");39 assertThat(tla.getFailedTests()).extracting(NAME_EXTRACTOR).containsExactly("verify");40 }41 /** Test a transformer on a method-level @Test */42 @Test43 public void verifyAnnotationTransformerMethod() {44 TestNG tng = create(AnnotationTransformerSampleTest.class);45 tng.setPreserveOrder(true);46 MyTransformer transformer = new MyTransformer();47 tng.addListener(transformer);48 TestListenerAdapter tla = new TestListenerAdapter();49 tng.addListener(tla);50 tng.run();51 assertThat(transformer.getMethodNames()).contains("two", "three", "four", "five", "verify");52 assertThat(tla.getPassedTests())53 .extracting(NAME_EXTRACTOR)54 .containsExactly(55 "five", "five", "five", "five", "five", "four", "four", "four", "four", "three",56 "three", "three", "two", "two", "verify");57 assertThat(tla.getFailedTests()).isEmpty();58 }59 @Test60 public void verifyAnnotationTransformerHasOnlyOneNonNullArgument() {61 TestNG tng = create(AnnotationTransformerSampleTest.class);62 MyParamTransformer transformer = new MyParamTransformer();63 tng.addListener(transformer);64 tng.run();65 assertThat(transformer.isSuccess()).isTrue();66 }67 @Test68 public void verifyMyParamTransformerOnlyOneNonNull() {69 assertThat(MyParamTransformer.onlyOneNonNull(null, null, null)).isFalse();70 assertThat(71 MyParamTransformer.onlyOneNonNull(72 MyParamTransformer.class, MyParamTransformer.class.getConstructors()[0], null))73 .isFalse();74 assertThat(MyParamTransformer.onlyOneNonNull(MyParamTransformer.class, null, null)).isTrue();75 }76 /**77 * Without an annotation transformer, we should have zero passed tests and one failed test called78 * "one".79 */80 @Test81 public void verifyAnnotationTransformerClass2() {82 runTest(null, null, "one");83 }84 /**85 * With an annotation transformer, we should have one passed test called "one" and zero failed86 * tests.87 */88 @Test89 public void verifyAnnotationTransformerClass() {90 runTest(new MyTimeOutTransformer(), "one", null);91 }92 private void runTest(IAnnotationTransformer transformer, String passedName, String failedName) {93 MySuiteListener.triggered = false;94 MySuiteListener2.triggered = false;95 TestNG tng = new TestNG();96 tng.setVerbose(0);97 if (transformer != null) {98 tng.addListener(transformer);99 }100 tng.setTestClasses(new Class[] {AnnotationTransformerClassSampleTest.class});101 TestListenerAdapter tla = new TestListenerAdapter();102 tng.addListener(tla);103 tng.run();104 List<ITestResult> results = passedName != null ? tla.getPassedTests() : tla.getFailedTests();105 String name = passedName != null ? passedName : failedName;106 Assert.assertEquals(results.size(), 1);107 Assert.assertEquals(name, results.get(0).getMethod().getMethodName());108 Assert.assertTrue(MySuiteListener.triggered);109 Assert.assertFalse(MySuiteListener2.triggered);110 }111 @Test112 public void verifyListenerAnnotationTransformerClass() {113 MySuiteListener.triggered = false;114 MySuiteListener2.triggered = false;115 TestNG tng = new TestNG();116 tng.setVerbose(0);117 tng.addListener(new MyListenerTransformer());118 tng.setTestClasses(new Class[] {AnnotationTransformerClassSampleTest.class});119 tng.run();120 Assert.assertFalse(MySuiteListener.triggered);121 Assert.assertTrue(MySuiteListener2.triggered);122 }123 @Test124 public void verifyConfigurationTransformer() {125 TestNG tng = new TestNG();126 tng.addListener(new ConfigurationTransformer());127 tng.setVerbose(0);128 tng.setTestClasses(new Class[] {ConfigurationSampleTest.class});129 TestListenerAdapter tla = new TestListenerAdapter();130 tng.addListener(tla);131 tng.run();132 Assert.assertEquals(ConfigurationSampleTest.getBefore(), "correct");133 }134 @Test135 public void verifyDataProviderTransformer() {136 TestNG tng = create();137 tng.addListener(new DataProviderTransformer());138 tng.setTestClasses(new Class[] {AnnotationTransformerDataProviderSampleTest.class});139 TestListenerAdapter tla = new TestListenerAdapter();140 tng.addListener(tla);141 tng.run();142 Assert.assertEquals(tla.getPassedTests().size(), 1);143 }144 @Test145 public void verifyFactoryTransformer() {146 TestNG tng = create();147 tng.addListener(new FactoryTransformer());148 tng.setTestClasses(new Class[] {AnnotationTransformerFactorySampleTest.class});149 TestListenerAdapter tla = new TestListenerAdapter();150 tng.addListener(tla);151 tng.run();152 Assert.assertEquals(tla.getPassedTests().size(), 1);153 }154 @Test(description = "Test for issue #605")155 public void verifyInvocationCountTransformer() {156 TestNG tng = create();157 tng.setTestClasses(new Class[] {AnnotationTransformerInvocationCountTest.class});158 TestListenerAdapter tla = new TestListenerAdapter();159 tng.addListener(tla);160 tng.run();161 Assert.assertEquals(tla.getPassedTests().size(), 3);162 tng = create();163 tng.addListener(new AnnotationTransformerInvocationCountTest.InvocationCountTransformer(5));164 tng.setTestClasses(new Class[] {AnnotationTransformerInvocationCountTest.class});165 tla = new TestListenerAdapter();166 tng.addListener(tla);167 tng.run();168 Assert.assertEquals(tla.getPassedTests().size(), 5);169 }170 @Test171 public void annotationTransformerInXmlShouldBeRun() throws Exception {172 String xml =173 "<suite name=\"SingleSuite\" >"174 + " <listeners>"175 + " <listener class-name=\"test.annotationtransformer.AnnotationTransformerInTestngXml\" />"176 + " </listeners>"177 + " <test enabled=\"true\" name=\"SingleTest\">"178 + " <classes>"179 + " <class name=\"test.annotationtransformer.AnnotationTransformerInTestngXml\" />"180 + " </classes>"181 + " </test>"182 + "</suite>";183 ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());184 Collection<XmlSuite> suites = new Parser(is).parse();185 TestNG tng = create();186 tng.setXmlSuites(Arrays.asList(suites.toArray(new XmlSuite[0])));187 TestListenerAdapter tla = new TestListenerAdapter();188 tng.addListener(tla);189 tng.run();190 Assert.assertEquals(tla.getPassedTests().size(), 1);191 }192 @Test(description = "GITHUB-1790")193 public void ensureAnnotationTransformerInvokedForClassLevelTestAnnotations() {194 TestNG testng = create(TestClassSample1.class, TestClassSample2.class);195 TransformerImpl transformer = new TransformerImpl();196 testng.addListener(transformer);197 testng.setVerbose(2);198 testng.run();199 assertThat(transformer.getClasses()).hasSize(2);200 assertThat(transformer.getConstructors()).isEmpty();201 assertThat(transformer.getMethods()).hasSize(2);202 }203}...

Full Screen

Full Screen

Source:ArtifactCollectorTest.java Github

copy

Full Screen

...22 TestListenerAdapter tla = new TestListenerAdapter();23 24 TestNG testNG = new TestNG();25 testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class});26 testNG.addListener((ITestNGListener) lc);27 testNG.addListener((ITestNGListener) tla);28 testNG.setGroups("testPassed");29 testNG.run();30 31 assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count");32 assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count");33 assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count");34 assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count");35 assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count");36 assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count");37 38 ITestResult result = tla.getPassedTests().get(0);39 assertNull(UnitTestArtifact.getCaptureState(result), "Artifact provider capture state should be 'null'");40 assertNull(UnitTestCapture.getArtifactPath(result), "Artifact capture should not have been requested");41 }42 43 @Test44 public void verifyCaptureOnFailure() {45 46 ListenerChain lc = new ListenerChain();47 TestListenerAdapter tla = new TestListenerAdapter();48 49 TestNG testNG = new TestNG();50 testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class});51 testNG.addListener((ITestNGListener) lc);52 testNG.addListener((ITestNGListener) tla);53 testNG.setGroups("testFailed");54 testNG.run();55 56 assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count");57 assertEquals(tla.getFailedTests().size(), 1, "Incorrect failed test count");58 assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count");59 assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count");60 assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count");61 assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count");62 63 ITestResult result = tla.getFailedTests().get(0);64 assertEquals(UnitTestArtifact.getCaptureState(result), CaptureState.CAPTURE_SUCCESS, "Incorrect artifact provider capture state");65 assertTrue(UnitTestCapture.getArtifactPath(result).isPresent(), "Artifact capture output path is not present");66 }67 68 @Test69 public void verifyCanNotCapture() {70 71 ListenerChain lc = new ListenerChain();72 TestListenerAdapter tla = new TestListenerAdapter();73 74 TestNG testNG = new TestNG();75 testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class});76 testNG.addListener((ITestNGListener) lc);77 testNG.addListener((ITestNGListener) tla);78 testNG.setGroups("canNotCapture");79 testNG.run();80 81 assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count");82 assertEquals(tla.getFailedTests().size(), 1, "Incorrect failed test count");83 assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count");84 assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count");85 assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count");86 assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count");87 88 ITestResult result = tla.getFailedTests().get(0);89 assertEquals(UnitTestArtifact.getCaptureState(result), CaptureState.CAN_NOT_CAPTURE, "Incorrect artifact provider capture state");90 assertFalse(UnitTestCapture.getArtifactPath(result).isPresent(), "Artifact capture output path should not be present");91 }92 93 @Test94 public void verifyWillNotCapture() {95 96 ListenerChain lc = new ListenerChain();97 TestListenerAdapter tla = new TestListenerAdapter();98 99 TestNG testNG = new TestNG();100 testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class});101 testNG.addListener((ITestNGListener) lc);102 testNG.addListener((ITestNGListener) tla);103 testNG.setGroups("willNotCapture");104 testNG.run();105 106 assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count");107 assertEquals(tla.getFailedTests().size(), 1, "Incorrect failed test count");108 assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count");109 assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count");110 assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count");111 assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count");112 113 ITestResult result = tla.getFailedTests().get(0);114 assertEquals(UnitTestArtifact.getCaptureState(result), CaptureState.CAPTURE_FAILED, "Incorrect artifact provider capture state");115 assertFalse(UnitTestCapture.getArtifactPath(result).isPresent(), "Artifact capture output path should not be present");116 }117 118 @Test119 public void verifyOnDemandCapture() {120 121 ListenerChain lc = new ListenerChain();122 TestListenerAdapter tla = new TestListenerAdapter();123 124 TestNG testNG = new TestNG();125 testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class});126 testNG.addListener((ITestNGListener) lc);127 testNG.addListener((ITestNGListener) tla);128 testNG.setGroups("onDemandCapture");129 testNG.run();130 131 assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count");132 assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count");133 assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count");134 assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count");135 assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count");136 assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count");137 138 ITestResult result = tla.getPassedTests().get(0);139 assertEquals(UnitTestArtifact.getCaptureState(result), CaptureState.CAPTURE_SUCCESS, "Incorrect artifact provider capture state");140 assertTrue(UnitTestCapture.getArtifactPath(result).isPresent(), "Artifact capture output path is not present");141 } ...

Full Screen

Full Screen

Source:RetryAnalyzerTest.java Github

copy

Full Screen

...31 @Test32 public void testInvocationCounts() {33 TestNG tng = create(InvocationCountTest.class);34 TestListenerAdapter tla = new TestListenerAdapter();35 tng.addListener(new TestResultPruner());36 tng.addListener(tla);37 tng.run();38 assertThat(tla.getFailedTests()).isEmpty();39 List<ITestResult> fsp = tla.getFailedButWithinSuccessPercentageTests();40 assertThat(fsp).hasSize(1);41 assertThat(fsp.get(0).getName()).isEqualTo("failAfterThreeRetries");42 List<ITestResult> skipped = tla.getSkippedTests();43 assertThat(skipped).hasSize(InvocationCountTest.invocations.size() - fsp.size());44 }45 @Test46 public void testIfRetryIsInvokedBeforeListener() {47 TestNG tng = create(TestClassSample.class);48 tng.addListener(new MyListener());49 tng.run();50 assertThat(TestClassSample.messages).containsExactly("afterInvocation", "retry", "afterInvocation");51 }52 @Test(description = "GITHUB-1600")53 public void testIfRetryIsInvokedBeforeListenerButHasToConsiderFailures() {54 TestNG tng = create(Github1600TestSample.class);55 Github1600Listener listener = new Github1600Listener();56 TestListenerAdapter tla = new TestListenerAdapter();57 tng.addListener(tla);58 tng.addListener(listener);59 tng.run();60 assertThat(tla.getFailedTests()).hasSize(1);61 assertThat(tla.getSkippedTests()).hasSize(1);62 }63 @Test(description = "GITHUB-1706", dataProvider = "1706")64 public void testIfRetryIsInvokedWhenTestMethodHas(Class<?> clazz, int size, Map<String, String> parameters) {65 XmlSuite xmlsuite = createXmlSuite("suite");66 XmlTest xmlTest = createXmlTest(xmlsuite, "test", clazz);67 if (!parameters.isEmpty()) {68 xmlTest.setParameters(parameters);69 }70 TestNG tng = create();71 tng.setXmlSuites(Collections.singletonList(xmlsuite));72 InvokedMethodNameListener listener = new InvokedMethodNameListener();73 tng.addListener(listener);74 tng.run();75 assertThat(listener.getSkippedMethodNames().size()).isEqualTo(size);76 }77 @DataProvider(name = "1706")78 public Object[][] getData() {79 return new Object[][]{80 {NativeInjectionSample.class, 2, Maps.newHashMap()},81 {DataDrivenSample.class, 4, Maps.newHashMap()},82 {ParameterInjectionSample.class, 2, constructParameterMap()}83 };84 }85 @Test(description = "GITHUB-1538")86 public void testIfDependentMethodsAreInvokedWhenRetrySucceeds() {87 TestNG testng = create(TestClassSampleWithTestMethodDependencies.class);88 TestListenerAdapter tla = new TestListenerAdapter();89 testng.addListener(tla);90 testng.run();91 assertThat(tla.getPassedTests()92 .stream()93 .map(RetryAnalyzerTest::methodName)94 .collect(Collectors.toList())95 ).containsExactly("a", "b");96 assertThat(tla.getFailedTests()).isEmpty();97 assertThat(tla.getSkippedTests()98 .stream()99 .map(RetryAnalyzerTest::methodName)100 .collect(Collectors.toList())101 ).containsExactly("a");102 }103 @Test(description = "GITHUB-1241")104 public void testToEnsureNewRetryAnalyzerInstanceUsedPerTest() {105 XmlSuite suite = createXmlSuite("Test Suite", "Test One", GitHub1241Sample.class);106 createXmlTest(suite, "Test Two", GitHub1241Sample.class);107 TestNG tng = create(suite);108 InvokedMethodNameListener listener = new InvokedMethodNameListener();109 tng.addListener(listener);110 tng.run();111 assertThat(listener.getInvokedMethodNames()).containsExactly("test1", "test2", "test2", "test1", "test2", "test2");112 }113 @Test(description = "GITHUB-1697")114 public void ensureRetriedMethodsAreDistinguishable() {115 XmlSuite xmlsuite = createXmlSuite("1697_suite");116 createXmlTest(xmlsuite, "1697_test", SampleTestclass.class);117 TestNG testng = create(xmlsuite);118 LocalReporter reporter = new LocalReporter();119 testng.addListener(reporter);120 testng.run();121 runAssertions(reporter.getRetried(), "dataDrivenTest");122 runAssertions(reporter.getSkipped(), "child");123 }124 @Test125 public void ensureRetriedMethodsAreDistinguishableInDataDrivenTests() {126 XmlSuite xmlsuite = createXmlSuite("1697_suite");127 createXmlTest(xmlsuite, "1697_test", DatadrivenSample.class);128 TestNG testng = create(xmlsuite);129 LocalReporter reporter = new LocalReporter();130 testng.addListener(reporter);131 testng.run();132 ITestResult firstResult = runAssertions(reporter.getRetried(), "testMethod");133 assertThat(firstResult.getParameters()).containsAll(Collections.singletonList(1));134 }135 private ITestResult runAssertions(Set<ITestResult> results, String methodName) {136 assertThat(results).hasSize(1);137 ITestResult firstResult = results.iterator().next();138 assertThat(firstResult.getMethod().getMethodName()).isEqualToIgnoringCase(methodName);139 return firstResult;140 }141 private static String methodName(ITestResult result) {142 return result.getMethod().getMethodName();143 }144 private static Map<String, String> constructParameterMap() {...

Full Screen

Full Screen

Source:ExecutionFlowControllerTest.java Github

copy

Full Screen

...21 TestListenerAdapter tla = new TestListenerAdapter();22 23 TestNG testNG = new TestNG();24 testNG.setTestClasses(new Class[]{HappyPathClass.class});25 testNG.addListener((ITestNGListener) efc);26 testNG.addListener((ITestNGListener) tla);27 testNG.run();28 29 assertEquals(tla.getFailedTests().size(), 0, "Unexpected test method failure");30 assertEquals(tla.getConfigurationFailures().size(), 0, "Unexpected configuration method failure");31 32 assertEquals(tla.getPassedTests().size(), 2, "Incorrect passed test count");33 assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count");34 assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count");35 36 assertEquals(HappyPathClass.fromBefore, HappyPathClass.VALUE, "Incorrect [before] value");37 assertEquals(HappyPathClass.fromMethod, HappyPathClass.VALUE, "Incorrect [method] value");38 assertEquals(HappyPathClass.fromAfter, HappyPathClass.VALUE, "Incorrect [after] value");39 40 }41 42 @Test43 public void testSkipFromBefore() {44 45 ExecutionFlowController efc = new ExecutionFlowController();46 TestListenerAdapter tla = new TestListenerAdapter();47 48 TestNG testNG = new TestNG();49 testNG.setTestClasses(new Class[]{SkipFromBefore.class});50 testNG.addListener((ITestNGListener) efc);51 testNG.addListener((ITestNGListener) tla);52 testNG.setConfigFailurePolicy(FailurePolicy.CONTINUE);53 testNG.run();54 55 assertEquals(tla.getFailedTests().size(), 0, "Unexpected test method failure");56 assertEquals(tla.getConfigurationFailures().size(), 0, "Unexpected configuration method failure");57 58 assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count");59 assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count");60 assertEquals(tla.getConfigurationSkips().size(), 1, "Incorrect configuration skip count");61 assertEquals(tla.getSkippedTests().size(), 1, "Incorrect skipped test count");62 ITestResult methodResult = tla.getSkippedTests().get(0);63 assertEquals(methodResult.getName(), "testMethod", "Incorrect skipped test name");64 65 assertEquals(SkipFromBefore.fromBefore, SkipFromBefore.VALUE, "Incorrect [before] value");66 assertEquals(methodResult.getAttribute(SkipFromBefore.ATTRIBUTE), SkipFromBefore.VALUE, "Incorrect [method] value");67 assertEquals(SkipFromBefore.fromAfter, SkipFromBefore.VALUE, "Incorrect [after] value");68 69 }70 71 @Test72 public void testSkipFromMethod() {73 74 ExecutionFlowController efc = new ExecutionFlowController();75 TestListenerAdapter tla = new TestListenerAdapter();76 77 TestNG testNG = new TestNG();78 testNG.setTestClasses(new Class[]{SkipFromMethod.class});79 testNG.addListener((ITestNGListener) efc);80 testNG.addListener((ITestNGListener) tla);81 testNG.run();82 83 assertEquals(tla.getFailedTests().size(), 0, "Unexpected test method failure");84 assertEquals(tla.getConfigurationFailures().size(), 0, "Unexpected configuration method failure");85 86 assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count");87 assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count");88 assertEquals(tla.getSkippedTests().size(), 1, "Incorrect skipped test count");89 assertEquals(tla.getSkippedTests().get(0).getName(), "testMethod", "Incorrect skipped test name");90 91 assertEquals(SkipFromMethod.fromBefore, SkipFromMethod.VALUE, "Incorrect [before] value");92 assertEquals(SkipFromMethod.fromMethod, SkipFromMethod.VALUE, "Incorrect [method] value");93 assertEquals(SkipFromMethod.fromAfter, SkipFromMethod.VALUE, "Incorrect [after] value");94 95 }96 97 @Test98 public void testMethodListenerExtension() {99 100 ExecutionFlowController efc = new ExecutionFlowController();101 TestListenerAdapter tla = new TestListenerAdapter();102 103 TestNG testNG = new TestNG();104 testNG.setTestClasses(new Class[]{MethodListenerExtension.class});105 testNG.addListener((ITestNGListener) efc);106 testNG.addListener((ITestNGListener) tla);107 testNG.run();108 109 assertEquals(tla.getFailedTests().size(), 0, "Unexpected test method failure");110 assertEquals(tla.getConfigurationFailures().size(), 0, "Unexpected configuration method failure");111 112 assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count");113 assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count");114 assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count");115 116 assertTrue(MethodListenerExtension.beforeMethodBefore, "Incorrect [beforeMethod] 'before' value");117 assertTrue(MethodListenerExtension.testMethodBefore, "Incorrect [testMethod] 'before' value");118 assertTrue(MethodListenerExtension.afterMethodBefore, "Incorrect [afterMethod] 'before' value");119 assertTrue(MethodListenerExtension.beforeMethodAfter, "Incorrect [beforeMethod] 'after' value");120 assertTrue(MethodListenerExtension.testMethodAfter, "Incorrect [testMethod] 'after' value"); ...

Full Screen

Full Screen

Source:ParameterTest.java Github

copy

Full Screen

...21 XmlTest test = createXmlTest(suite, "Before with parameter sample", ParameterSample.class);22 test.addParameter("first-name", "Cedric");23 TestNG tng = create(suite);24 InvokedMethodNameListener listener = new InvokedMethodNameListener();25 tng.addListener((ITestNGListener) listener);26 tng.run();27 assertThat(listener.getFailedMethodNames()).isEmpty();28 assertThat(listener.getSkippedMethodNames()).isEmpty();29 assertThat(listener.getSucceedMethodNames()).containsExactly(30 "beforeTest(Cedric)", "testNonExistentParameter(null)",31 "beforeTest(Cedric)", "testSingleString(Cedric)"32 );33 }34 @Test35 public void beforeMethodWithParameters() {36 XmlSuite suite = createXmlSuite("beforeMethodWithParameters");37 XmlTest test = createXmlTest(suite, "Before with parameter sample", BeforeWithParameterSample.class);38 test.addParameter("parameter", "parameter value");39 TestNG tng = create(suite);40 InvokedMethodNameListener listener = new InvokedMethodNameListener();41 tng.addListener((ITestNGListener) listener);42 tng.run();43 assertThat(listener.getFailedMethodNames()).isEmpty();44 assertThat(listener.getSkippedMethodNames()).isEmpty();45 assertThat(listener.getSucceedMethodNames()).containsExactly(46 "beforeMethod(parameter value)", "testExample(abc,def)"47 );48 }49 @Test50 public void enumParameters() {51 XmlSuite suite = createXmlSuite("enumParameters");52 XmlTest test = createXmlTest(suite, "Enum parameter sample", EnumParameterSample.class);53 test.addParameter("parameter", "VALUE_1");54 TestNG tng = create(suite);55 InvokedMethodNameListener listener = new InvokedMethodNameListener();56 tng.addListener((ITestNGListener) listener);57 tng.run();58 assertThat(listener.getFailedMethodNames()).isEmpty();59 assertThat(listener.getSkippedMethodNames()).isEmpty();60 assertThat(listener.getSucceedMethodNames()).containsExactly("testMethod(VALUE_1)");61 }62 @Test(description = "GITHUB-1105: Test skipped instead failed if incorrect enum value")63 public void invalidEnumParameters() {64 XmlSuite suite = createXmlSuite("enumParameters");65 XmlTest test = createXmlTest(suite, "Enum parameter sample", EnumParameterSample.class);66 test.addParameter("parameter", "INVALID_VALUE");67 TestNG tng = create(suite);68 InvokedMethodNameListener listener = new InvokedMethodNameListener();69 tng.addListener((ITestNGListener) listener);70 tng.run();71 assertThat(listener.getSucceedMethodNames()).isEmpty();72 assertThat(listener.getSkippedMethodNames()).isEmpty();73 assertThat(listener.getFailedBeforeInvocationMethodNames()).containsExactly("testMethod");74 Throwable exception = listener.getResult("testMethod").getThrowable();75 assertThat(exception).isInstanceOf(TestNGException.class).hasCauseInstanceOf(IllegalArgumentException.class);76 }77 @Test(description = "GITHUB-1061")78 public void testNativeInjection() {79 TestNG testng = create(Issue1061Sample.class);80 TestListenerAdapter listener = new TestListenerAdapter();81 testng.addListener((ITestNGListener) listener);82 testng.run();83 assertThat(listener.getFailedTests().size()).isEqualTo(2);84 String[] expectedMsgs = new String[]{85 "Method test.parameters.Issue1061Sample.test() didn't finish within the time-out 1000",86 "Method test.parameters.Issue1061Sample.test() didn't finish within the time-out 3000"87 };88 List<String> actualMsgs = Lists.newArrayList();89 for (ITestResult result : listener.getFailedTests()) {90 actualMsgs.add(result.getThrowable().getMessage());91 }92 assertThat(actualMsgs).containsExactlyInAnyOrder(expectedMsgs);93 }94 @Test(description = "GITHUB-1554")95 public void testNativeInjectionAndParamsInjection() {96 XmlSuite suite = createXmlSuite("suite");97 XmlTest test = createXmlTest(suite, "test", Issue1554TestClassSample.class);98 Map<String, String> params = new HashMap<>();99 params.put("browser", "chrome");100 test.setParameters(params);101 TestNG testng = create(suite);102 TestListenerAdapter listener = new TestListenerAdapter();103 testng.addListener((ITestNGListener) listener);104 testng.run();105 assertThat(listener.getPassedTests().isEmpty()).isFalse();106 }107}...

Full Screen

Full Screen

Source:IDEARemoteTestNG.java Github

copy

Full Screen

...71 cause.printStackTrace(System.err);72 }73 }74 private void attachListeners(IDEATestNGRemoteListener listener) {75 addListener((Object)new IDEATestNGSuiteListener(listener));76 addListener((Object)new IDEATestNGTestListener(listener));77 try {78 Class<?> configClass = Class.forName("org.testng.IDEATestNGConfigurationListener");79 Object configurationListener = configClass.getConstructor(new Class[] {IDEATestNGRemoteListener.class}).newInstance(listener);80 addListener((Object)configurationListener);81 Class<?> invokeClass = Class.forName("org.testng.IDEATestNGInvokedMethodListener");82 Object invokedMethodListener = invokeClass.getConstructor(new Class[]{IDEATestNGRemoteListener.class}).newInstance(listener);83 addListener((Object)invokedMethodListener);84 //start with configuration started if invoke method listener was not added, otherwise with85 configClass.getMethod("setIgnoreStarted").invoke(configurationListener);86 }87 catch (Throwable ignored) {}88 }89}...

Full Screen

Full Screen

Source:TestLauncher.java Github

copy

Full Screen

...14* @author netzulo.com15* @since 2016-07-2516* @version 0.5.417* @update FIX 0.5.4_a18* @TODO: change addListener to load ITestNGListener classes instead of ITestListener19*/20public class TestLauncher {21 private TestNG testng;22 private HtmlReporter htmlReporter = new HtmlReporter();23 24 public TestNG getTestng() {25 return testng;26 }27 public TestLauncher(boolean defaultListeners) {28 29 testng = new TestNG(defaultListeners); 30 //testng.setParallel(ParallelMode.TRUE);31 32 if(!defaultListeners){33 testng.addListener(reporter());34 testng.addListener(listener());35 }36 else{37// testng.addListener(new Reporter());38 testng.addListener(new TestListenerAdapter());39 }40 }41 42 public TestLauncher(boolean isCustomReporter,boolean isCustomListener) {43 if(!isCustomReporter && !isCustomListener){44 //ALL its FALSE45 testng = new TestNG(false); 46 }47 else{48 testng = new TestNG(true);49 if(isCustomReporter){50 testng.addListener(reporter());51 }52 else{53 //DEFAULT REPORTERs54 testng.addListener(new Reporter());55 }56 if(isCustomListener){57 testng.addListener(listener());58 }59 else{60 //DEFAULT LISTENERs61 testng.addListener(new TestListenerAdapter());62 }63 }64 }65 66 67 public IReporter reporter(){68 return new IReporter(){69 @Override70 public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { 71 htmlReporter.generateReport(xmlSuites, suites, outputDirectory); 72 } 73 };74 }75 ...

Full Screen

Full Screen

Source:CheTestNG.java Github

copy

Full Screen

...35 }36 }37 private void addCheListeners() {38 CheTestNGListener listener = new CheTestNGListener();39 addListener(new CheSuiteListener(listener));40 addListener(new CheTestListener(listener));41 try {42 Class<?> configurationListenerClass =43 Class.forName("org.testng.listeners.CheConfigurationListener");44 Object confListener =45 configurationListenerClass.getConstructor(CheTestNGListener.class).newInstance(listener);46 addListener(confListener);47 Class<?> methodListenerClass = Class.forName("org.testng.listeners.CheInvokedMethodListener");48 Object methodListener =49 methodListenerClass.getConstructor(CheTestNGListener.class).newInstance(listener);50 addListener(methodListener);51 configurationListenerClass.getMethod("setIgnoreStarted").invoke(confListener);52 } catch (Throwable ignored) {53 }54 }55 private void flatSuites(List<XmlSuite> suites, List<XmlSuite> result) {56 for (XmlSuite suite : suites) {57 result.add(suite);58 flatSuites(suite.getChildSuites(), result);59 }60 }61}...

Full Screen

Full Screen

addListener

Using AI Code Generation

copy

Full Screen

1import org.testng.TestNG;2import java.util.ArrayList;3import java.util.List;4public class TestNGRunner {5 public static void main(String[] args) {6 TestNG testNG = new TestNG();7 List<String> suites = new ArrayList<>();8 suites.add("src/test/resources/testng.xml");9 testNG.setTestSuites(suites);10 testNG.run();11 }12}13import org.testng.TestNG;14import java.util.ArrayList;15import java.util.List;16public class TestNGRunner {17 public static void main(String[] args) {18 TestNG testNG = new TestNG();19 List<String> suites = new ArrayList<>();20 suites.add("src/test/resources/testng.xml");21 testNG.setTestSuites(suites);22 testNG.run();23 }24}25import org.testng.TestNG;26import java.util.ArrayList;27import java.util.List;28public class TestNGRunner {29 public static void main(String[] args) {30 TestNG testNG = new TestNG();31 List<String> suites = new ArrayList<>();32 suites.add("src/test/resources/testng.xml");33 testNG.setTestSuites(suites);34 testNG.run();35 }36}37import org.testng.TestNG;38import java.util.ArrayList;39import java.util.List;40public class TestNGRunner {41 public static void main(String[] args) {42 TestNG testNG = new TestNG();43 List<String> suites = new ArrayList<>();44 suites.add("src/test/resources/testng.xml");45 testNG.setTestSuites(suites);46 testNG.run();47 }48}49import org.testng.TestNG;50import java.util.ArrayList;51import java.util.List;52public class TestNGRunner {53 public static void main(String[] args) {54 TestNG testNG = new TestNG();55 List<String> suites = new ArrayList<>();56 suites.add("src/test/resources/testng.xml");57 testNG.setTestSuites(suites);58 testNG.run();59 }60}

Full Screen

Full Screen

addListener

Using AI Code Generation

copy

Full Screen

1import org.testng.TestNG;2import org.testng.xml.XmlClass;3import org.testng.xml.XmlSuite;4import org.testng.xml.XmlTest;5public class TestNGTest {6 public static void main(String[] args) {7 TestNG testng = new TestNG();8 testng.addListener(new TestNGListener());9 XmlSuite suite = new XmlSuite();10 suite.setName("TestNG Suite");11 XmlTest test = new XmlTest(suite);12 test.setName("TestNG Test");13 XmlClass testClass = new XmlClass("com.test.testng.TestClass");14 test.setXmlClasses(new ArrayList<XmlClass>(Arrays.asList(testClass)));15 testng.setXmlSuites(new ArrayList<XmlSuite>(Arrays.asList(suite)));16 testng.run();17 }18}19import org.testng.ISuite;20import org.testng.ISuiteListener;21public class TestNGListener implements ISuiteListener {22 public void onStart(ISuite suite) {23 System.out.println("onStart: before suite starts");24 }25 public void onFinish(ISuite suite) {26 System.out.println("onFinish: after suite completes");27 }28}29import org.testng.annotations.Test;30public class TestClass {31 public void testMethod1() {32 System.out.println("testMethod1");33 }34 public void testMethod2() {35 System.out.println("testMethod2");36 }37}

Full Screen

Full Screen

addListener

Using AI Code Generation

copy

Full Screen

1package mypackage;2import org.testng.TestNG;3import org.testng.xml.XmlSuite;4import org.testng.xml.XmlTest;5import java.util.ArrayList;6import java.util.List;7public class TestNGListener {8 public static void main(String[] args) {9 TestNG testNG = new TestNG();10 testNG.addListener(new MyITestListener());11 XmlSuite suite = new XmlSuite();12 suite.setName("My Suite");13 XmlTest test = new XmlTest(suite);14 test.setName("My Test");15 List<String> files = new ArrayList<>();16 files.add("testng.xml");17 test.setSuiteFiles(files);18 List<XmlSuite> suites = new ArrayList<>();19 suites.add(suite);20 testNG.setXmlSuites(suites);21 testNG.run();22 }23}24package mypackage;25import org.testng.ITestContext;26import org.testng.ITestListener;27import org.testng.ITestResult;28import org.testng.Reporter;29public class MyITestListener implements ITestListener {30 public void onTestStart(ITestResult result) {31 Reporter.log("onTestStart: " + result.getName());32 }33 public void onTestSuccess(ITestResult result) {34 Reporter.log("onTestSuccess: " + result.getName());35 }36 public void onTestFailure(ITestResult result) {37 Reporter.log("onTestFailure: " + result.getName());38 }39 public void onTestSkipped(ITestResult result) {40 Reporter.log("onTestSkipped: " + result.getName());41 }42 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {43 Reporter.log("onTestFailedButWithinSuccessPercentage: " + result.getName());44 }45 public void onStart(ITestContext context) {46 Reporter.log("onStart: " + context.getName());47 }48 public void onFinish(ITestContext context) {49 Reporter.log("onFinish: " + context.getName());50 }51}

Full Screen

Full Screen

addListener

Using AI Code Generation

copy

Full Screen

1package com.example;2import org.testng.TestNG;3import org.testng.xml.XmlSuite;4import java.util.ArrayList;5import java.util.List;6public class TestNGExample {7 public static void main(String args[]) {8 TestNG testNG = new TestNG();9 List<String> suites = new ArrayList<String>();10 suites.add("testng.xml");11 testNG.setTestSuites(suites);12 testNG.addListener(new MyListener());13 testNG.run();14 }15}16package com.example;17import org.testng.TestNG;18import org.testng.xml.XmlSuite;19import java.util.ArrayList;20import java.util.List;21public class TestNGExample {22 public static void main(String args[]) {23 TestNG testNG = new TestNG();24 List<String> suites = new ArrayList<String>();25 suites.add("testng.xml");26 testNG.setTestSuites(suites);27 testNG.addListener(new MyListener());28 testNG.run();29 }30}31package com.example;32import org.testng.TestNG;33import org.testng.xml.XmlSuite;34import java.util.ArrayList;35import java.util.List;36public class TestNGExample {37 public static void main(String args[]) {38 TestNG testNG = new TestNG();39 List<String> suites = new ArrayList<String>();40 suites.add("testng.xml");41 testNG.setTestSuites(suites);42 testNG.addListener(new MyListener());43 testNG.run();44 }45}46package com.example;47import org.testng.TestNG;48import org.testng.xml.XmlSuite;49import java.util.ArrayList;50import java.util.List;51public class TestNGExample {52 public static void main(String args[]) {53 TestNG testNG = new TestNG();54 List<String> suites = new ArrayList<String>();55 suites.add("testng.xml");56 testNG.setTestSuites(suites);57 testNG.addListener(new MyListener());58 testNG.run();59 }60}61package com.example;62import org.testng.TestNG;63import org.testng.xml.XmlSuite;64import java.util.ArrayList;65import java.util.List;66public class TestNGExample {67 public static void main(String args[]) {68 TestNG testNG = new TestNG();

Full Screen

Full Screen

addListener

Using AI Code Generation

copy

Full Screen

1public void testRegisterListener() {2 TestNG testNG = new TestNG();3 testNG.setTestClasses(new Class[] { TestClass.class });4 testNG.addListener(new TestListener());5 testNG.run();6}7public class TestListener implements ITestListener {8 public void onTestStart(ITestResult result) {9 System.out.println("Test started: " + result.getName());10 }11 public void onTestSuccess(ITestResult result) {12 System.out.println("Test passed: " + result.getName());13 }14 public void onTestFailure(ITestResult result) {15 System.out.println("Test failed: " + result.getName());16 }17 public void onTestSkipped(ITestResult result) {18 System.out.println("Test skipped: " + result.getName());19 }20 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {21 System.out.println("Test failed within success percentage: " + result.getName());22 }23 public void onStart(ITestContext context) {24 System.out.println("Test on start: " + context.getName());25 }26 public void onFinish(ITestContext context) {27 System.out.println("Test on finish: " + context.getName());28 }29}30public class TestClass {31 public void testMethod1() {32 System.out.println("Test method 1");33 }34 public void testMethod2() {35 System.out.println("Test method 2");36 }37}38public class TestListener implements ITestListener {39 public void onTestStart(ITestResult result) {40 System.out.println("Test started: " + result.getName());41 }

Full Screen

Full Screen

addListener

Using AI Code Generation

copy

Full Screen

1import org.testng.TestNG;2import java.util.ArrayList;3import java.util.List;4public class TestNGRunner {5public static void main(String[] args) {6TestNG runner=new TestNG();7List<String> suitefiles=new ArrayList<String>();8suitefiles.add("C:\\Users\\sourav\\Documents\\testng.xml");9runner.setTestSuites(suitefiles);10runner.run();11}12}13import org.testng.Assert;14import org.testng.SkipException;15import org.testng.annotations.Listeners;16import org.testng.annotations.Test;17@Listeners(com.testng.TestNGListener.class)18public class TestNGListenerDemo {19public void testToPass() {20System.out.println("This method to test pass");21}22public void testToFail() {23System.out.println("This method to test fail");24Assert.assertTrue(false);25}26public void testToSkip() {27System.out.println("This method to test skip");28throw new SkipException("Skipping - This is not ready for testing ");29}30}31import org.testng.ITestContext;32import org.testng.ITestListener;33import org.testng.ITestResult;34public class TestNGListener implements ITestListener {35public void onTestStart(ITestResult result) {36System.out.println("Test Case started: "+result.getName());37}38public void onTestSuccess(ITestResult result) {39System.out.println("Test Case passed: "+result.getName());40}41public void onTestFailure(ITestResult result) {42System.out.println("Test Case failed: "+result.getName());43}44public void onTestSkipped(ITestResult result) {45System.out.println("Test Case skipped: "+result.getName());46}47public void onTestFailedButWithinSuccessPercentage(ITestResult result) {48System.out.println("Test failed but within percentage %

Full Screen

Full Screen

TestNG tutorial

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.

Chapters

  1. JUnit 5 vs. TestNG: Compare and explore the core differences between JUnit 5 and TestNG from the Selenium WebDriver viewpoint.
  2. Installing TestNG in Eclipse: Start installing the TestNG Plugin and learn how to set up TestNG in Eclipse to begin constructing a framework for your test project.
  3. Create TestNG Project in Eclipse: Get started with creating a TestNG project and write your first TestNG test script.
  4. Automation using TestNG: Dive into how to install TestNG in this Selenium TestNG tutorial, the fundamentals of developing an automation script for Selenium automation testing.
  5. Parallel Test Execution in TestNG: Here are some essential elements of parallel testing with TestNG in this Selenium TestNG tutorial.
  6. Creating TestNG XML File: Here is a step-by-step tutorial on creating a TestNG XML file to learn why and how it is created and discover how to run the TestNG XML file being executed in parallel.
  7. Automation with Selenium, Cucumber & TestNG: Explore for an in-depth tutorial on automation using Selenium, Cucumber, and TestNG, as TestNG offers simpler settings and more features.
  8. JUnit Selenium Tests using TestNG: Start running your regular and parallel tests by looking at how to run test cases in Selenium using JUnit and TestNG without having to rewrite the tests.
  9. Group Test Cases in TestNG: Along with the explanation and demonstration using relevant TestNG group examples, learn how to group test cases in TestNG.
  10. Prioritizing Tests in TestNG: Get started with how to prioritize test cases in TestNG for Selenium automation testing.
  11. Assertions in TestNG: Examine what TestNG assertions are, the various types of TestNG assertions, and situations that relate to Selenium automated testing.
  12. DataProviders in TestNG: Deep dive into learning more about TestNG's DataProvider and how to effectively use it in our test scripts for Selenium test automation.
  13. Parameterization in TestNG: Here are the several parameterization strategies used in TestNG tests and how to apply them in Selenium automation scripts.
  14. TestNG Listeners in Selenium WebDriver: Understand the various TestNG listeners to utilize them effectively for your next plan when working with TestNG and Selenium automation.
  15. TestNG Annotations: Learn more about the execution order and annotation attributes, and refer to the prerequisites required to set up TestNG.
  16. TestNG Reporter Log in Selenium: Find out how to use the TestNG Reporter Log and learn how to eliminate the need for external software with TestNG Reporter Class to boost productivity.
  17. TestNG Reports in Jenkins: Discover how to generate TestNG reports in Jenkins if you want to know how to create, install, and share TestNG reports in Jenkins.

Certification

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.

YouTube

Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.

Run Testng automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful