TestNG (Part 3)
Parameters in TestNG
TestNG provides the ability to pass parameters to a test method from the testng.xml file. This allows you to run the same test with different data sets.
To pass parameters to a test method, you need to use the @Parameters annotation in your test class and specify the parameter values in the testng.xml file.
// Java Code
public class TestClass {
@Test
@Parameters({"param1", "param2"})
public void testMethod(String param1, int param2) {
// your test logic
}
}
// testng.xml
<test name="My Test">
<parameters>
<parameter name="param1" value="value1"/>
<parameter name="param2" value="100"/>
</parameters>
<classes>
<class name="TestClass"/>
</classes>
</test>
In this example, the testMethod is annotated with the @Parameters annotation and specifies two parameters: "param1" and "param2". The parameter values are specified in the testng.xml file under the <parameters> tag. When the test is run, the values of "param1" and "param2" will be passed to the testMethod as arguments.
1. DataFactory
TestNG factory is used to create instances of test classes dynamically. This is useful if you want to run the test class any no of times.
For example, if you have a test to login into a site and you want to run this test multiple times,then its easy to use TestNG factory where you create multiple instances of test class and run the tests.
public class WebTestFactory {
//createInstances method will create 10 objects of WebTest class
@Factory
public Object[] createInstances() {
Object[] result = new Object[10];
for (int i = 0; i < 10; i++) {
result[i] = new WebTest(i);
}
return result;
}2. Provide in xml
You can provide parameters in XML files and use them in tests; these are global-level parameters like ex. URL in XML -->
<parameter name="url" value="www.md.com"/>
<parameter name ="uname" value="md"/>
Code -->
@Parameters({"url","key"})
@Test()
public void meth1(String url, String key){
}3. Provide in test
@Dataprovider
public void getData(){
Object[][] data = new Object[][]
data[0][0] = "one"
data[0][1] = "two"
data[1][0] = "one"
data[1][1] = "two"
return data;
}
//using data >>
@Test(dataprovider = "getData" )
public void testone(string u1, string u2) --catch the data here
--test will run for number of iterations of data is presentHow to use dependsonMethods ?
@Test(dependsonMethods="method1")
public void method2(){}
here method1 will get executed firstHow to Skip a test?
@Test(enabled = false)What is timeout in TestNG?
In TestNG, the timeout attribute is used to specify the maximum amount of time a test method should take to complete. If the test method takes longer than the specified timeout, it will be marked as failed.
The timeout attribute can be specified at the test level or the method level in the testng.xml file.
Example:
@Test(timeout = 4000)
<test name="My Test" timeout="10">
<classes>
<class name="TestClass1">
<methods>
<include name="testMethod1"/>
<include name="testMethod2" timeout="5"/>
</methods>
</class>
</classes>
</test>
In this example, the timeout value of 10 seconds is specified at the test level. This means that any test method within the test should complete within 10 seconds. However, the timeout value for testMethod2 is overridden to 5 seconds. This means that testMethod2 must complete within 5 seconds, regardless of the timeout value specified at the test level.
Parallel execution in TestNG
Parallel execution in TestNG allows multiple tests to run simultaneously. This can greatly reduce the time it takes to run a large suite of tests. In TestNG, you can achieve parallel execution in the following ways:
- By Class: You can run all the test methods in a single test class in parallel.
- By Methods: You can run individual test methods in parallel.
- By Suite: You can run all the test methods in all the test classes in a single suite in parallel.
To enable parallel execution in TestNG, you need to specify the parallel attribute in the testng.xml file. The attribute can have the following values:
- "methods": Parallel execution of individual test methods.
- "classes": Parallel execution of test methods within each test class.
- "tests": Parallel execution of entire test classes.
just add this in xml file in suite tag- parallel="tests" thread-count="2"
<suite name="TestAll" parallel="tests" thread-count="2">
<test name="order">
<classes>
<class name="com.mkyong.testng.examples.suite.TestConfig">
<methods>
<method>
<exclude name = "exclude_method_name.*"/>
</method>
</methods>
</class>
<class name="com.mkyong.testng.examples.suite.TestOrder" />
</classes>
</test>
<test name="database">
<classes>
<class name="com.mkyong.testng.examples.suite.TestConfig" />
<class name="com.mkyong.testng.examples.suite.TestDatabase" />
</classes>
</test>
</suite>How to include/exclude methods in TestNG?
TestNG provides the ability to include or exclude specific test methods by using the include and exclude attributes in the testng.xml file.
To include a specific test method, use the following syntax in the testng.xml file:
<include name="testMethodName"/>
To exclude a specific test method, use the following syntax in the testng.xml file:
<exclude name="testMethodName"/>
By default, all test methods in the specified test class will be run unless otherwise specified using the include or exclude attributes.
How to run only specified test cases in TestNG?
To run only specified test cases in TestNG, you can use the following methods:
- Use the
<include>tag in the testng.xml file (described above) - Use the TestNG annotations
@Testandenabled:
public class TestClass {
@Test(enabled=false)
public void testMethod1() {
// your test logic
}
@Test
public void testMethod2() {
// your test logic
}
}
3. Use the group feature:
//tag your tests
@Test(group={"smoke"})
public void method2(){}
//xml
<suite name="TestAll" parallel="tests" thread-count="2">
<listeners>
<listener class-name="pkg.classname"/>
</listensers>
<test name= "smoke test">
<parameter name="url" value="www.md.com"/>
<parameter name ="uname" value="md"/>
<groups>
<run>
<include name = "smoke"/>
<exclude name = "group name"/>
<run/>
<groups/>
<classes>
<class name ="pkg.classname">
<classes/>
</test>
</suite>TestNG parallel Execution on different browsers?
<suite name="Parallel Testing" parallel="tests" thread-count="2">
<test name="Firefox testing">
<parameter name="BrowserType" value="firefox">
</parameter>
<classes>
<class name="testng.ParallelTest"/>
</classes>
</test>
<test name="Chrome testing">
<parameter name="BrowserType" value="chrome">
</parameter>
<classes>
<class name="testng.ParallelTest"/>
</classes>
</test>
</suite>TestNG flow of execution:
<suite name="TestAll">
<test name="order">
<classes>
<class name="com.mkyong.testng.examples.suite.TestConfig" />
<class name="com.mkyong.testng.examples.suite.TestOrder" />
</classes>
</test>
<test name="database">
<classes>
<class name="com.mkyong.testng.examples.suite.TestConfig" />
<class name="com.mkyong.testng.examples.suite.TestDatabase" />
</classes>
</test>
</suite>
____________________________________________________________________________
testBeforeSuite()
testBeforeTest()
testFindOrder
testMakeEmptyOrder
testMakeOrder
testUpdateOrder
testAfterTest()
testBeforeTest()
testConnectMongoDB
testConnectMsSQL
testConnectMySQL
testConnectOracle()
testAfterTest()
testAfterSuite()