Get test class name from which data provider method is called in TestNG

161 views
Skip to first unread message

Nisarg Dave

unread,
Dec 24, 2019, 4:38:33 AM12/24/19
to testng-dev

I am using TestNG @DataProvider Annotation along with @Factory functionality in order to run a flow. When the control goes to @DataProvider method, I want to know from which test class this dataprovider method was called & on basis of that I want to supply test data.

This is my Bean class:

public class Bean {

    private String fName = null;
    private String lName = null;
    private String address = null;

    public synchronized String getfName() {
        return fName;
    }

    public synchronized void setfName(String fName) {
        this.fName = fName;
    }

    public synchronized String getlName() {
        return lName;
    }

    public synchronized void setlName(String lName) {
        this.lName = lName;
    }

    public synchronized String getAddress() {
        return address;
    }

    public synchronized void setAddress(String address) {
        this.address = address;
    }
}

This is my constructor of TestClass with @Factory

@Factory(dataProvider = "dataProvider")
    public TestClass(Bean bean) {

        this.fName = bean.getfName();
        this.lName = bean.getlName();
        this.address = bean.getAddress();
    } 

Here is my DataProvider:

    @DataProvider(name = "dataProvider")
    public static Object[][] dataProvider() {

        Bean oneBean = new Bean();
        oneBean.setfName("Nisarg");
        oneBean.setlName("Dave");
        oneBean.setAddress("India");

        Bean secondBean = new Bean();
        secondBean.setfName("ABCD");
        secondBean.setlName("XYZ");
        secondBean.setAddress("UK");

        return new Object[][] { 
                                    new Object[] { oneBean },
                                    new Object[] { secondBean }
                              };
    }

Here in my DataProvider I know I can pass Method m or ITestContext as parameters, but both of them are not giving me Test class Name from which it was called.


The order in which the execution will run (for annotated methods) in TestNG is: 

@DataProvider
@Factory
@BeforeClass
@BeforeMethod
@Test

And, one more thing is: I am not applying dataProvider attribute in each @Test test case, because I am looking to run one entire flow in particular sequence:

Lets consider an example of Facebook application: Sign up --> Login --> Send a friend Request --> Logout. These 4 are functionalities (in short 4 @Test in a Test Class)

So if I pass 3 dataproviders to each @Test method, then they will get executed in below order:

Sign up --> Sign Up --> Sign Up
Login --> Login --> Login
Send a friend Request --> Send a friend Request --> Send a friend Request
Logout --> Logout --> Logout

However, I want to run in in below Sequence for 3 different Data providers:

Sign up --> Login --> Send a friend Request --> Logout
Sign up --> Login --> Send a friend Request --> Logout
Sign up --> Login --> Send a friend Request --> Logout

So, I am using @Factory annotation. So, now the issue is in my DataProvider method, I want to know, from which test class, this Data provider method was invoked, so I will provide the data accordingly.

I know I can add parameters in dataprovider method as: Method m and ITestContext. But as DataProvider is supplied to @Factory method, Method m comes null & ITestContext also doesn't have class related info from which it was called. 


Can anyone please help on this?

⇜Krishnan Mahadevan⇝

unread,
Dec 25, 2019, 9:37:23 PM12/25/19
to testn...@googlegroups.com
You just need to pass in an ITestNGMethod object as a parameter to your "@DataProvider" method. TestNG will natively inject a TestNG test method object into your data provider via which you should be able to extract out whatever you need.

Here's a sample

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.ITestNGMethod;
import org.testng.annotations.DataProvider;

public class DataProviderHouse {

@DataProvider(name = "dataProvider")
public static Object[][] dataProvider(ITestNGMethod method) {
Class clazz = method.getRealClass();
Method tm = method.getConstructorOrMethod().getMethod();
Constructor constructor = method.getConstructorOrMethod().getConstructor();
String name = clazz.getName() + ".";
if (tm == null) {
name = constructor.getName();
} else {
name = name + tm.getName();
}

System.err.println("Data provider is being invoked by " + name);
Bean oneBean = new Bean("Master Oogway", 75);
Bean secondBean = new Bean("Master Shifu", 60);


return new Object[][] {new Object[] {oneBean}, new Object[] {secondBean}};
}
}
public class Bean {

private final String name;
private final int age;

public Bean(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}
}

import org.testng.Assert;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class TestClass {

private final String name;
private final int age;

@Factory(dataProvider = "dataProvider", dataProviderClass = DataProviderHouse.class)
public TestClass(Bean bean) {
this.name = bean.getName();
this.age = bean.getAge();
}

@Test
public void testName() {
Assert.assertNotNull(name);
}

@Test
public void testAge() {
Assert.assertTrue(age > 0 && age <= 100);
}

@Override
public String toString() {
return "TestClass{" + "name='" + name + '\'' + ", age=" + age + '}';
}
}




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 Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-dev/45720e34-a52e-4cea-895d-1b646980ab4a%40googlegroups.com.

Nisarg Dave

unread,
Dec 26, 2019, 10:04:33 AM12/26/19
to testng-dev
Hey Krishnan,

You know, you are awesome ?

Please do know that you have helped me to solve an issue in which I was stuck by a week or so. Thanks a ton !

Because of you guys, the community believes in to hack around TestNG!
Thanks very much !

Regards,
Nisarg

Reply all
Reply to author
Forward
0 new messages