简体   繁体   中英

Get Test method name in TestNG/Maven reporting in case of DataProvider running multiple tests with different data for single Test method

I have a Test method in my Automation script that has to be run for 6 different people. There is a custom DataProvider that provides those 6 people and method name is getSalaryDetails() . Execution is working perfectly fine and tests are running fine. Reporting is where the issue comes.

For these 6 Test runs for a single method, results are being displayed as 6 times for getSalaryDetails() which is making difficult to understand the DataProvider for which the test would be failing.

Is there a way to implement in TestNG/Maven so i can get the Test method name by appending the parameter/Data provider somewhere so it would read somewhat like Person1getSalaryDetails() Person2getSalaryDetails() and so on..!

Here is the code that is being used for reporting

public class ReportListener implements ITestListener {


    //Extent Report Declarations
    private static ExtentReports extent = ExtentManager.createInstance();
    private static ThreadLocal<ExtentTest> test = new ThreadLocal<>();


    @Override
    public void onTestStart(ITestResult result) {

        System.out.println((result.getMethod().getMethodName() + " started!"));
        //Start operation for extentreports.
        ExtentTest extentTest = extent.createTest(result.getMethod().getMethodName(), result.getMethod().getDescription());
        test.set(extentTest);
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        System.out.println((result.getMethod().getMethodName() + " passed!"));
        test.get().pass("Test passed" + result);
    }

    @Override
    public void onTestFailure(ITestResult result) {
        System.out.println((result.getMethod().getMethodName() + " failed!"));
        test.get().fail(result.getThrowable());
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        System.out.println((result.getMethod().getMethodName() + " skipped!"));
        test.get().skip(result.getThrowable());
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        System.out.println(("onTestFailedButWithinSuccessPercentage for " + result.getMethod().getMethodName()));
    }

    @Override
    public void onStart(ITestContext context) {
        System.out.println("Extent Reports Version 3 Test Suite started!");
    }

    @Override
    public void onFinish(ITestContext context) {
        System.out.println(("Extent Reports Version 3  Test Suite is ending!"));
        extent.flush();
    }

Parameters passed by the data provider to test methods can be fetched in Listener methods as follows

@Override
public void onTestStart(ITestResult result) {
    System.out.println((result.getMethod().getMethodName() + " started!"));
    //Start operation for extentreports.
    String methodName = result.getMethod().getMethodName();
    Object[] parameters = result.getParameters();
    if (parameters != null && parameters.length > 0) {
        methodName = parameters[0].toString + methodName;
    }
    ExtentTest extentTest = extent.createTest(methodName, result.getMethod().getDescription());
    test.set(extentTest);
}

Another option is we can add the parameter from data provider method to test description from the test method.

@Test
public void testMethod(String personName) {
    ITestResult result = Reporter.getCurrentTestResult();
    result.getMethod().setDescription(personName + " along with other string");
}
@Test(dataProvider = "inputData")
    public void testMethod(final Map<String, Object> dataMap){
    try{

        //Note: As you said you are using dataProvider every test run you will get the personName- add name like shown below line.
        //dataMap.get("personName") - input data from dataprovider
        extentTest = extent.createTest(Thread.currentThread().getStackTrace()[1].getMethodName().toString()+dataMap.get("personName"));
    }catch(Exception e){
        extentTest.log(Status.ERROR, e.getMessage());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM