How to execute same @Test method multiple times with different parameters, without using a dataprovider?

12,806 views
Skip to first unread message

Alexander Poulikakos

unread,
Nov 17, 2012, 6:55:12 PM11/17/12
to testng...@googlegroups.com
How can I execute same @test method twice, but with different parameters? Without using a DataProvider?

I would like to be able to specify my testng.xml something like below. How can I achieve this? I need the tests to be run on same instance, so I must put them within same <test> tag.

====================================
<suite name="my-suite">
<test name="my-test">
<classes>
<class name="testng.MultipleTest">
<methods>
<include name="m1">
<parameter name="testCaseId" value="0001" />
</include>
<include name="m1">
<parameter name="testCaseId" value="0002" />
</include>
</methods>
</class>
</classes>
</test>
</suite>
====================================
package testng;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class MultipleTest {
@Test
@Parameters("testCaseId")
public void m1(String testCaseId) throws Exception {
System.out.println("testCaseId: " + testCaseId);
}
}

Krishnan Mahadevan

unread,
Nov 17, 2012, 9:27:48 PM11/17/12
to testng...@googlegroups.com
AFAIK, you cannot do this via a TestNG xml filr. DataProviders are meant for such scenarios. Why wouldnt you not be able to use a DataProvider?
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/S7KeJxDJ4c8J.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

nagarajan raman

unread,
Nov 18, 2012, 2:36:33 AM11/18/12
to testng...@googlegroups.com
HI,

As Krishnan specified, Dataprovider would be the right approach for your scenarion.

Any how , can you modify your .xml as below.

<suite name="my-suite">
<test name="my-test">
<classes>
<class name="testng.MultipleTest">
<methods>
<parameter name="testCaseId" value="0001" />
                                       <include name="m1"/>
                                       <parameter name="testCaseId" value="0002" />
<include name="m1"/>

</methods>
</class>
</classes>
</test>
</suite>

Thanks,
Raj

Alexander Poulikakos

unread,
Nov 18, 2012, 4:19:51 PM11/18/12
to testng...@googlegroups.com
We have a test management tool, containing the requirements that needs to be tested. Each requirement points to the class and method (with the correct parameters to cover the requirement). Same Class/method can be used to cover many requirements (so the method parameters decides exactly which requirement to test). Each requirement has a unique id.

Within the test management tool, one can create suites of requirements. From this suite a testng.xml file can be generated and then passed to TestNG for execution. The requirement id is passed to the test method so verdict can be sent back to the test management tool for the corresponding requirement.

That is why I would like to have the generated testng.xml look something like the below. Additionally, all users of this testcase might not have a test management tool, they might want to create the xml file manually.

How would a dataProvider solve this?

<suite name="my-suite">
<test name="my-test">
<classes>
<class name="testng.MultipleTest">
<methods>
<include name="m1">
<parameter name="reqId" value="0001" />
<parameter name="param1" value="value111" />
<parameter name="param2" value="value222" />
</include>
<include name="m1">
<parameter name="reqId" value="0002" />
<parameter name="param1" value="value333" />
<parameter name="param2" value="value444" />
</include>
</methods>
</class>
</classes>
</test>
</suite>



/Alex

Alexander Poulikakos

unread,
Nov 18, 2012, 4:23:13 PM11/18/12
to testng...@googlegroups.com
No, this does not work.

Krishnan Mahadevan

unread,
Nov 22, 2012, 11:57:10 AM11/22/12
to testng...@googlegroups.com
Binesh, i dont think you can do this without a data provider. 

On Thursday, November 22, 2012, Binesh Subramanian wrote:
Hi,
We are also facing the same issue. any update on this would be really helpful.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/1DH9YiKmQwsJ.

To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

niharika varshney

unread,
Nov 23, 2012, 12:51:35 AM11/23/12
to testng...@googlegroups.com
Would making multiple tests in your testng.xml help as below?

<test name="Test1" preserve-order="false">
  <parameter name="param1" value="Test1 with param"/>  
    <classes> 
      <class name="com.test.ParameterTests"> 
      </class>
    </classes> 
</test>
 <test name="Test2" preserve-order="false">
  <parameter name="param1" value="Test2 with param"/>  
    <classes> 
      <class name="com.test.ParameterTests"> 
      </class>
    </classes> 
