Monday, 9 March 2020

Top asked advanced TestNG annotations: Set-1 for SDET/Automation Testing/Developers?




Must know TestNG annotations for Experienced Automation Testers
  • alwaysRun 
If set to true, this test method will always be run even if it depends on a method that failed.

  • dependsOnMethods
The list of methods this method depends on.

  • @test:
Marks a class or a method as part of the test.

Example:
@Test
public void methodone() {

           System.out.println("Methodone");

           // Failing test explicitly

           Assert.fail();

      }

      // alwaysRun attribute will override dependsOnMethods if dependent method is failed or skipped

      @Test(dependsOnMethods = "methodone", alwaysRun=true)

      public void methodtwo() {

           System.out.println("Method two");

      }




















  • Description:    
The description for this method. It is important to give a small information about the test case.


  • Enabled:
Whether methods on this class/method are enabled.
When executing TestNG tests, there are scenarios where you have to disable a particular test or a set of tests from getting executed. For example, where a serious bug exists in a feature due to certain tests belonging to certain scenarios that cannot be executed. As the issue has already been identified we may need to disable the test scenarios from being executed.
Disabling a test in TestNG can be done by setting the enable attribute of the @Test annotation to false. This will disable the test method from being executed as part of the test suite.
Note: If this attribute is set for the Test annotation at class level, all the public methods inside the class will be disabled.

Example:

@Test( description = "Verify Login with valid credentials", enabled=false )
Public void login(){}


  • invocationCount 
The number of times this method should get invoked.

  • threadPoolSize  
The size of the thread pool for this method. The method will be invoked from multiple threads as specified by invocationCount.

Note: this attribute is ignored if invocationCount is not specified

  • timeOut   
The maximum number of milliseconds this test should take.

Earlier we discussed how to run classes, methods, and tests in parallel or in multi-threaded mode. TestNG also provides the flexibility to configure a test method to be run in a multi-threaded environment. This is achieved by configuring it while using the @Test annotation on a method.
AutoTest.java
public class AutoTest
{
    @Test(threadPoolSize = 3, invocationCount = 6, timeOut = 1000)
    public void testMethod()
    {
        Long id = Thread.currentThread().getId();
        System.out.println("Test method executing on thread with id as: " + id);
    }
}
The method is configured to run in multi-threaded mode by using the threadPoolSize attribute along with the Test annotation. The value of the threadPoolSize is set to 3; this configures the test method to be run in three different threads.
The other two attributes, invocationCount and timeOut, configures the test to be invoked a multiple number of times and fail if the execution takes more time.

  •  expectedExceptions      
The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.

public class ExceptionTesting {


    @Test(expected = ArithmeticException.class)
    public void testWithException() {
        int i = 1 / 0;
    }

 


Learn (API-Microservice)Testing+(CoreJava+UI)-SDET with Self Paced Videos and one LIVE Doubt Session

TRANING VIDEOS AVAILABLE with Live Doubt Session @4500/-(course-1 below,API TRaining Videos With ClassNotes and Coding Set) and 6500/- (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122

Entire course content can be found below:  COURSE CONTENTCheck below link for question and answers with code:

Check below for TestNG annotations on @After and @Before:

TestNG@after@before 

Check below link on top Interview preparation:

SQL Query   Cucumber-BDD    Linux Commands    Maven   GIT
 


1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

All Time Popular Posts

Most Featured Post

Introduction to Web Testing with Playwright

  Introduction to Web Testing with Playwright 📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUG...