Dataprovider Mixing Up Values for Parallel Methods

时间:2019-04-16 22:04:35

标签: parallel-processing testng

I have two methods that use the same data provider. The Data provider reads an excel file to build the object to pass to the methods. However, when running the two methods in parallel, the data provider only passes back one variable for both methods.

I attempted to change the dataprovider variable names and object names but this did not work. I confirmed that the excel util function is working. Each if statement is called correctly in the data provider method, but it returns the wrong variable. How can I ensure that the data provider executes sequentially as to now mix up the variables?

Here is my Java class with testNG

public class dataprovidertest extends DriverMethods {

    @Test (groups = {"test"}, dataProvider = "datadriven")
    public void method1(String var1) throws Exception {
            System.out.println("method 1 variable: " + var1);
    }


    @Test (groups = {"test"}, dataProvider = "datadriven")
    public void method2(String var2) throws Exception {
        System.out.println("method 2 variable: " + var2);
    }

    @DataProvider (name = "datadriven")
    public Object[][] provideData(Method m) throws Exception{

        if(m.getName().equalsIgnoreCase("method1")) {
            String sTestCaseName1 = m.getName();
            Object[][] testObjArray1 = ExcelUtils.getTableArray("C:\\Users\\user\\dataprovidertest_TestData.xlsx", sTestCaseName1);
            return (testObjArray1);
        }

        if(m.getName().equalsIgnoreCase("method2")) {
            String sTestCaseName2 = m.getName();
            Object[][] testObjArray2 = ExcelUtils.getTableArray("C:\\Users\\user\\dataprovidertest_TestData.xlsx", sTestCaseName2);
            return (testObjArray2);
        }


        return null;
    }

Here is my xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods" >

    <test thread-count="6" name="Test" parallel = "methods">
      <classes>
        <class name="cpq.dataprovidertest">
            <methods>
                <include name ="method1"/> 
                <include name ="method2"/>

            </methods>
     </class>
      </classes>
    </test> <!-- Test -->

</suite> <!-- Suite -->

Here is a description of my excel sheet:

Sheet "method1" has value "method1 var"
Sheet "method2" has value "method2 var"

Currently, the output is:

[testng] method 2 variable: method2 var
[testng] method 1 variable: method2 var

or

  [testng] method 2 variable: method1 var
  [testng] method 1 variable: method1 var

How can I change this so it becomes:

  [testng] method 2 variable: method2 var
  [testng] method 1 variable: method1 var

Thanks!

i noticed that when I separate the two methods into class tags in the xml file, and run the tests parallel by classes, it works as expected:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" data-provider-thread-count="2">

    <test thread-count="6" name="Test" parallel = "classes">
      <classes>
        <class name="cpq.dataprovidertest">
            <methods>
                <include name ="method1"/> 
            </methods>
        </class>
        <class name="cpq.dataprovidertest">
            <methods>
                <include name ="method2"/> 
            </methods>
        </class>
      </classes>
    </test> <!-- Test -->

</suite> <!-- Suite -->

1 个答案:

答案 0 :(得分:0)

尝试同步化

public synchronized Object[][]

已更新:添加了与从工作表收集数据的方法同步的功能。