</test>  

Regards,

Dharshana kasun Warusavitharana

unread,
Nov 29, 2012, 12:27:15 AM11/29/12
to testng...@googlegroups.com
Hi, 

I did a simple solution to this. My specification wa to run the whole suite with different specifications. In both cases im skipping not applicable tests using a custom annotation using MethodInterceptor.

For trigger the duplicated suite i simply call following set of codes in ISuiteListner class
 just passing the suite info to the TestNg runner

 TestNG testNG = superSuite("MixedModeSuite-" + suite.getName(), suiteVariablesList);
                  testNG.run();

And bit of java to avoid duplications and custom reporting.

This is suppose to be a some kind of hack but it worked,

Thank You,
Dharshana.
--
...................................................
Dharshana Kasun Warusavitharana
Faculty of Engineering Technology
The Open University of Sri Lanka

Visit my blog -    http://dkasunw.blogspot.com
Contact me on skype  - dkasunw
Follow me at - http://twitter.com/dkasunw

_______________Change the World ,Beat the world ,Inspired by world but never let the world Change you!____________________



Swastik Ganguly

unread,
Mar 19, 2015, 11:35:50 AM3/19/15
to testng...@googlegroups.com
Hi Group members,

Is anyone looking into this.
Multiple same parameter call without using multiple <test></test>.

If any solution please share the link.

Thanks,

Krishnan Mahadevan

unread,
Mar 20, 2015, 1:19:40 AM3/20/15
to testng...@googlegroups.com
The subject of this post and your response are a bit confusing. Please elaborate your problem in detail.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.

Swastik Ganguly

unread,
Mar 20, 2015, 2:47:42 AM3/20/15
to testng...@googlegroups.com
I came across the same known issue, if any solution please let know,
Thanks in advance.

Parameter Param1 is Re-used multiple times, with different values in same test tag.
How to achieve this without different test tag?

<Test name="test1" preserve-order=true>
<classes>
<class name="package.classname">
<include name="method name1">
<parameter name="param1" value="value1">
<parameter name="param2" value="value2">
</include>
<include name="method name2">
<parameter name="param1" value="value1.1">
<parameter name="param3" value="value3">
</include>
<class>
</classes>
</test>

--
You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/oVpyV-nRbFU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.

Krishnan Mahadevan

unread,
Mar 20, 2015, 2:56:29 AM3/20/15
to testng...@googlegroups.com
Would the following work for you instead ?

Create a Factory method which is powered by a data provider. The data provider provides values for every instance that the Factory method creates and the @Test annotated test methods in the Test Class use the parameters that were fed into every test class and work with it.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Alexander Poulikakos

unread,
Mar 28, 2015, 10:04:57 AM3/28/15
to testng...@googlegroups.com
Hi
 
I did a solution (for my previous employer) for this, using a generic DataProvider that reads the values from the testng.xml file and feeds the @Test methods with the correct parameters. If you’re interested I could re-write it and open-source it?
 
Regards,
Alex

vij...@gmail.com

unread,
Sep 2, 2016, 11:45:03 AM9/2/16
to testng-users
Hello All,

I have a similar Requirement wherein I want to login agents on multiple machines with the same set of code sequentially in the same test case.
I have testing and selenium grid In place.

Could any one suggest how can I go about it, so to explain the test in detail:
Step 1 : Login to machine 1 and launch agent client webpage
Step 2: Login to machine 2 and launch agent client webpage
Step 3: Login agent on machine1
Step 4 : Login agent on machine 2

All the above steps in the same test case, I have methods of launching webclient and also loggin in the agent

Thank you,
Viji

⇜Krishnan Mahadevan⇝

unread,
Sep 2, 2016, 12:20:01 PM9/2/16
to testng...@googlegroups.com
You can use a Factory that is powered by a data provider.

Something like this maybe ?

public class Sample {
private int i;

@Factory(dataProvider = "dp")
public Sample(int i) {
this.i = i;
}

@Test
public void test1() {
//do something with "i"
}

@Test
public void test2() {
//do something else with "i"
}

@DataProvider(name = "dp")
public static Object[][] getData() {
return new Object[][] {
{1},
{2},
{3}
};
}
}

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.

vij...@gmail.com

unread,
Sep 8, 2016, 5:59:09 AM9/8/16
to testng-users
Hi Krishnan,

