TestNG Annotations:
- Annotations can be used to
decide the program execution flow.
- They are used to determine
the order in which test methods are executed.
Example:
@Test
@BeforeTest,
@AfterTest
@BeforeMethod,@AfterMethod
@BeforeSuite, @AfterSuite
@BeforeClass, @AfterClass
// decides what test
methods neeed to be executed first, 2nd , 3rd..etc
Annotation starts with @ symbol
Annotations decide the sequence in which test methods need to be
executed (e.g., first, second, third, etc.).
Test Method:
If we define a test method with the @Test
annotation, it is called a Test Method (or) Test (or) Test Annotation method.
Exmaple 1: Define TestA() with @Test
Syntax:
@Test // Test Method
(or) Test Annontation method
(or) Test
public void TestA()
{
}
Example 2: Define TestB() with
@Test:
@Test // Test Method
public void TestB()
{
}
Define class with
one @Test:
package TestNGBasics1;
public class TestAnnontationMethod {
//
// public static void
main(String[] args) {
// //
TODO Auto-generated method stub
// System.out.println("hi
java");
//
// }
// No main() method
// Note: The "Run as Java
application" option is not available if there is no main() method
// Going forward, we don't use main()
// TestNG will take care of execution based on
annotations
}
//-------------------------------------
// Define @Test
annotation method
ex1:
package TestNGBasics1;
import org.testng.annotations.Test;
public class TestAnnotationMethod {
// public static void
main(String[] args) {
// System.out.println("Hi
Java");
//
// }
// No main() method
// Note: The "Run
as Java application" option is not available if there is no main() method
// Going forward, we don't use main()
// TestNG will take care
of execution based on annotations
// TestNG executes Test Methods based on annontations
// Define @Test method
// Define annotation
before the method (or Test method or Test annotation method or Test case or
Test)
@Test
public void testA() {
// Normal Java
method
System.out.println("stmt-1 from testA()");
System.out.println("stmt-2 from testA()");
}
}
Run TestNG Class:
Right-click inside the code > Click 'Run as TestNG Test' option.
o/p:
----
[RemoteTestNG] detected TestNG version 7.0.0
stmt-1 from test A()
stmt-2 from testA()
PASSED: testA
===============================================
Default test
Tests run: 1, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Define Class with One @Test Method and One Non-Test Method:
package
TestNgBasics1;
import
org.testng.annotations.Test;
public class
TestAnnotation2 {
// Define Test method
@Test // Test Method
public void testA() { // normal Java method
System.out.println("stmt-1 from
testA()");
System.out.println("stmt-2 from
testA()");
}
// Note: If the annotation is not there,
TestNG does not execute that method
// If the annotation is there, TestNG
executes the test methods
public void testB() { // no annotation --
it does not run this method
System.out.println("stmt-1 from
testB()");
System.out.println("stmt-2 from
testB()");
}
}
o/p:
[RemoteTestNG] detected
TestNG version 7.0.0
stmt-1 from test A()
stmt-2 from testA()
PASSED: testA
===============================================
Default test
Tests run: 1, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
HW define test method testC()
in separate classes and run them
HW define test method testD()
in separate classes and run them
Define Class with Two @Test Methods:
// If 2 test method names have the same
annotation, TestNG executes test methods based on "alphabetical
order"
// ex: testA(), testB()
package TestNgBasics1;
import org.testng.annotations.Test;
public class TestAnnonationWith2Methods
{
// Define @Test
@Test // Test Method
public
void testB() // no annotation -- not run this method
{
System.out.println("stmt-1
from testB()");
System.out.println("stmt-2
from testB()");
}
//
Define @Test
@Test // Test Method
public
void testA() // normal java method
{
System.out.println("stmt-1
from testA()");
System.out.println("stmt-2
from testA()");
}
}
o/p:
TestNG will execute the test methods in alphabetical order, so testA() will run before testB().
[RemoteTestNG] detected
TestNG version 7.0.0
stmt-1 from testA()
stmt-2 from testA()
stmt-1 from test B()
stmt-2 from testB()
PASSED: testA
PASSED: testB
===============================================
Default test
Tests run: 2, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
HW Define testC() with @Test annont for above program and execute using TestNg, write the o/p?
HW Define testD() with @Test annont for above program and execute using TestNg, write the o/p?
Example 2: Write Output for the Below Program
package TestNgBasics1;
import org.testng.annotations.Test;
public class TestAnnonationWith2Methods2 {
// it executes test method names based on
alphabetical order
// apple(),
bat(). dog()
@Test
public void dog()
// 2
{
System.out.println("stmt-1
from Dog");
}
@Test
public void bat()
// 1
{
System.out.println("stmt-1
from bat");
}
@Test
public void
apple() // 1
{
System.out.println("stmt-1
from apple");
}
@Test
public void
apple123() // 1 //
apple, ant, august ---> ant()
, apple() , august()
{
System.out.println("stmt-1
from apple123");
}
}
o/p:
---
[RemoteTestNG] detected TestNG version 7.0.0
stmt-1 from apple
stmt-1 from apple123
stmt-1 from bat
stmt-1 from Dog
PASSED: apple
PASSED: apple123
PASSED: bat
PASSED: dog
===============================================
Default test
Tests run: 4, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
Define Class with @BeforeTest:
// BeforeTest annotation method gets executed only once before
executing any test methods in curent class
package TestNgBasics1;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class BeforeTestAnnonBasics1
{
//Define
BeforeTest annont
// @Test
@BeforeTest
public void preCondition()
{
System.out.println("@BeforeTest
annot method gets executed only once before executing any @test method ");
}
//Define
Test Method
@Test
public void
testA() //Test Method
{
System.out.println("stmt-1
from testA()");
}
}
o/p:
[RemoteTestNG] detected TestNG version 7.0.0
@BeforeTest annon metod gets executed only once before executing
any @test method
stmt-1 from testA()
PASSED: testA
===============================================
Default test
Tests run: 1, Failures: 0,
Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Define Class with @BeforeTest Method and Two @Test Methods:
// BeforeTest
annotation method gets executed only once before executing test method
package TestNgBasics1;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class BeforeTestAnnonBasics1
{
//Define
@BeforeTest
@BeforeTest
public
void preCondition()
{
System.out.println("@BeforeTest
annot metod gets executed only once before executing any @test method ");
}
@Test // Test Method
public
void testB()
{
System.out.println("stmt-1
from testB()");
}
@Test // Test Method
public
void testA() // 1
{
System.out.println("stmt-1
from testA()");
}
}
o/p:
[RemoteTestNG] detected TestNG version 7.0.0
@BeforeTest annon metod gets executed only once before executing
any @test method
stmt-1 from testA()
stmt-1 from testB()
PASSED: testA
PASSED: testB
===============================================
Default test
Tests run: 2, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
Note:
· @BeforeTest
is not considered as Test method (or) Test (or) Test case.
·
Only @Test methods will be considered as Test
method (or) Test (or) Test case
HW Add testC() test method-in above program and Check the o/p ?
@AfterTest:
// AfterTest
annotation method gets executed only once after executing all test
methods in current class
package TestNgBasics1;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
public class AfterTestAnnonBasics {
// Define
AfterTest annot
//
@beforeTest gets excuted only once before executing all @Test method
@AfterTest
public
void postCondition()
{
System.out.println("@AfterTest
gets executed only once after executing @Test Method");
}
@Test // Test Method
public
void testA()
{
System.out.println("stmt-1
from testA()");
}
}
o/p:
[RemoteTestNG] detected TestNG version 7.0.0
stmt-1 from testA()
@AfterTest gets executed only once after excuting @Test Method
PASSED: testA
===============================================
Default test
Tests run: 1, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
ex2:
Define class with
2 test methods with @AfterTest :
package TestNgBasics1;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
public class AfterTestAnnonBasics {
// Define
AfterTest annot
//
@beforeTest gets excuted only once before executing all @Test method
@AfterTest
public
void postCondition()
{
System.out.println("@AfterTest
gets executed only once after executing @Test Method");
}
@Test // Test Method
public
void testA()
{
System.out.println("stmt-1
from testA()");
}
@Test // Test Method
public
void testB()
{
System.out.println("stmt-1
from testB()");
}
}
o/p:
[RemoteTestNG] detected TestNG version 7.0.0
stmt-1 from testA()
stmt-1 from testB()
@AfterTest gets executed only once after excuting @Test Method
PASSED: testA
PASSED: testB
===============================================
Default test
Tests run: 2, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
HW Add testC() test method-
and Check the o/p for above program?
Define class with
@BeforeTest and @AfterTest Combinations :
package TestNgBasics1;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class BeforeTestAfterTestCombinations {
// Define
@BeforeTest
@BeforeTest
public
void testD()
{
System.out.println("@BeforeTest
- gets executed only once before
executing 1st test method");
}
@Test
public
void testA() // Test annotation method
{
System.out.println("test
A - stmt-1");
System.out.println("test
A - stmt-2");
}
@Test
public
void testB() // Test annotation method
{
System.out.println("test
B - stmt-1");
System.out.println("test
B - stmt-2");
}
//Define
@AfterTest - annotation method gets executed only once after executing all Test Method
@AfterTest
public
void test()
{
System.out.println("AfterTest
- annotation method gtes executed only once
after executiing all Test Method");
}
}
o/p:
[RemoteTestNG] detected
TestNG version 7.0.0
@BeforeTest - gets executed
only once before execution test method
test A - stmt-1
test A - stmt-2
test B - stmt-1
test B - stmt-2
AfterTest - annotation method gtes executed only once after executiing all Test Method
PASSED: testA
PASSED: testB
===============================================
Default test
Tests run: 2, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
// HW add testc(), write the o/p for above program?
@BeforeMethod :
·
@BeforeMethod
annotation gets executed multiple times before executing each @Test method.
·
Only this method gets
executed multiple times.
·
Remaining all
annotation methods get executed only once.
package TestNgBasics1;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class BeforeMethodAnont1 {
// @Before
test -- only once
//Define
@BeforeMethod annotation - gets
executed multiple times before Executing
Each @Test Method
@BeforeMethod
public
void test() // BeforeMethod annotation method
{
System.out.println("@BeforeMethod anonation - gets executed multiple times for
each Test method ");
}
//Define
@Test
@Test
public
void testA() // Test annotation method
{
System.out.println("test
A - stmt-1");
System.out.println("test
A - stmt-2");
}
}
o/p:
[RemoteTestNG] detected TestNG version 7.0.0
@BeforeMethod anonation -
gets executed multiple times for each Test method
test A - stmt-1
test A - stmt-2
PASSED: testA
===============================================
Default test
Tests run: 1, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
BeforeMethod with
2 @test methods:
package TestNgBasics1;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class BeforeMethodAnont1 {
// @Before
test -- only once
// @BeforeMethod anonation - gets executed multiple times for
each Test method
@BeforeMethod
// Note: BeforeMethod annon gets executed multiple
times
// remaining all annonations will get
executed only once
public void test()
// BeforeMethod annotation method
{
System.out.println("@BeforeMethod anonation - gets executed multiple times for
each Test method ");
}
@Test
public void
testA() // Test annotation method
{
System.out.println("test
A - stmt-1");
System.out.println("test
A - stmt-2");
}
@Test
public void
testB()
{
System.out.println("test
B - stmt-1");
System.out.println("test
B - stmt-2");
}
}
}
o/p:
[RemoteTestNG] detected TestNG version 7.0.0
@BeforeMethod anonation -
gets executed multiple times for each Test method
test A - stmt-1
test A - stmt-2
@BeforeMethod anonation -
gets executed multiple times for each Test method
test B - stmt-1
test B - stmt-2
PASSED: testA
PASSED: testB
===============================================
Default test
Tests run: 2, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
HW Add testC() test method-
and Check the o/p for above program?
@AfterMethod :
package TestNgBasics1;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class AfterMethodAnont1 {
// @after
test -- executed only once
// @@AfterMethod annotation - gets executed multiple times
after executing for each and every Test
method
// Define @afterMethod
@AfterMethod
public void test()
// AfterMethod annotation method
{
System.out.println("@@AfterMethod anonation - gets executed multiple times
after executing for each Test method");
}
@Test
public void
testA() // Test annotation method
{
System.out.println("test
A - stmt-1");
System.out.println("test
A - stmt-2");
}
@Test
public void
testB()
{
System.out.println("test
B - stmt-1");
System.out.println("test
B - stmt-2");
}
}
o/p:
[RemoteTestNG] detected TestNG version 7.0.0
test A - stmt-1
test A - stmt-2
@@AfterMethod anonation -
gets executed multiple times after executing for each Test method
test B - stmt-1
test B - stmt-2
@@AfterMethod anonation -
gets executed multiple times after executing for each Test method
PASSED: testA
PASSED: testB
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
HW Add testC()
test method- and Check the o/p for
above program?
@BeforeMethod and
@AfterMethod Annotations:
package TestNgBasics1;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class BeforeMethodAfterMethodAnnot1 {
//
@after test -- executed only once
// @@AfterMethod anonation - gets executed multiple times
after executing for each Test method
//
Define @AfterMethod
@AfterMethod
public
void test() // BeforeMethod annotation method
{
System.out.println("@@AfterMethod anonation - gets executed multiple times
after executing for each Test method");
}
@Test
public
void testA() // Test annotation method
{
System.out.println("test
A - stmt-1");
System.out.println("test
A - stmt-2");
}
@Test
public
void testB()
{
System.out.println("test
B - stmt-1");
System.out.println("test
B - stmt-2");
}
//
@Before test -- only once
// @BeforeMethod anonation - gets executed multiple times for
each Test method
//
Define @BeforeMethod
@BeforeMethod
public
void test2() // BeforeMethod annotation method
{
System.out.println("@BeforeMethod anonation - gets executed multiple times
before executing for each Test method ");
}
}
o/p:
[RemoteTestNG] detected TestNG version 7.0.0
@BeforeMethod anonation -
gets executed multiple times before executing for each Test method
test A - stmt-1
test A - stmt-2
@@AfterMethod anonation -
gets executed multiple times after executing for each Test method
@BeforeMethod anonation -
gets executed multiple times before executing for each Test method
test B - stmt-1
test B - stmt-2
@@AfterMethod anonation -
gets executed multiple times after executing for each Test method
PASSED: testA
PASSED: testB
===============================================
Default test
Tests run: 2, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
FAQ ***: Tell me some annonations in TestNG?
@Test,
@BeforeTest, @AfterTest
@BeforeMethod,@afterMethod
@BeforeClass , @AfterClass... etc
FAQ ***: Difference between @BeforeTest and @BeforeMethod?
@BeforeTest method gets executed one time or multiple
times--> only once before executing any one Test Method
@BeforeMethod Test gets executed one time or multiple
times--> Multiple times before
executing each Test Method
Note :
Method - means Multiple
times
for each @Test method --
@BeforeMethod @AfterMethod annontations get executed
HW
@BeforeClass and 1 @Test method:
HW @BeforeClass
Annotation with 2 @Test Methods:
o/p:
HW
@AfterClass and 1 @Test method:
HW @AfterClass with
2 @Test Methods:
HW
@BeforeClass and @AfterClass with 2 Test methods:
HW @BeforeSuite :
HW :@AfterSuite
gets executed only once after executing
all test methods
Note:
1.@BeforeMethod, @AfterMethod gets executed multiple times
2. Remaining all annontation methods get executed only once
BeforeTest
AfterTest
BeforeClass
AfterClass
BeforeSuite
AfterSuite
BeforeMethod --Multiple times
AfterMethod -- Multiple times
Sample web
applications for automation practise:
1. Orange HRMS
Demo : HRMS Project -Human Resource management
System -- > Project
https://opensource-demo.orangehrmlive.com/web/index.php/auth/login
Username : Admin
Password : admin123
Employee onboarding -->
Leave details--> Salary
Management --> Bonus management/
salary hike management -->
Exit activities
HW automate -- >
daily some scenarios / some
pages
2. Open MRS: -->
Health Care Domain Project
https://openmrs.org/demo
(OR)
https://o3.openmrs.org/openmrs/spa/login
Click Enter the OpenMRS 3 Demo
(or) https://o3.openmrs.org/openmrs/spa/login
Username: admin
Password: Admin123
manage the hospital related activities
admit into
hospital--> Register patient details
--> consult some Doctor > update details of disease--> Diagnose -> treatment needs surgey -->
medication details (medicines, Injections) --> Pharmacy - take medicines
--> manage Billing activities for
patient > exit clearence activities
once treatment is done.
3. Sample Banking
Project:
https://www.globalsqa.com/angularJs-protractor/BankingProject/#/login
HW Automate some
web test cases 2 or 3 per day ?
Mixed All
Annonations:
// @test , @BeforeSuite ,@BeforeTest, @before
class before method , test
// After method ,Afterclass, Aftertest ,After suite
When we have multiple annonations in class , TestNg gives below
priority for annonations
@BeforeSuite gets executed only once
@BeforeTest - gets executed
only once before execution test method
@BeforeClass gets executed only once
@BeforeMethod -
@Test
@AfterMethod () dont forget
this
@AfterClass
@AfterTest
@AfterSuite
Order of
execution:
BeforeSuite
> BeforeTest > Beforeclass > BeforeMethod > @Test
Aftersuite < AfterTest <
AfterClass AfterMethod < ..
Code:
package TestNGBasics1;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class MixedAnnontations
{
// BeforeSuite >
BeforeTest > Beforeclass > BeforeMethod > @Test
// Aftersuite <
<AfterTest <
AfterClass AfterMethod < ..
//Define
@BeforeSuite
@BeforeSuite
public
void beforeSuite()
{
System.out.println("@@BeforeSuite
gets execued only once");
}
//
Define @BeforeTest
@BeforeTest
public
void testD()
{
System.out.println("@BeforeTest
- gets executed only once before
execution test method");
}
// //
Define @BeforeClass
@BeforeClass
public
void beforeclass()
{
System.out.println("@BeforeClass
gets execued only once");
}
//
Define @BeforeMethod
@BeforeMethod
public
void beforeMethod()
{
System.out.println("@@BeforeMethod
- gets executed multiple times before
executiing each test method");
}
//
Define @Test
@Test
public
void testA() // Test annotation method
{
System.out.println("test
A - stmt-1");
System.out.println("test
A - stmt-2");
}
//
Define @Test
@Test
public
void testB() // Test annotation method
{
System.out.println("test
B - stmt-1");
System.out.println("test
B - stmt-2");
}
//
Define @AfterMethod
@AfterMethod
public
void afterMethod()
{
System.out.println("@@@AfterMethod
- gets executed multiple times after
executiing for each test method");
}
//
Define @AfterClass
@AfterClass
public
void afterClass()
{
System.out.println("@@AfterClass
gets execued only once");
}
//
define @AfterTest
@AfterTest
public
void afterTest()
{
System.out.println("@@AfterTest
- gets executed only once ");
}
//
define @AfterSuite
@AfterSuite
public
void afterSuite()
{
System.out.println("@@@AfterSuite
gets execued only once");
}
}
o/p:
[RemoteTestNG] detected
TestNG version 7.0.0
@@BeforeSuite gets execued only once
@BeforeTest - gets executed
only once before execution test method
@BeforeClass gets execued only once
@@BeforeMethod - gets
executed multiple times before executiing each test method
test A - stmt-1
test A - stmt-2
@@@AfterMethod - gets
executed multiple times after executiing for each test method
@@BeforeMethod - gets
executed multiple times before executiing each test method
test B - stmt-1
test B - stmt-2
@@@AfterMethod - gets
executed multiple times after executiing for each test method
@@AfterClass gets execued only once
@@AfterTest - gets executed
only once
@@@AfterSuite gets execued only once
PASSED: testA
PASSED: testB
===============================================
Default test
Tests run: 2, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
No comments:
Post a Comment