testng annotations run code example

Example: annotations in testng

TESTNG: TestNextGeneration
   
-->  some of the annotation from TestNG are
    @Test --> is used to create test method. Normally we need a
main method to run a class/method, but with this annotation
we can run a class, without a main method.

   @BeforeClass: Runs one time before everything in this class.
   Only runs once, does not matter how many test methods we have.
We can;
prepare test data / set path / open browser / create
connections / initialize some classes / open test url / login

    @BeforeMethod: Always run before each test method. It
does not matter if the test passes or fails.
We can;
prepare test data / open browser / create connections /
initialize some classes / open test url / login

   @AfterMethod --> Methods with this annotation always run
after each test method. It does not matter if the test passes or
fails.
For example, we can use this annotation to
close browser / log out / delete test data / report / close connections

@AfterClass
Runs one time after everything in this class. Only runs once,
does not matter how many test methods we have.

@Priority
What are the annotations available in TestNG and
what is their purpose?
If we wat the methods to be executed in a different order we
use “Priority” annotations. The default it is = 0. If @Test does
not have a priority, it means first. Tests that have priority will
run based on the priority number.
If no priority, tests will run based on alphabetical order.

Tags:

Misc Example