Thanks for the response.
I tried something with loggin into gmail on similar lines, following is the code:

package com.scripts;

import com.frame.*;
import com.lib.EnvSetup;
import com.lib.InitLog4j;
import com.lib.SuiteListener;
import com.lib.UtilityFun;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;
@Listeners({ SuiteListener.class})
public class Gmail_Login_TesCase2 extends Gmail_Login_Template{
    // Instantiate Page class, utility and test environment objects
    EnvSetup EnvObj = new EnvSetup();
    UtilityFun utilObj = new UtilityFun();
    protected Logger log = Logger.getLogger(this.getClass().getName());
    private String username;
    private String password;
    private String applicationName;
    public static RemoteWebDriver WEBDRIVER;
    protected GmailLoginPage GmailLoginPage;

    @Factory (dataProvider="dp")
    public Gmail_Login_TesCase2(String username, String password,String applicationName){
        this.username=username;
        this.password = password;
        this.applicationName = applicationName;
  
    }

    @Test (description="Gmail Login")
    public void Gmail_Login() throws MalformedURLException {
     
        // Read Test Data
        log.info("Logging in Gmail");
   
        DesiredCapabilities capability = DesiredCapabilities.chrome();
        capability.setBrowserName("chrome");
  capability.setCapability("applicationName",applicationName);
        WEBDRIVER = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
        GmailLoginPage = PageFactory.initElements(WEBDRIVER, GmailLoginPage.class);
       
        log.info("Entering into GMAIL");

        log.info("Entering into GMAIL");
        try{
         
       
        WEBDRIVER.get("http://www.gmail.com");
        WEBDRIVER.findElementById("Email").sendKeys(username);
        WEBDRIVER.findElementById("next").click();
       
        System.out.println(password);
        WEBDRIVER.findElementById("Passwd").sendKeys(password);
        WEBDRIVER.findElementById("signIn").click();
       
        }
   
        catch (Exception e) {
            log.info(e.getMessage());
            }
    
       
    }
   
    @DataProvider(name = "dp")
    public static Object[][] getData() {
        return new Object[][] {
                {new String("XXXX"),new String("XXXX"),new String("agent1")},
                {new String("XXXX"),new String("XXXX"),new String("agent2")}
 
        };
  
 }

 @Override
 public void performBeforeMethodOperation() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void performBeforeSuiteOperation() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void performAfterMethodOperation() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void performAfterSuiteOperation() {
  // TODO Auto-generated method stub
  
 }
   
}
 
And the testng xml looks like:


<suite name="Sample test suite" >
 <test name="Gmail_Login" preserve-order="true">
   <classes>
    <class name="com.scripts.Gmail_Login_TesCase2">
    </class>
   </classes>
 </test>  
</suite>
After the run, the test results showed that:


===============================================
Sample test suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================

Two queries:
1) Again the purpose of running under same test case is not solved, as the above run shows that two test cases are run, but I want the run this as a single test case
2) Secondly, if I want to do further operations in the individual remote WebDriver, how can I identify the remotewebdriver on each machine after it has been launched and work on it.

Thank you,
Viji Iyer

⇜Krishnan Mahadevan⇝

unread,
Sep 9, 2016, 11:39:15 PM9/9/16
to testng...@googlegroups.com
Viji,
I don't quite understand what you are looking for in the below two queries :
  • Again the purpose of running under same test case is not solved, as the above run shows that two test cases are run, but I want the run this as a single test case
A test case that runs multiple times with different data would be visualised as if it was a different test only ?
If you are not keen on doing that, then wrap your entire test method inside a for loop which iterates over different data and runs the same code. The flip-side is, if your first data iteration fails, the rest of the iterations will not run. If you wrap within a try..catch block to capture error and move on, then you also would be required to write custom code that introspects if there were any errors and finally raises an exception etc., so that TestNG can fail your test method.
  • Secondly, if I want to do further operations in the individual remote WebDriver, how can I identify the remotewebdriver on each machine after it has been launched and work on it.
Not sure what you mean here. What do you mean how can I identify the remotewebdriver on each machine after it has been launched ? If your test class instance basically is responsible for generating its remotewebdriver instance, then all the tests inside it would have access to it as a data member. That is exactly what my test example demonstrates.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages