TestNG provides a feature to run all the test from a single configuration xml file.
We need to create testng.xml file in Eclipse from Menu File>New>File.
Following is the sample testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Nopackage" >
<classes>
<class name="testSuite.TestCases" /><!--You can get this name from Run Configuration under Run Menu by Clicking Browse button for class-->
</classes>
</test>
</suite>
After creating testng.xml file we need to provide configuration in Run
To create run configuration go to Run >> Run Configuration. Here you will find TestNG option in left navigation. Right click same option and select New
Refer Below screenshot
Provide the Name, Class and Suite by clicking Browse button as shown in the following image
Now Click on Apply and Click Run.
Your TestNg Tests will start running.
Now we have configured testng.xml file for the project.
We Can create a bat file that will refer testng.xml file to run all the test from directly clicking bat file.
Create a new bat file using the following snippet shown below
cd C:\workspace\projectname
set ProjectPath=C:\workspace\projectname
echo %ProjectPath%
set classpath=%ProjectPath%\bin;%ProjectPath%\lib\*
echo %classpath%
java org.testng.TestNG %ProjectPath%\testng.xml
In this way there is no need to open Eclipse again and again to run the tests we can directly call the above bat file that will directly run the tests and also it will generate test-output folder containing all the results of test execution.
Other way to Create TestNG.xml
Right Click on the package and Select Convert To TesNB from TestNG Menu
As an Example
This is the testng.xml (where the architecture of tesng is Suite Test Class )
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="com.gurpreet.pages.Test3"/>
<class name="com.gurpreet.pages.Test4"/>
<class name="com.gurpreet.pages.Test1"/>
<class name="com.gurpreet.pages.Test2"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Here first Test3 class will be called as it is occuring first in testng.xml mentioned above
The Following TestNG is for handling test cases at Package Level
Here architecture is Suite Test Package,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<packages>
<package name="com.gurpreet.pages.*"></package>
</packages>
</test> <!-- Test -->
</suite> <!-- Suite -->
Or
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<packages>
<package name="com.gurpreet.pages.*"/>
</packages>
</test> <!-- Test -->
</suite> <!-- Suite -->
Note that you can close the tag of Class or Package like following
<package name="com.gurpreet.pages.*"/>
We can exclude and include Test Cases
To Include methods or Test Cases in class
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="com.gurpreet.pages.Test1">
<methods>
<include name="function1Test1"></include>
</methods>
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
To exclude methods or Test Cases in class
Note here methods is used to highlight test cases to be considered from class name
So type methods tag between classes
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="com.gurpreet.pages.Test1">
<methods>
<exclude name="function1Test1"></exclude>
</methods>
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
If you want to run one test case from 100 test cases
you can write include test case or method name
or preserve-order="true"
In such case only that statement will run
Grouping in TestNG.xml
You write group before class because classes will have the group, so before testng picks the class u need to specify which groups from the class need to be called.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none"> //Revise parallel can be tests, methods or classes refer
http://www.seleniumeasy.com/testng-tutorials/parallel-execution-of-test-methods-in-testng
<test name="Test">
<groups> //Revise
<run>
<exclude name="my"></exclude>
</run>
</groups>
<classes>
<class name="com.gurpreet.pages.Test1">
<methods>
<include name="function2Test1"></include>
</methods>
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Presevre Order of Execution in TestNG (//Revise)
preserve-order="true" or preserve-order="false" //Revise
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none" preserve-order="true">
<test name="Test">
<groups>
<run>
<exclude name="my"></exclude>
</run>
</groups>
<classes>
<class name="com.gurpreet.pages.Test1"/>
<class name="com.gurpreet.pages.Test4"/>
<class name="com.gurpreet.pages.Test2"/>
<class name="com.gurpreet.pages.Test3"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
OutPut
[TestNG] Running:
C:\Users\NEERAJ\workspace\MortalGlobe\src\test\java\com\gurpreet\pages\secondtestng.xml
In Function1 of test 1 class
In Function1 of test 4 class
In Function1 of test 2 class
In Function1 of test 3 class
===============================================
Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================
To Call Multiple TestNG.XML (//Revise)
Use suite-files, as u r telling suit that number of TestNG.XML are present in the suite
Here we write at Suit Level and we dont include test and class.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suitedfs" parallel="none">
<suite-files>
<suite-file path="C:\Users\NEERAJ\workspace\MortalGlobe\src\main\java\com\gurpreet\project\firsttestng.xml"></suite-file>
</suite-files>
</suite> <!-- Suite -->
In this way there is no need to open Eclipse again and again to run the tests we can directly call the above bat file that will directly run the tests and also it will generate test-output folder containing all the results of test execution.
Other way to Create TestNG.xml
Right Click on the package and Select Convert To TesNB from TestNG Menu
As an Example
This is the testng.xml (where the architecture of tesng is Suite Test Class )
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="com.gurpreet.pages.Test3"/>
<class name="com.gurpreet.pages.Test4"/>
<class name="com.gurpreet.pages.Test1"/>
<class name="com.gurpreet.pages.Test2"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
The Following TestNG is for handling test cases at Package Level
Here architecture is Suite Test Package,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<packages>
<package name="com.gurpreet.pages.*"></package>
</packages>
</test> <!-- Test -->
</suite> <!-- Suite -->
Or
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<packages>
<package name="com.gurpreet.pages.*"/>
</packages>
</test> <!-- Test -->
</suite> <!-- Suite -->
Note that you can close the tag of Class or Package like following
<package name="com.gurpreet.pages.*"/>
We can exclude and include Test Cases
To Include methods or Test Cases in class
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="com.gurpreet.pages.Test1">
<methods>
<include name="function1Test1"></include>
</methods>
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
To exclude methods or Test Cases in class
Note here methods is used to highlight test cases to be considered from class name
So type methods tag between classes
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="com.gurpreet.pages.Test1">
<methods>
<exclude name="function1Test1"></exclude>
</methods>
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
If you want to run one test case from 100 test cases
you can write include test case or method name
or preserve-order="true"
In such case only that statement will run
or preserve-order="true"
In such case only that statement will run
Grouping in TestNG.xml
You write group before class because classes will have the group, so before testng picks the class u need to specify which groups from the class need to be called.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none"> //Revise parallel can be tests, methods or classes refer
http://www.seleniumeasy.com/testng-tutorials/parallel-execution-of-test-methods-in-testng
http://www.seleniumeasy.com/testng-tutorials/parallel-execution-of-test-methods-in-testng
<test name="Test">
<groups> //Revise
<run>
<exclude name="my"></exclude>
</run>
</groups>
<classes>
<class name="com.gurpreet.pages.Test1">
<methods>
<include name="function2Test1"></include>
</methods>
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Presevre Order of Execution in TestNG (//Revise)
preserve-order="true" or preserve-order="false" //Revise
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none" preserve-order="true">
<test name="Test">
<groups>
<run>
<exclude name="my"></exclude>
</run>
</groups>
<classes>
<class name="com.gurpreet.pages.Test1"/>
<class name="com.gurpreet.pages.Test4"/>
<class name="com.gurpreet.pages.Test2"/>
<class name="com.gurpreet.pages.Test3"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
OutPut
[TestNG] Running:
C:\Users\NEERAJ\workspace\MortalGlobe\src\test\java\com\gurpreet\pages\secondtestng.xml
In Function1 of test 1 class
In Function1 of test 4 class
In Function1 of test 2 class
In Function1 of test 3 class
===============================================
Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================
Use suite-files, as u r telling suit that number of TestNG.XML are present in the suite
Here we write at Suit Level and we dont include test and class.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suitedfs" parallel="none">
<suite-files>
<suite-file path="C:\Users\NEERAJ\workspace\MortalGlobe\src\main\java\com\gurpreet\project\firsttestng.xml"></suite-file>
</suite-files>
</suite> <!-- Suite -->
No comments:
Post a Comment