How to use TestListenerAdapter class of org.testng package

Best Testng code snippet using org.testng.TestListenerAdapter

Source:AnnotationTransformerTest.java Github

copy

Full Screen

2import org.assertj.core.api.iterable.Extractor;3import org.testng.Assert;4import org.testng.IAnnotationTransformer;5import org.testng.ITestResult;6import org.testng.TestListenerAdapter;7import org.testng.TestNG;8import org.testng.annotations.Test;9import org.testng.xml.Parser;10import org.testng.xml.XmlSuite;11import test.SimpleBaseTest;12import java.io.ByteArrayInputStream;13import java.util.Arrays;14import java.util.Collection;15import java.util.List;16import static org.assertj.core.api.Assertions.assertThat;17public class AnnotationTransformerTest extends SimpleBaseTest {18 private static final Extractor NAME_EXTRACTOR = new Extractor<ITestResult, String>() {19 @Override20 public String extract(ITestResult input) {21 return input.getName();22 }23 };24 /**25 * Make sure that without a transformer in place, a class-level26 * annotation invocationCount is correctly used.27 */28 @Test29 public void verifyAnnotationWithoutTransformer() {30 TestNG tng = create(AnnotationTransformerSampleTest.class);31 tng.setPreserveOrder(true);32 TestListenerAdapter tla = new TestListenerAdapter();33 tng.addListener(tla);34 tng.run();35 assertThat(tla.getPassedTests()).extracting(NAME_EXTRACTOR)36 .containsExactly(37 "five",38 "four", "four", "four", "four", "four",39 "three", "three", "three", "three", "three",40 "two", "two"41 );42 assertThat(tla.getFailedTests()).extracting(NAME_EXTRACTOR)43 .containsExactly("verify");44 }45 /**46 * Test a transformer on a method-level @Test47 */48 @Test49 public void verifyAnnotationTransformerMethod() {50 TestNG tng = create(AnnotationTransformerSampleTest.class);51 tng.setPreserveOrder(true);52 MyTransformer transformer = new MyTransformer();53 tng.setAnnotationTransformer(transformer);54 TestListenerAdapter tla = new TestListenerAdapter();55 tng.addListener(tla);56 tng.run();57 assertThat(transformer.getMethodNames()).contains("two", "three", "four", "five", "verify");58 assertThat(tla.getPassedTests()).extracting(NAME_EXTRACTOR)59 .containsExactly(60 "five", "five", "five", "five", "five",61 "four", "four", "four", "four",62 "three", "three", "three",63 "two", "two",64 "verify"65 );66 assertThat(tla.getFailedTests()).isEmpty();67 }68 @Test69 public void verifyAnnotationTransformerHasOnlyOneNonNullArgument() {70 TestNG tng = create(AnnotationTransformerSampleTest.class);71 MyParamTransformer transformer = new MyParamTransformer();72 tng.setAnnotationTransformer(transformer);73 tng.run();74 assertThat(transformer.isSuccess()).isTrue();75 }76 @Test77 public void verifyMyParamTransformerOnlyOneNonNull() {78 assertThat(MyParamTransformer.onlyOneNonNull(null, null, null)).isFalse();79 assertThat(MyParamTransformer.onlyOneNonNull(80 MyParamTransformer.class, MyParamTransformer.class.getConstructors()[0], null)).isFalse();81 assertThat(MyParamTransformer.onlyOneNonNull(MyParamTransformer.class, null, null)).isTrue();82 }83 /**84 * Without an annotation transformer, we should have zero85 * passed tests and one failed test called "one".86 */87 @Test88 public void verifyAnnotationTransformerClass2() {89 runTest(null, null, "one");90 }91 /**92 * With an annotation transformer, we should have one passed93 * test called "one" and zero failed tests.94 */95 @Test96 public void verifyAnnotationTransformerClass() {97 runTest(new MyTimeOutTransformer(), "one", null);98 }99 private void runTest(IAnnotationTransformer transformer,100 String passedName, String failedName)101 {102 MySuiteListener.triggered = false;103 MySuiteListener2.triggered = false;104 TestNG tng = new TestNG();105 tng.setVerbose(0);106 if (transformer != null) {107 tng.setAnnotationTransformer(transformer);108 }109 tng.setTestClasses(new Class[] { AnnotationTransformerClassSampleTest.class});110 TestListenerAdapter tla = new TestListenerAdapter();111 tng.addListener(tla);112 tng.run();113 List<ITestResult> results =114 passedName != null ? tla.getPassedTests() : tla.getFailedTests();115 String name = passedName != null ? passedName : failedName;116 Assert.assertEquals(results.size(), 1);117 Assert.assertEquals(name, results.get(0).getMethod().getMethodName());118 Assert.assertTrue(MySuiteListener.triggered);119 Assert.assertFalse(MySuiteListener2.triggered);120 }121 @Test122 public void verifyListenerAnnotationTransformerClass() {123 MySuiteListener.triggered = false;124 MySuiteListener2.triggered = false;125 TestNG tng = new TestNG();126 tng.setVerbose(0);127 tng.setAnnotationTransformer(new MyListenerTransformer());128 tng.setTestClasses(new Class[]{AnnotationTransformerClassSampleTest.class});129 tng.run();130 Assert.assertFalse(MySuiteListener.triggered);131 Assert.assertTrue(MySuiteListener2.triggered);132 }133 @Test134 public void verifyConfigurationTransformer() {135 TestNG tng = new TestNG();136 tng.setAnnotationTransformer(new ConfigurationTransformer());137 tng.setVerbose(0);138 tng.setTestClasses(new Class[] { ConfigurationSampleTest.class});139 TestListenerAdapter tla = new TestListenerAdapter();140 tng.addListener(tla);141 tng.run();142 Assert.assertEquals(ConfigurationSampleTest.getBefore(), "correct");143 }144 @Test145 public void verifyDataProviderTransformer() {146 TestNG tng = create();147 tng.setAnnotationTransformer(new DataProviderTransformer());148 tng.setTestClasses(new Class[] { AnnotationTransformerDataProviderSampleTest.class});149 TestListenerAdapter tla = new TestListenerAdapter();150 tng.addListener(tla);151 tng.run();152 Assert.assertEquals(tla.getPassedTests().size(), 1);153 }154 @Test155 public void verifyFactoryTransformer() {156 TestNG tng = create();157 tng.setAnnotationTransformer(new FactoryTransformer());158 tng.setTestClasses(new Class[] { AnnotationTransformerFactorySampleTest.class});159 TestListenerAdapter tla = new TestListenerAdapter();160 tng.addListener(tla);161 tng.run();162 Assert.assertEquals(tla.getPassedTests().size(), 1);163 }164 @Test(description = "Test for issue #605")165 public void verifyInvocationCountTransformer() {166 TestNG tng = create();167 tng.setTestClasses(new Class[] { AnnotationTransformerInvocationCountTest.class });168 TestListenerAdapter tla = new TestListenerAdapter();169 tng.addListener(tla);170 tng.run();171 Assert.assertEquals(tla.getPassedTests().size(), 3);172 tng = create();173 tng.setAnnotationTransformer(new AnnotationTransformerInvocationCountTest.InvocationCountTransformer(5));174 tng.setTestClasses(new Class[]{AnnotationTransformerInvocationCountTest.class});175 tla = new TestListenerAdapter();176 tng.addListener(tla);177 tng.run();178 Assert.assertEquals(tla.getPassedTests().size(), 5);179 }180 @Test181 public void annotationTransformerInXmlShouldBeRun() throws Exception {182 String xml = "<suite name=\"SingleSuite\" >" +183 " <listeners>" +184 " <listener class-name=\"test.annotationtransformer.AnnotationTransformerInTestngXml\" />" +185 " </listeners>" +186 " <test enabled=\"true\" name=\"SingleTest\">" +187 " <classes>" +188 " <class name=\"test.annotationtransformer.AnnotationTransformerInTestngXml\" />" +189 " </classes>" +190 " </test>" +191 "</suite>"192 ;193 ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());194 Collection<XmlSuite> suites = new Parser(is).parse();195 TestNG tng = create();196 tng.setXmlSuites(Arrays.asList(suites.toArray(new XmlSuite[0])));197 TestListenerAdapter tla = new TestListenerAdapter();198 tng.addListener(tla);199 tng.run();200 Assert.assertEquals(tla.getPassedTests().size(), 1);201 }202}...

Full Screen

Full Screen

Source:FailurePolicyTest.java Github

copy

Full Screen

1package test.configurationfailurepolicy;2import static org.testng.Assert.assertEquals;3import org.testng.ITestContext;4import org.testng.TestListenerAdapter;5import org.testng.TestNG;6import org.testng.annotations.BeforeClass;7import org.testng.annotations.DataProvider;8import org.testng.annotations.Test;9import org.testng.xml.XmlSuite;10import testhelper.OutputDirectoryPatch;11public class FailurePolicyTest {12 // only if this is run from an xml file that sets this on the suite13 @BeforeClass(enabled=false)14 public void setupClass( ITestContext testContext) {15 assertEquals(testContext.getSuite().getXmlSuite().getConfigFailurePolicy(), XmlSuite.CONTINUE);16 }17 @DataProvider( name="dp" )18 public Object[][] getData() {19 Object[][] data = new Object[][] {20 // params - confFail, confSkip, skipedTests21 new Object[] { new Class[] { ClassWithFailedBeforeClassMethod.class }, 1, 1, 1 },22 new Object[] { new Class[] { ClassWithFailedBeforeMethodAndMultipleTests.class }, 2, 0, 2 },23 new Object[] { new Class[] { ClassWithFailedBeforeMethodAndMultipleInvocations.class }, 4, 0, 4 },24 new Object[] { new Class[] { ExtendsClassWithFailedBeforeMethod.class }, 2, 2, 2 },25 new Object[] { new Class[] { ClassWithFailedBeforeClassMethod.class }, 1, 1, 1 },26 new Object[] { new Class[] { ExtendsClassWithFailedBeforeClassMethod.class }, 1, 2, 2 },27 new Object[] { new Class[] { ClassWithFailedBeforeClassMethod.class, ExtendsClassWithFailedBeforeClassMethod.class }, 2, 3, 3 },28 new Object[] { new Class[] { ClassWithSkippingBeforeMethod.class }, 0, 1, 1 },29 new Object[] { new Class[] { FactoryClassWithFailedBeforeMethod.class }, 2, 0, 2 },30 new Object[] { new Class[] { FactoryClassWithFailedBeforeMethodAndMultipleInvocations.class }, 8, 0, 8 },31 new Object[] { new Class[] { FactoryClassWithFailedBeforeClassMethod.class }, 2, 2, 2 },32 };33 return data;34 }35 @Test( dataProvider = "dp" )36 public void confFailureTest(Class[] classesUnderTest, int configurationFailures, int configurationSkips, int skippedTests) {37 TestListenerAdapter tla = new TestListenerAdapter();38 TestNG testng = new TestNG();39 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());40 testng.setTestClasses(classesUnderTest);41 testng.addListener(tla);42 testng.setVerbose(0);43 testng.setConfigFailurePolicy(XmlSuite.CONTINUE);44 testng.run();45 verify(tla, configurationFailures, configurationSkips, skippedTests);46 }47 @Test48 public void commandLineTest_policyAsSkip() {49 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),50 "-configfailurepolicy", "skip",51 "-testclass", "test.configurationfailurepolicy.ClassWithFailedBeforeMethodAndMultipleTests" };52 TestListenerAdapter tla = new TestListenerAdapter();53 TestNG.privateMain(argv, tla);54 verify(tla, 1, 1, 2);55 }56 @Test57 public void commandLineTest_policyAsContinue() {58 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),59 "-configfailurepolicy", "continue",60 "-testclass", "test.configurationfailurepolicy.ClassWithFailedBeforeMethodAndMultipleTests" };61 TestListenerAdapter tla = new TestListenerAdapter();62 TestNG.privateMain(argv, tla);63 verify(tla, 2, 0, 2);64 }65 @Test66 public void commandLineTestWithXMLFile_policyAsSkip() {67 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),68 "-configfailurepolicy", "skip", "src/test/resources/testng-configfailure.xml" };69 TestListenerAdapter tla = new TestListenerAdapter();70 TestNG.privateMain(argv, tla);71 verify(tla, 1, 1, 2);72 }73 @Test74 public void commandLineTestWithXMLFile_policyAsContinue() {75 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),76 "-configfailurepolicy", "continue", "src/test/resources/testng-configfailure.xml" };77 TestListenerAdapter tla = new TestListenerAdapter();78 TestNG.privateMain(argv, tla);79 verify(tla, 2, 0, 2);80 }81 private void verify( TestListenerAdapter tla, int configurationFailures, int configurationSkips, int skippedTests ) {82 assertEquals(tla.getConfigurationFailures().size(), configurationFailures, "wrong number of configuration failures");83 assertEquals(tla.getConfigurationSkips().size(), configurationSkips, "wrong number of configuration skips");84 assertEquals(tla.getSkippedTests().size(), skippedTests, "wrong number of skipped tests");85 }86}...

Full Screen

Full Screen

Source:CommandLineTest.java Github

copy

Full Screen

...3import static org.testng.Assert.assertTrue;4import org.testng.Assert;5import org.testng.ITestContext;6import org.testng.ITestResult;7import org.testng.TestListenerAdapter;8import org.testng.TestNG;9import org.testng.annotations.Test;10import test.sample.JUnitSample1;11import testhelper.OutputDirectoryPatch;12import java.util.List;13public class CommandLineTest {14 /**15 * Test -junit16 */17 @Test(groups = { "current" } )18 public void junitParsing() {19 String[] argv = {20 "-log", "0",21 "-d", OutputDirectoryPatch.getOutputDirectory(),22 "-junit",23 "-testclass", "test.sample.JUnitSample1"24 };25 TestListenerAdapter tla = new TestListenerAdapter();26 TestNG.privateMain(argv, tla);27 List<ITestResult> passed = tla.getPassedTests();28 assertEquals(passed.size(), 2);29 String test1 = passed.get(0).getMethod().getMethodName();30 String test2 = passed.get(1).getMethod().getMethodName();31 assertTrue(JUnitSample1.EXPECTED1.equals(test1) && JUnitSample1.EXPECTED2.equals(test2) ||32 JUnitSample1.EXPECTED1.equals(test2) && JUnitSample1.EXPECTED2.equals(test1));33 }34 /**35 * Test the absence of -junit36 */37 @Test(groups = { "current" } )38 public void junitParsing2() {39 String[] argv = {40 "-log", "0",41 "-d", OutputDirectoryPatch.getOutputDirectory(),42 "-testclass", "test.sample.JUnitSample1"43 };44 TestListenerAdapter tla = new TestListenerAdapter();45 TestNG.privateMain(argv, tla);46 List<ITestResult> passed = tla.getPassedTests();47 assertEquals(passed.size(), 0);48 }49 /**50 * Test the ability to override the default command line Suite name51 */52 @Test(groups = { "current" } )53 public void suiteNameOverride() {54 String suiteName="MySuiteName";55 String[] argv = {56 "-log", "0",57 "-d", OutputDirectoryPatch.getOutputDirectory(),58 "-junit",59 "-testclass", "test.sample.JUnitSample1",60 "-suitename", "\""+suiteName+"\""61 };62 TestListenerAdapter tla = new TestListenerAdapter();63 TestNG.privateMain(argv, tla);64 List<ITestContext> contexts = tla.getTestContexts();65 assertTrue(contexts.size()>0);66 for (ITestContext context:contexts) {67 assertEquals(context.getSuite().getName(),suiteName);68 }69 }70 /**71 * Test the ability to override the default command line test name72 */73 @Test(groups = { "current" } )74 public void testNameOverride() {75 String testName="My Test Name";76 String[] argv = {77 "-log", "0",78 "-d", OutputDirectoryPatch.getOutputDirectory(),79 "-junit",80 "-testclass", "test.sample.JUnitSample1",81 "-testname", "\""+testName+"\""82 };83 TestListenerAdapter tla = new TestListenerAdapter();84 TestNG.privateMain(argv, tla);85 List<ITestContext> contexts = tla.getTestContexts();86 assertTrue(contexts.size()>0);87 for (ITestContext context:contexts) {88 assertEquals(context.getName(),testName);89 }90 }91 @Test92 public void testUseDefaultListenersArgument() {93 TestNG.privateMain(new String[] {94 "-log", "0", "-usedefaultlisteners", "false", "-testclass", "test.sample.JUnitSample1"95 }, null);96 }97 @Test98 public void testMethodParameter() {99 String[] argv = {100 "-log", "0",101 "-d", OutputDirectoryPatch.getOutputDirectory(),102 "-methods", "test.sample.Sample2.method1,test.sample.Sample2.method3",103 };104 TestListenerAdapter tla = new TestListenerAdapter();105 TestNG.privateMain(argv, tla);106 List<ITestResult> passed = tla.getPassedTests();107 Assert.assertEquals(passed.size(), 2);108 Assert.assertTrue((passed.get(0).getName().equals("method1") &&109 passed.get(1).getName().equals("method3"))110 ||111 (passed.get(1).getName().equals("method1") &&112 passed.get(0).getName().equals("method3")));113 }114 private static void ppp(String s) {115 System.out.println("[CommandLineTest] " + s);116 }117}...

Full Screen

Full Screen

Source:TestngListener.java Github

copy

Full Screen

...3import java.util.List;4import org.testng.ITestContext;5import org.testng.ITestNGMethod;6import org.testng.ITestResult;7import org.testng.TestListenerAdapter;8import main.java.com.dbyl.appiumCore.utils.CaseId;9/**10 * The listener interface for receiving testng events. The class that is11 * interested in processing a testng event implements this interface, and the12 * object created with that class is registered with a component using the13 * component's <code>addTestngListener<code> method. When the testng event14 * occurs, that object's appropriate method is invoked.15 *16 * @author Young17 * @version V1.018 * @see TestngEvent19 */20public class TestngListener extends TestListenerAdapter {21 /*22 * (non-Javadoc)23 * 24 * @see org.testng.TestListenerAdapter#onTestFailure(org.testng.ITestResult)25 */26 @Override27 public void onTestFailure(ITestResult tr) {28 ITestNGMethod im = tr.getMethod();29 getCaseID(im);30 super.onTestFailure(tr);31 }32 /*33 * (non-Javadoc)34 * 35 * @see org.testng.TestListenerAdapter#onTestSkipped(org.testng.ITestResult)36 */37 @Override38 public void onTestSkipped(ITestResult tr) {39 ITestNGMethod im = tr.getMethod();40 getCaseID(im);41 super.onTestSkipped(tr);42 }43 /*44 * (non-Javadoc)45 * 46 * @see org.testng.TestListenerAdapter#onStart(org.testng.ITestContext)47 */48 @Override49 public void onStart(ITestContext testContext) {50 System.out.println("Start Test");51 super.onStart(testContext);52 }53 /*54 * (non-Javadoc)55 * 56 * @see org.testng.TestListenerAdapter#onFinish(org.testng.ITestContext)57 */58 @Override59 public void onFinish(ITestContext testContext) {60 // TODO Auto-generated method stub61 super.onFinish(testContext);62 }63 /*64 * (non-Javadoc)65 * 66 * @see org.testng.TestListenerAdapter#getFailedTests()67 */68 @Override69 public List<ITestResult> getFailedTests() {70 // TODO Auto-generated method stub71 return super.getFailedTests();72 }73 /*74 * (non-Javadoc)75 * 76 * @see org.testng.TestListenerAdapter#getPassedTests()77 */78 @Override79 public List<ITestResult> getPassedTests() {80 // TODO Auto-generated method stub81 return super.getPassedTests();82 }83 /*84 * (non-Javadoc)85 * 86 * @see org.testng.TestListenerAdapter#getSkippedTests()87 */88 @Override89 public List<ITestResult> getSkippedTests() {90 // TODO Auto-generated method stub91 return super.getSkippedTests();92 }93 /*94 * (non-Javadoc)95 * 96 * @see org.testng.TestListenerAdapter#onTestStart(org.testng.ITestResult)97 */98 @Override99 public void onTestStart(ITestResult result) {100 ITestNGMethod im = result.getMethod();101 getCaseID(im);102 super.onTestStart(result);103 }104 /**105 * Gets the case ID.106 *107 * @param im108 * the im109 * @return the case ID110 */111 private String[] getCaseID(ITestNGMethod im) {112 Method m = im.getConstructorOrMethod().getMethod();113 CaseId caseId = m.getAnnotation(CaseId.class);114 if (null != caseId) {115 for (String str : caseId.id()) {116 System.out.println("++++++++++++++++>>>>>>>>>>>>>\n\n\n" + str + "<<<<<<<\n\n");117 }118 return caseId.id();119 }120 return null;121 }122 /*123 * (non-Javadoc)124 * 125 * @see org.testng.TestListenerAdapter#getTestContexts()126 */127 @Override128 public List<ITestContext> getTestContexts() {129 // TODO Auto-generated method stub130 return super.getTestContexts();131 }132}...

Full Screen

Full Screen

Source:CheckTestNamesTest.java Github

copy

Full Screen

1package test.sanitycheck;2import java.util.Arrays;3import org.testng.Assert;4import org.testng.TestListenerAdapter;5import org.testng.TestNG;6import org.testng.TestNGException;7import org.testng.annotations.Test;8import org.testng.xml.XmlClass;9import org.testng.xml.XmlSuite;10import org.testng.xml.XmlTest;11import test.SimpleBaseTest;12public class CheckTestNamesTest extends SimpleBaseTest {13 /**14 * Child suites and same suite has two tests with same name15 */16 @Test17 public void checkWithChildSuites() {18 runSuite("sanitycheck/test-a.xml");19 }20 /**21 * Simple suite with two tests with same name22 */23 @Test24 public void checkWithoutChildSuites() {25 runSuite("sanitycheck/test1.xml");26 }27 private void runSuite(String suitePath)28 {29 TestListenerAdapter tla = new TestListenerAdapter();30 boolean exceptionRaised = false;31 try {32 TestNG tng = create();33 String testngXmlPath = getPathToResource(suitePath);34 tng.setTestSuites(Arrays.asList(testngXmlPath));35 tng.addListener(tla);36 tng.run();37 } catch (TestNGException ex) {38 exceptionRaised = true;39 Assert.assertEquals(tla.getPassedTests().size(), 0);40 Assert.assertEquals(tla.getFailedTests().size(), 0);41 }42 Assert.assertTrue(exceptionRaised);43 }44 /**45 * Simple suite with no two tests with same name46 */47 @Test48 public void checkNoError() {49 TestListenerAdapter tla = new TestListenerAdapter();50 TestNG tng = create();51 String testngXmlPath = getPathToResource("sanitycheck/test2.xml");52 tng.setTestSuites(Arrays.asList(testngXmlPath));53 tng.addListener(tla);54 tng.run();55 Assert.assertEquals(tla.getPassedTests().size(), 2);56 }57 /**58 * Child suites and tests within different suites have same names59 */60 @Test(enabled = false)61 public void checkNoErrorWtihChildSuites() {62 TestListenerAdapter tla = new TestListenerAdapter();63 TestNG tng = create();64 String testngXmlPath = getPathToResource("sanitycheck/test-b.xml");65 tng.setTestSuites(Arrays.asList(testngXmlPath));66 tng.addListener(tla);67 tng.run();68 Assert.assertEquals(tla.getPassedTests().size(), 4);69 }70 /**71 * Checks that suites created programmatically also run as expected72 */73 @Test74 public void checkTestNamesForProgrammaticSuites() {75 XmlSuite xmlSuite = new XmlSuite();76 xmlSuite.setName("SanityCheckSuite");...

Full Screen

Full Screen

Source:MixedTest.java Github

copy

Full Screen

1package test.mixed;2import org.testng.Assert;3import org.testng.TestListenerAdapter;4import org.testng.TestNG;5import org.testng.annotations.Test;6import test.BaseTest;7import testhelper.OutputDirectoryPatch;8/**9 *10 * @author lukas11 */12public class MixedTest extends BaseTest {13 @Test14 public void mixedWithExcludedGroups() {15 String[] argv = {16 "-d", OutputDirectoryPatch.getOutputDirectory(),17 "-log", "0",18 "-mixed",19 "-groups", "unit",20 "-excludegroups", "ignore",21 "-testclass", "test.mixed.JUnit3Test1,test.mixed.JUnit4Test1,test.mixed.TestNGTest1,test.mixed.TestNGGroups"22 };23 TestListenerAdapter tla = new TestListenerAdapter();24 TestNG.privateMain(argv, tla);25 Assert.assertEquals(tla.getPassedTests().size(), 5); //2 from junit3test1, 2 from junit4test1, 0 from testngtest1 (no groups), 1 from testnggroups (1 is included, 1 is excluded)26 Assert.assertEquals(tla.getFailedTests().size(), 0);27 }28 @Test29 public void mixedClasses() {30 String[] argv = {31 "-d", OutputDirectoryPatch.getOutputDirectory(),32 "-log", "0",33 "-mixed",34 "-testclass", "test.mixed.JUnit3Test1,test.mixed.JUnit4Test1,test.mixed.TestNGTest1"35 };36 TestListenerAdapter tla = new TestListenerAdapter();37 TestNG.privateMain(argv, tla);38 Assert.assertEquals(tla.getPassedTests().size(), 6);39 Assert.assertEquals(tla.getFailedTests().size(), 0);40 }41 @Test42 public void mixedMethods() {43 String[] argv = {44 "-d", OutputDirectoryPatch.getOutputDirectory(),45 "-mixed",46 "-log", "0",47 "-methods", "test.mixed.JUnit3Test1.testB,test.mixed.JUnit4Test1.atest,test.mixed.TestNGTest1.tngCustomTest1"48 };49 TestListenerAdapter tla = new TestListenerAdapter();50 TestNG.privateMain(argv, tla);51 Assert.assertEquals(tla.getPassedTests().size(), 3);52 Assert.assertEquals(tla.getFailedTests().size(), 0);53 }54}

Full Screen

Full Screen

Source:ListenerClass.java Github

copy

Full Screen

23import org.testng.IClass;4import org.testng.ITestResult;5import org.testng.Reporter;6import org.testng.TestListenerAdapter;78// TODO: Auto-generated Javadoc9/**10 * The Class ListenerClass.11 */12public class ListenerClass extends TestListenerAdapter {13 14 /* (non-Javadoc)15 * @see org.testng.TestListenerAdapter#onTestStart(org.testng.ITestResult)16 */17 @Override18 public void onTestStart(ITestResult tr) {19 //Reporter.log("Test Started....");20 }2122 /* (non-Javadoc)23 * @see org.testng.TestListenerAdapter#onTestSuccess(org.testng.ITestResult)24 */25 @Override26 public void onTestSuccess(ITestResult tr) {2728 log("Test '" + tr.getName() + "' PASSED");2930 // This will print the class name in which the method is present31 3233 // This will print the priority of the method.34 // If the priority is not defined it will print the default priority as35 // 'o'36 log("Priority of this method is " + tr.getMethod().getPriority());3738 System.out.println(".....");39 }4041 /* (non-Javadoc)42 * @see org.testng.TestListenerAdapter#onTestFailure(org.testng.ITestResult)43 */44 @Override45 public void onTestFailure(ITestResult tr) {4647 log("Test '" + tr.getName() + "' FAILED");48 log("Priority of this method is " + tr.getMethod().getPriority());49 System.out.println(".....");50 }5152 /* (non-Javadoc)53 * @see org.testng.TestListenerAdapter#onTestSkipped(org.testng.ITestResult)54 */55 @Override56 public void onTestSkipped(ITestResult tr) {57 log("Test '" + tr.getName() + "' SKIPPED");58 System.out.println(".....");59 }6061 /**62 * Log.63 *64 * @param methodName the method name65 */66 private void log(String methodName) {67 System.out.println(methodName); ...

Full Screen

Full Screen

Source:TestNG173Test.java Github

copy

Full Screen

1package test.testng173;2import java.util.Arrays;3import org.testng.Assert;4import org.testng.ITestResult;5import org.testng.TestListenerAdapter;6import org.testng.TestNG;7import org.testng.annotations.Test;8import org.testng.xml.XmlClass;9import org.testng.xml.XmlSuite;10import org.testng.xml.XmlTest;11import test.SimpleBaseTest;12public class TestNG173Test extends SimpleBaseTest {13 @Test14 public void orderShouldBePreservedInMethodsWithSameNameAndInDifferentClasses() {15 TestNG tng = create();16 XmlSuite s = createXmlSuite("PreserveOrder");17 XmlTest t = new XmlTest(s);18 t.getXmlClasses().add(new XmlClass("test.testng173.ClassA"));19 t.getXmlClasses().add(new XmlClass("test.testng173.ClassB"));20 t.setPreserveOrder("true");21 tng.setXmlSuites(Arrays.asList(s));22 TestListenerAdapter tla = new TestListenerAdapter();23 tng.addListener(tla);24 tng.run();25 // bug26 //verifyPassedTests(tla, "test1", "test2", "testX", "test1", "test2");27 // Proposed fix28 verifyPassedTests(tla, "test1", "test2", "testX", "test2", "test1");29 }30 @Test31 public void orderShouldBePreservedInMethodsWithSameNameAndInDifferentClassesAndDifferentPackage() {32 TestNG tng = create();33 XmlSuite s = createXmlSuite("PreserveOrder");34 XmlTest t = new XmlTest(s);35 t.getXmlClasses().add(new XmlClass("test.testng173.ClassA"));36 t.getXmlClasses().add(new XmlClass("test.testng173.anotherpackage.ClassC"));37 t.setPreserveOrder("true");38 tng.setXmlSuites(Arrays.asList(s));39 TestListenerAdapter tla = new TestListenerAdapter();40 tng.addListener(tla);41 tng.run();42 // bug43 //verifyPassedTests(tla, "test1", "test2", "testX", "test1", "test2");44 verifyPassedTests(tla, "test1", "test2", "testX", "test2", "test1");45 }46}...

Full Screen

Full Screen

TestListenerAdapter

Using AI Code Generation

copy

Full Screen

1import org.testng.TestListenerAdapter;2import org.testng.TestNG;3import org.testng.xml.XmlSuite;4public class TestNGRunner {5 public static void main(String[] args) {6 TestListenerAdapter tla = new TestListenerAdapter();7 TestNG testng = new TestNG();8 testng.setTestClasses(new Class[] { TestNGTests.class });9 testng.addListener(tla);10 testng.run();11 }12}13package com.test;14import org.testng.annotations.Test;15public class TestNGTests {16 public void testMethod1() {17 System.out.println("TestNG testMethod1");18 }19 public void testMethod2() {20 System.out.println("TestNG testMethod2");21 }22 public void testMethod3() {23 System.out.println("TestNG testMethod3");24 }25}

Full Screen

Full Screen

TestListenerAdapter

Using AI Code Generation

copy

Full Screen

1package com.automation.tests;2import org.testng.Assert;3import org.testng.annotations.Test;4import org.testng.annotations.BeforeTest;5import org.testng.annotations.AfterTest;6import org.testng.ITestResult;7import org.testng.TestListenerAdapter;8public class TestNGListenerTest extends TestListenerAdapter {9 public void testMethod1() {10 System.out.println("TestNGListenerTest -> testMethod1");11 }12 public void testMethod2() {13 System.out.println("TestNGListenerTest -> testMethod2");14 Assert.assertTrue(false);15 }16 public void beforeTest() {17 System.out.println("TestNGListenerTest -> beforeTest");18 }19 public void afterTest() {20 System.out.println("TestNGListenerTest -> afterTest");21 }22 public void onTestFailure(ITestResult tr) {23 System.out.println("TestNGListenerTest -> onTestFailure");24 }25 public void onTestSkipped(ITestResult tr) {26 System.out.println("TestNGListenerTest -> onTestSkipped");27 }28 public void onTestSuccess(ITestResult tr) {29 System.out.println("TestNGListenerTest -> onTestSuccess");30 }31}32In the output, we can see that before executing the test cases, beforeTest() method is called. After executing the test cases, afterTest() method is called. When a test case fails, onTestFailure() method is called. When a test case passes, onTestSuccess() method is called. When a

Full Screen

Full Screen
copy
1public class SessionMap {23 private static Map<ServletContext, Set<HttpSession>> map =4 new HashMap<ServletContext, Set<HttpSession>>();56 private SessionMap() {7 }89 public static Map<ServletContext, Set<HttpSession>> getInstance() {10 return map;11 }1213 public static void invalidate(String[] contexts) {14 synchronized (map) {15 List<String> l = Arrays.asList(contexts); 16 for (Map.Entry<ServletContext, Set<HttpSession>> e : map.entrySet()) {17 // context name without the leading slash18 String c = e.getKey().getContextPath().substring(1);19 if (l.contains(c)) {20 for (HttpSession s : e.getValue()) 21 s.invalidate();22 }23 }24 }25 }2627}28
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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful