Have the following Sample code ... when running tests (and in reports), I would like the Test Names to be set to the description field provided by the provider (really it's any String).
... however, even when extending from ITest, it seems like all the provider parameters get appended to the TestName, what I want is the description only.
So actual test name should be "TestName1" instead of "TestName2[1](TestName2, 2, 2, 4)" .. which is what is showing up in XML reports, and test.aftertest name.
import org.testng.Assert;
import org.testng.ITest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.lang.reflect.Method;
public class TestNgProviderExample implements ITest{
@Test(dataProvider = "summationProvider")
public void testProvider(String description, int number1, int number2, int sum) {
Assert.assertEquals(sum, number1 + number2);
}
@DataProvider(name = "summationProvider")
public Object[][] summationData() {
Object[][] testData = {{"TestName1",1,2,3},{"TestName2",2,2,4}};
return testData;
}
private String reportedTestName = "";
@BeforeMethod(alwaysRun = true)
public void testData(Method method, Object[] testData) {
reportedTestName = testData[0].toString();
}
@Override
public String getTestName() {
return reportedTestName;
}
}
Simply running a
@Test
-annotated method without parameters can solve it.Instead of parameters my solution uses class fields and a constructor
@Factory
method.Output with formatting from my IntelliJ Idea:
Of course, it is possible to make the
@Factory
instantiate another class, not the sameTestClass
.And if you have 40 separate variables from parameters and want to keep it short... Add a class that would be a holder for those parameters, e.g.
ParametrizedInput
. It would at least make it possible to hide all those instance variables. You can placedescription
in the second class as well (in that caseThreadLocal<ParametrizedInput>
usage would be advised).The second class will grow with parameters, but since it is a plain old Java object, it shouldn't break anything in test. If you don't want to set all those variables, another idea would be to access parameters lazily in tests. Following expert's (Krishnan Mahadevan) advice I figured out that the name with a method can be set with a
@BeforeMethod
- and@AfterMethod
-annotated methods.Parameter Holder class:
Result: