TestNG
TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
1. Create project in Eclispse
2. Install TestNG plugin Ecliplse marketplace
3. Include selenium TestNG jar
4. Create class 1 and and class 2 as below
5. Run the test suit using TestNG suite
Class1.java
---------------------------
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Class1 {
@BeforeClass()
public void beforeclass(){
System.out.println(" Beforecall from class 1");
System.out.println("-------------------------------------");
}
@BeforeMethod
public void welcome(){
System.out.println("Open Browser-- Beforemethod is called in class 1");
}
@Test
public void Register(){
// throw new SkipException("Skipping the test");
System.out.println("Registering user - Test 1");
}
@Test
public void Login(){
System.out.println("Test Login - Test 2");
}
@Test
public void change_password(){
System.out.println("Password change - Test 3");
}
@AfterMethod
public void logout(){
System.out.println("Close Browser - Aftermethod is called in class 1");
System.out.println("------------------------------");
}
@AfterClass
public void after_class(){
System.out.println("destroy driver in call 1");
System.out.println("-------------------------------------");
}
}
Class2.java
----------------------
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Class2 {
@BeforeSuite
public void beforesuit(){
System.out.println("@BeforeSuite - written in class 2");
}
@BeforeTest
public void beforetest(){
System.out.println("This is before test - @BeforeTest written in class 2");
}
@BeforeClass()
public void beforeclass(){
System.out.println(" @beforeClass- Initialize driver for class 2");
}
@Test
public void add_user(){
System.out.println(" Test 1 in class 2");
}
@AfterClass
public void after_class(){
System.out.println("@AfterClass - destroy driver in call 2");
System.out.println("-------------------------------------");
}
@AfterSuite
public void aftersuit(){
System.out.println("@AfterSuite - written in class 2");
}
}
OP:
@BeforeSuite - written in class 2
This is before test - @BeforeTest written in class 2
Beforecall from class 1
-------------------------------------
Open Browser-- Beforemethod is called in class 1
Test Login - Test 2
Close Browser - Aftermethod is called in class 1
------------------------------
Open Browser-- Beforemethod is called in class 1
Registering user - Test 1
Close Browser - Aftermethod is called in class 1
------------------------------
Open Browser-- Beforemethod is called in class 1
Password change - Test 3
Close Browser - Aftermethod is called in class 1
------------------------------
destroy driver in call 1
-------------------------------------
@beforeClass- Initialize driver for class 2
Test 1 in class 2
@AfterClass - destroy driver in call 2
-------------------------------------
[Utils] Attempting to create F:\workplace\TestNg\test-output\Default suite\Default test.xml
[Utils] Directory F:\workplace\TestNg\test-output\Default suite exists: true
PASSED: Login
PASSED: Register
PASSED: change_password
PASSED: add_user
===============================================
Default test
Tests run: 4, Failures: 0, Skips: 0
===============================================
@AfterSuite - written in class 2
===============================================
Default suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================
TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
1. Create project in Eclispse
2. Install TestNG plugin Ecliplse marketplace
3. Include selenium TestNG jar
4. Create class 1 and and class 2 as below
5. Run the test suit using TestNG suite
Class1.java
---------------------------
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Class1 {
@BeforeClass()
public void beforeclass(){
System.out.println(" Beforecall from class 1");
System.out.println("-------------------------------------");
}
@BeforeMethod
public void welcome(){
System.out.println("Open Browser-- Beforemethod is called in class 1");
}
@Test
public void Register(){
// throw new SkipException("Skipping the test");
System.out.println("Registering user - Test 1");
}
@Test
public void Login(){
System.out.println("Test Login - Test 2");
}
@Test
public void change_password(){
System.out.println("Password change - Test 3");
}
@AfterMethod
public void logout(){
System.out.println("Close Browser - Aftermethod is called in class 1");
System.out.println("------------------------------");
}
@AfterClass
public void after_class(){
System.out.println("destroy driver in call 1");
System.out.println("-------------------------------------");
}
}
Class2.java
----------------------
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Class2 {
@BeforeSuite
public void beforesuit(){
System.out.println("@BeforeSuite - written in class 2");
}
@BeforeTest
public void beforetest(){
System.out.println("This is before test - @BeforeTest written in class 2");
}
@BeforeClass()
public void beforeclass(){
System.out.println(" @beforeClass- Initialize driver for class 2");
}
@Test
public void add_user(){
System.out.println(" Test 1 in class 2");
}
@AfterClass
public void after_class(){
System.out.println("@AfterClass - destroy driver in call 2");
System.out.println("-------------------------------------");
}
@AfterSuite
public void aftersuit(){
System.out.println("@AfterSuite - written in class 2");
}
}
OP:
@BeforeSuite - written in class 2
This is before test - @BeforeTest written in class 2
Beforecall from class 1
-------------------------------------
Open Browser-- Beforemethod is called in class 1
Test Login - Test 2
Close Browser - Aftermethod is called in class 1
------------------------------
Open Browser-- Beforemethod is called in class 1
Registering user - Test 1
Close Browser - Aftermethod is called in class 1
------------------------------
Open Browser-- Beforemethod is called in class 1
Password change - Test 3
Close Browser - Aftermethod is called in class 1
------------------------------
destroy driver in call 1
-------------------------------------
@beforeClass- Initialize driver for class 2
Test 1 in class 2
@AfterClass - destroy driver in call 2
-------------------------------------
[Utils] Attempting to create F:\workplace\TestNg\test-output\Default suite\Default test.xml
[Utils] Directory F:\workplace\TestNg\test-output\Default suite exists: true
PASSED: Login
PASSED: Register
PASSED: change_password
PASSED: add_user
===============================================
Default test
Tests run: 4, Failures: 0, Skips: 0
===============================================
@AfterSuite - written in class 2
===============================================
Default suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================
No comments:
Post a Comment