0

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

Morfic
  • 15,178
  • 3
  • 51
  • 61
aviral
  • 1

2 Answers2

0

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

Harish
  • 1,433
  • 9
  • 21
  • After incorporating your suggestion still my isuue is not resolved. I want to get (TC_7) removed from every test run which is displayed in the report. Now my before method code look like @BeforeMethod(alwaysRun=true) public void before(Method method,Object[] parameters) { String testCaseId=""; testCaseId = parameters[0].toString(); this.testInstanceName=testCaseId; } and I removed the setTestName function. – aviral Mar 26 '18 at 05:53
  • Please help on this – aviral Mar 27 '18 at 11:16
  • Can you share the version of the code you are using right now and its output? – Harish Mar 27 '18 at 14:20
  • The *(TC_7) * is from the data provider and I am not sure where this is appended from. Even using different listeners, I am able to set the test name but unable to remove the appended parametrized value from the data provider – Harish Mar 31 '18 at 04:36
0
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

aviral
  • 1