Wednesday 1 August 2018

Explain testNG annotation in sequence with real time scenario?example of @After@Before in TestNG?selenium interview qus/testng

Explained TestNG annotations in sequence, very imp interview questions.

Execute the script and try to observe how @test @Method @Suite @Class works.

In the interview they may ask to tell how many time @test and @method will run. I have added the output after executing it to eclipse.As we have pass three pair of values in dataprovider so 3 times test script got executed.

public class NewTest {
  @Test(dataProvider = "dp")
  public void f(Integer n, String s) {
  System.out.println(n + s);
  }
  @BeforeMethod
  public void beforeMethod() {
  System.out.println("Beforemethod:");
  }
  @AfterMethod
  public void afterMethod() {
  System.out.println("Aftermethod:");
  }
  @DataProvider
  public Object[][] dp() {
    return new Object[][] {
      new Object[] { 1, "a" },
      new Object[] { 2, "b" },
      new Object[] { 0,"2test" }
    };
  }
  @BeforeClass
  public void beforeClass() {
  System.out.println("Beforeclass:");
  }

  @AfterClass
  public void afterClass() {
  System.out.println("AfterClass");
  }

  @BeforeTest
  public void beforeTest() {
  System.out.println("BeforeTest:");
  }

  @AfterTest
  public void afterTest() {
  System.out.println("AfterTest");
  }

  @BeforeSuite
  public void beforeSuite() {
  System.out.println("BeforeSuite");
  }

  @AfterSuite
  public void afterSuite() {
  System.out.println("AfterSuite");
  }

}



Output:

BeforeSuite
BeforeTest:
Beforeclass:
Beforemethod:
1a
Aftermethod:
Beforemethod:
2b
Aftermethod:
Beforemethod:
02test
Aftermethod:
AfterClass
AfterTest
PASSED: f(1, "a")
PASSED: f(2, "b")
PASSED: f(0, "2test")

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================

AfterSuite

===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

No comments:

Post a Comment

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...