Best Testng code snippet using org.testng.Interface ITestNGMethod.getDescription
Source:MyReporterListener.java  
...244                {245                    buff.append("<tr class=\"" + style246                            + (cq % 2 == 0 ? "odd" : "even") + "\">");247                }248                String description = method.getDescription();249                String testInstanceName = resultSet250                        .toArray(new ITestResult[] {})[0].getTestName();251                buff.append("<td><a href=\"#m"252                        + m_methodIndex253                        + "\">"254                        + qualifiedName(method)255                        + " "256                        + (description != null && description.length() > 0 ? "(\""257                                + description + "\")"258                                : "")259                        + "</a>"260                        + (null == testInstanceName ? "" : "<br>("261                                + testInstanceName + ")") + "</td>"262                        + "<td class=\"numi\">" + resultSet.size() + "</td>"...Source:AbstractMatcherTest.java  
...41    // Assert collections42    //---------------------------------------------------------------------43    protected <T> void assertMatches( T arg, Matcher<T> matcher )44    {45        assertMatches( arg, matcher, getDescription( matcher ) );46    }47    protected <T> void assertMatches( T arg, Matcher<T> matcher, String message )48    {49        doAssert( new AbstractMatcherTest.SimpleAssert<T>( matcher, arg, message )50        {51            @Override52            public void doAssert()53            {54                Assert.assertTrue( matcher.matches( arg ), message );55            }56        } );57    }58    protected <T> void assertDoesNotMatch( T arg, Matcher<? super T> matcher )59    {60        assertDoesNotMatch( arg, matcher, "Unexpected match" );61    }62    @SuppressWarnings( "unchecked" )63    protected  <T> void assertDoesNotMatch( T arg, Matcher<? super T> matcher, String message )64    {65        doAssert( new AbstractMatcherTest.SimpleAssert<T>( ( Matcher<T> ) matcher, arg, message )66        {67            @Override68            public void doAssert()69            {70                Assert.assertFalse( matcher.matches( arg ), message );71            }72        } );73    }74    /**75     * validates a description76     * @param expected77     * @param matcher78     */79    public static void assertDescription( String expected, Matcher<?> matcher )80    {81        Description description = new StringDescription();82        description.appendDescriptionOf( matcher );83        Assert.assertEquals( "Expected description", expected, description.toString() );84    }85    @SuppressWarnings( "unchecked" )86    protected <T> void assertMismatchDescription( T arg, Matcher<? super T> matcher, String expected )87    {88        doAssert( new AbstractMatcherTest.SimpleAssert<T>( ( Matcher<T> ) matcher, arg, expected )89        {90            @Override91            public void doAssert()92            {93                Assert.assertFalse( matcher.matches( arg ), "Precondition: Matcher should not match item." );94                Assert.assertEquals( expected, getMismatchDescription( matcher, arg ), "Expected mismatch description" );95            }96        } );97    }98    @SuppressWarnings( "unchecked" )99    protected <T> void assertMismatchDescription( T arg, Matcher<? super T> matcher, Pattern expected )100    {101        doAssert( new AbstractMatcherTest.SimpleAssert<T>( ( Matcher<T> ) matcher, arg, expected.pattern() )102        {103            @Override104            public void doAssert()105            {106                Assert.assertFalse( matcher.matches( arg ), "Precondition: Matcher should not match item." );107                Assert.assertTrue( expected.matcher( getMismatchDescription( matcher, arg ) ).matches(), "Expected mismatch description pattern" );108            }109        } );110    }111    @SuppressWarnings( "unchecked" )112    void assertDescription( Matcher<?> matcher, String expected )113    {114        doAssert( new AbstractMatcherTest.SimpleAssert( matcher, null, expected )115        {116            @Override117            public void doAssert()118            {119                Description description = new StringDescription();120                description.appendDescriptionOf( matcher );121                Assert.assertEquals( expected, description.toString().trim(), "Expected description" );122            }123        } );124    }125    @SuppressWarnings( "unchecked" )126    void assertNullSafe( Matcher<?> matcher )127    {128        doAssert( new AbstractMatcherTest.SimpleAssert( matcher, null, null )129        {130            @Override131            public void doAssert()132            {133                try134                {135                    matcher.matches( null );136                }137                catch( Exception e )138                {139                    Assert.fail( "Matcher was not null safe" );140                }141            }142        } );143    }144    @SuppressWarnings( "unchecked" )145    void assertUnknownTypeSafe( Matcher<?> matcher )146    {147        doAssert( new AbstractMatcherTest.SimpleAssert( matcher, null, null )148        {149            @Override150            public void doAssert()151            {152                try153                {154                    matcher.matches( new AbstractMatcherTest.UnknownType() );155                }156                catch( Exception e )157                {158                    Assert.fail("Matcher was not unknown type safe, because: " + e );159                }160            }161        } );162    }163    //endregion164    public void testIsNullSafe()165    {166        // should not throw a NullPointerException167        //createMatcher().matches( null );168    }169    public void testCopesWithUnknownTypes()170    {171        // should not throw ClassCastException172        //createMatcher().matches( new UnknownType() );173    }174    protected void describeTestCase()175    {176        ITestNGMethod iTestNGMethod = Reporter.getCurrentTestResult().getMethod();177        log.info( "Executing test : \n|-- name: {}\n|-- description: {} ",178                  iTestNGMethod.getQualifiedName(), iTestNGMethod.getDescription()179        );180    }181    //region Implementation of IMatcherAssetLifeCycle interface182    //---------------------------------------------------------------------183    // Implementation of IMatcherAssetLifeCycle interface184    //---------------------------------------------------------------------185    @Override186    public void onBeforeAssert( MatcherAssertEvent<?> ac )187    {188        log.info( "executing [ {} ]", ac.getMessage() );189    }190    @Override191    public void executeAssert( MatcherAssertEvent<?> assertCommand )192    {193        assertCommand.doAssert();194    }195    @Override196    public void onAssertSuccess( MatcherAssertEvent<?> ac )197    {198        log.info( "[ " + ac.getMessage() + " ] success: '" + getDescription( ac.getMatcher() ) + "'" );199    }200    @Override201    public void onAssertFailure( MatcherAssertEvent<?> ac, AssertionError ex )202    {203        log.error( "[ " + ac.getMessage() + " ] failed because: '" + getMismatchDescription( ac.getMatcher(), ac.getActual() ) + "'" );204    }205    @Override206    public void onAfterAssert( MatcherAssertEvent<?> assertCommand )207    {208        System.out.println( "\n" );209    }210    /**211     * assertion life cycle212     *213     * @param assertCommand the assertion command that includes assertion and test information214     */215    protected void doAssert( MatcherAssertEvent<?> assertCommand )216    {217        onBeforeAssert( assertCommand );218        try219        {220            executeAssert( assertCommand );221            onAssertSuccess( assertCommand );222        }223        catch( AssertionError ex )224        {225            onAssertFailure( assertCommand, ex );226            //errors.put( ex, assertCommand );227        }228        finally229        {230            onAfterAssert( assertCommand );231        }232    }233    @Deprecated234    protected void assertAll()235    {236        if( ! errors.isEmpty() )237        {238            int i = 1;239            StringBuilder sb = new StringBuilder( "The following <" )240                                       .append( errors.size() )241                                       .append( "> " )242                                       .append( "assertions failed:" );243            boolean first = true;244            for( Map.Entry<AssertionError, MatcherAssertEvent<?>> ae : errors.entrySet() )245            {246                if( first )247                {248                    first = false;249                }250                else251                {252                    sb.append( "," );253                }254                sb.append( "\n" ).append( i ++ ).append( ".\t" );255                sb.append( ae.getKey().getMessage() );256            }257            errors.clear();258            throw new AssertionError( sb.toString() );259        }260    }261    //endregion262    protected String getDescription( Matcher<?> matcher )263    {264        Description description = new StringDescription();265        matcher.describeTo( description );266        String full = description.toString().trim();267        return StringUtils.abbreviate( full, 60 );268    }269    protected <T> String getMismatchDescription( Matcher<?> matcher, T arg )270    {271        Description description = new StringDescription();272        matcher.describeMismatch( arg, description );273        return description.toString().trim();274    }275    //region Implementation of inner class SimpleAssert276    //---------------------------------------------------------------------...Source:ITestNGMethod.java  
...206  void setThreadPoolSize(int threadPoolSize);207208  boolean getEnabled();209210  public String getDescription();211  void setDescription(String description);212213  public void incrementCurrentInvocationCount();214  public int getCurrentInvocationCount();215  public void setParameterInvocationCount(int n);216  public int getParameterInvocationCount();217218  public ITestNGMethod clone();219220  public IRetryAnalyzer getRetryAnalyzer();221  public void setRetryAnalyzer(IRetryAnalyzer retryAnalyzer);222223  public boolean skipFailedInvocations();224  public void setSkipFailedInvocations(boolean skip);
...Source:TestngListener.java  
...73//		GenericLib.setCongigValue(GenericLib.sConfigFile, "DATE", sdateTime);74	}75	public void onTestSuccess(ITestResult result) 76	{77		sDescription.add(result.getMethod().getDescription());78		iPassCount = iPassCount+1;79		//GenericLib.setStatus(result.getName().toString(), "Passed",sTestName,sStatus);80	}81	public void onTestFailure(ITestResult result) 82	{83		sDescription.add(result.getMethod().getDescription());84		iFailCount = iFailCount+1;85		//GenericLib.setStatus(result.getName().toString(), "Failed",sTestName,sStatus);86		//NXGReports.addStep("Testcase Failed.", LogAs.FAILED, new CaptureScreen(ScreenshotOf.BROWSER_PAGE));87	}88	public void onTestSkipped(ITestResult result) 89	{90		sDescription.add(result.getMethod().getDescription());91		iSkippedCount = iSkippedCount+1;92//		GenericLib.setStatus(result.getName(), "Skipped",sTestName,sStatus);93	}94	public void onTestFailedButWithinSuccessPercentage(ITestResult result)95	{96	}97	public void onStart(ITestContext context) 98	{99	}100	public void onFinish(ITestContext context) 101	{102		Set<ITestResult> failedTests = context.getFailedTests().getAllResults();103		for (ITestResult temp : failedTests) {104			ITestNGMethod method = temp.getMethod();...Source:Guru99Reporter.java  
...40            System.out.println("--------FAILED TEST CASE---------");41            for (ITestNGMethod iTestNGMethod : failedMethods) {42                //Print failed test cases detail43                System.out.println("TESTCASE NAME->"+iTestNGMethod.getMethodName()44                        +"\nDescription->"+iTestNGMethod.getDescription()45                        +"\nPriority->"+iTestNGMethod.getPriority()46                        +"\n:Date->"+new Date(iTestNGMethod.getDate()));47                48            }49        }50        }51        52    }53}...getDescription
Using AI Code Generation
1String description = testMethod.getDescription();2String description = result.getDescription();3String description = testContext.getDescription();4testMethod.setDescription("New Description");5result.setDescription("New Description");6testContext.setDescription("New Description");7String name = testMethod.getTestName();8String name = result.getTestName();9String name = testContext.getTestName();10testMethod.setTestName("New Name");11result.setTestName("New Name");12testContext.setTestName("New Name");13String[] groups = testMethod.getGroups();14String[] groups = result.getGroups();15String[] groups = testContext.getGroups();16testMethod.setGroups(new String[]{"New Group"});17result.setGroups(new String[]{"New Group"});18testContext.setGroups(new String[]{"New Group"});19String[] groups = testMethod.getGroups();20String[] groups = result.getGroups();21String[] groups = testContext.getGroups();getDescription
Using AI Code Generation
1public void testMethod1() {2    System.out.println("Test method 1");3}4public void testMethod2() {5    System.out.println("Test method 2");6}7public void testMethod3() {8    System.out.println("Test method 3");9}10public void testMethod4() {11    System.out.println("Test method 4");12}13public void testMethod5() {14    System.out.println("Test method 5");15}16public void testMethod6() {17    System.out.println("Test method 6");18}19public void testMethod7() {20    System.out.println("Test method 7");21}22public void testMethod8() {23    System.out.println("Test method 8");24}25public void testMethod9() {26    System.out.println("Test method 9");27}28public void testMethod10() {29    System.out.println("Test method 10");30}31public void testMethod11() {32    System.out.println("Test method 11");33}34public void testMethod12() {35    System.out.println("Test method 12");36}37public void testMethod13() {38    System.out.println("Test method 13");39}40public void testMethod14() {41    System.out.println("Test method 14");42}43public void testMethod15() {44    System.out.println("Test method 15");45}46public void testMethod16() {47    System.out.println("Test method 16");48}49public void testMethod17() {50    System.out.println("Test method 17");51}52public void testMethod18() {53    System.out.println("Test method 18");54}55public void testMethod19() {56    System.out.println("Test method 19");57}58public void testMethod20() {59    System.out.println("Test method 20");60}61public void testMethod21() {62    System.out.println("Test method 21");63}64public void testMethod22() {65    System.out.println("Test method 22");66}67public void testMethod23() {68    System.out.println("Test method 23");69}70public void testMethod24() {71    System.out.println("Test method 24");72}73public void testMethod25()getDescription
Using AI Code Generation
1public class TestNgTest {2    public void test() throws Exception {3        Class<?> clazz = Class.forName("org.testng.ITestNGMethod");4        Method method = clazz.getMethod("getDescription");5        System.out.println(method.invoke(method));6    }7}8public class TestNgTest {9    public void test() throws Exception {10        Class<?> clazz = Class.forName("org.testng.ITestResult");11        Method method = clazz.getMethod("getDescription");12        System.out.println(method.invoke(method));13    }14}15public class TestNgTest {16    public void test() throws Exception {17        Class<?> clazz = Class.forName("org.testng.ITestResult");18        Method method = clazz.getMethod("getDescription");19        System.out.println(method.invoke(method));20    }21}getDescription
Using AI Code Generation
1package com.automation.remarks.testng;2import org.testng.ITestNGMethod;3import org.testng.annotations.Test;4public class TestNGMethodDescription {5    @Test(description = "This is a test method")6    public void testMethod() {7        ITestNGMethod testNGMethod = new ITestNGMethod() {8            public String getDescription() {9                return "This is a test method";10            }11        };12        System.out.println("Description of the test method is: " + testNGMethod.getDescription());13    }14}getDescription
Using AI Code Generation
1org.testng.ITestNGMethod method = null;2String description = method.getDescription();3System.out.println(description);4org.testng.ITestNGMethod method = null;5method.setDescription("This is a test method");6System.out.println(method.getDescription());7package com.javacodegeeks.testng.maven;8import org.testng.annotations.Test;9public class TestNGMavenTest {10@Test(description="This is a test method")11public void testMethodOne() {12System.out.println("TestNGMavenTest.testMethodOne");13}14}15package com.javacodegeeks.testng.maven;16import org.testng.annotations.Test;17public class TestNGMavenTest {18@Test(description="This is a test method")19public void testMethodOne() {20System.out.println("TestNGMavenTest.testMethodOne");21}22@Test(description="This is a test method")23public void testMethodTwo() {24System.out.println("TestNGMavenTest.testMethodTwo");25}26}27package com.javacodegeeks.testng.maven;28import org.testng.annotations.Test;29public class TestNGMavenTest {30@Test(description="This is a test method")31public void testMethodOne() {32System.out.println("TestNGMavenTest.testMethodOne");33}34@Test(description="This is a test method")35public void testMethodTwo() {36System.out.println("TestNGMavenTest.testMethodTwo");37}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!!
