-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
supporting parameters at method level? #823
Comments
No, you should use a |
@cbeust, the challenge with |
You can define a limited amount of parameters in your http://testng.org/doc/documentation-main.html#parameters-testng-xml Cédric On Mon, Oct 12, 2015 at 9:03 PM, JayVem notifications@github.com wrote:
|
@cbeust Aren't these parameters at suite level? What if I wanted to create a complex data type at method level. For instance, consider this code...
The Employee parameter that is being sent to calculateBenefits method has to be created from the database. How would I achieve this? I would like to pass a primary key and inside the calculateBenefits method, I want to read this primary key, fetch data from db and create an employee object for that test run. I know it sounds complicated. I think this is what I am looking for -
How do I provide parameters to a dataprovider for a test at run time? How easy a change is it to create this behavior by altering testng source? |
You can mix data provider + parameters. Your data provider will take primary key from the parameter and will create your Employee object + pass any other attributes. Then your test method will use the data provider and its attributes. |
@juherr Could you give me an example of how I can I have multiple parameters at method level, each going to a dataprovider ? |
@juherr To simplify my question, I am programmatically generating a testng.xml. Now, let's say I have a method modifyEmployee(Employee employee). I need to create an xml in such a way that this method is invoked 4 times, but each time with a different Employee. I would know the primary key of each of these employees at the time of creating testng.xml. So, what is the best way to achieve what I am trying to do? |
I suppposed @dataProvider can work with @parameters but I'm not sure (I should test it one day).
You should ask your question on Staskoverflow or the mailing list if you want more ideas ;) |
Actually, parameters on method level exists. Here is an example: <suite name="my-suite" verbose="1">
<test name="my-test">
<classes>
<class name="testng.ex1.TestParams">
<methods>
<include name="m1">
<parameter name="key1" value="val1"/>
<parameter name="key2" value="val2"/>
</include>
<include name="m2">
<parameter name="key1" value="valA"/>
<parameter name="key2" value="valB"/>
</include>
</methods>
</class>
</classes>
</test>
</suite> package testng.ex1;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestParams {
@Test
@Parameters({ "key1", "key2" })
public void m1(String key1, String key2) throws Exception {
System.out.println(key1 + ", " + key2);
}
@Test
@Parameters({ "key1", "key2" })
public void m2(String key1, String key2) throws Exception {
System.out.println(key1 + ", " + key2);
}
} |
👍 Good to know! I've never seen it before. @JayVem I close the issue as it already exists. |
another approach is to use a data-provider that fetches the keys from testng.xml. See example: <suite name="my-suite" verbose="1">
<test name="my-test">
<classes>
<parameter name="keys" value="key1,key2,key3,key4" />
<class name="testng.ex2.TestParams" />
</classes>
</test>
</suite> package testng.ex2;
import java.util.Arrays;
import java.util.List;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestParams {
@Test(dataProvider = "dp")
public void m1(Employee e) throws Exception {
System.out.println("name: " + e.getName() + ", age: " + e.getAge());
}
@DataProvider(name = "dp")
@Parameters("keys")
public Object[][] createData(ITestContext ctx) {
String keysString = ctx.getCurrentXmlTest().getLocalParameters().get("keys");
List<String> keys = Arrays.asList(keysString.split(","));
Object[][] result = new Object[keys.size()][1];
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
result[i] = new Object[] { new Employee(key) };
}
return result;
}
} package testng.ex2;
public class Employee {
private final String name;
private final int age;
public Employee(String key) {
// use 'key' to lookup employee in database
name = key + "_name"; // dummy value
age = 41; // dummy value
}
String getName() {
return name;
}
int getAge() {
return age;
}
} |
Thanks @drapostolos , I think dataprovider with testcontext sounds like it will do. I will try this out. Thank you so much for the quick answer! |
Is it possible to support special parameters at method level?
For instance,
I am trying to achieve this by modifying testng code - I would appreciate any guidance/start for this.
The text was updated successfully, but these errors were encountered: