Need help in modifying the Testng Report

168 Views Asked by At

In my report the last Test name in excel sheet is getting appended in my results. Here is my code

public class Test_suite implements ITest {

    private String testInstanceName="";

    public String getTestName() 
    {
        return testInstanceName;
    }
    private void setTestName(String anInstanceName) 
    {
        this.testInstanceName = anInstanceName;
    }

    @DataProvider() 
    public Object[][] Unit() throws Exception
    {
        Object[][] testObjArray = Excel.getTableArray("./Test Data/Test.xlsx","Unit");
        return (testObjArray);
    }

    @BeforeMethod(alwaysRun=true)
    public void before(Method method,Object[] parameters)
    {
            String testCaseId="";
            testCaseId = parameters[0].toString();
            System.out.println(testCaseId);
            setTestName(testCaseId);
    }

@Test(dataProvider="Unit")
public void Test(){
}

And my report looks like this

Report

2

There are 2 best solutions below

4
Harish On

This is because all tests that uses a dataProvider will be executed in parallel. The default thread count used is 10. And as you can see, these methods use the same insance of the class, in your case Test_suite.java

The insance variable testInstanceName is mutable and is overwriten by every dataprovider thread and the latest is used to set as the test name. You can achieve what you are intending to do by containing the set test name logic within your before method and not using an instance variable

0
aviral On
package API_Testing;

import java.lang.reflect.Method;

import org.testng.ITest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import resources.Excel;
import resources.Utils;

public class Carto implements ITest{

    private String testInstanceName="";

    public String getTestName() 
    {
        return testInstanceName;
    }

    @DataProvider 
    public Object[][] Data() throws Exception
    {
        Object[][] testObjArray = Excel.getTableArray("./Test_Cases/Test_Cases.xlsx","Carto");
        return (testObjArray);
    }

    @BeforeMethod(alwaysRun=true)
    public void before(Method method,Object[] parameters)
    {
        String testCaseId="";
        testCaseId = parameters[0].toString();
        this.testInstanceName=testCaseId;
    }

    @Test(dataProvider="Data")
    public void Test(String TCname,String Type,String API,String Input,String Input_Path,String Validator,String Output,String Output_value )throws  Exception
    {
        Utils util=new Utils();
        Output_value=Output_value.substring(1,Output_value.length()-1);
        util.Test_body(Type, API, Input, Input_Path, Validator, Output, Output_value);
    }
}

Now my code is like this and output is same what I uploaded earlier. No change