我们可以在同一方法上使用@dataprovider和@parameters注释吗?

时间:2016-09-15 13:50:33

标签: java selenium annotations

这是我尝试过的样本程序。但这不起作用。并且抛出dataProvider不匹配错误。

public class dataprovide {
  @Test (dataProvider="providerData")
  @Parameters("num")
  public void f(int number, int expected,int num) {
      Assert.assertEquals(number +num, expected);   //num =10 in testng.xml

       //throw new SkipException("Message");
  }
  @DataProvider/*(name = "provideNumber")*/
    public Object[][] providerData()
    {
      return new Object[][]{
          {10,20}, {100,110}
      };
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用context.getCurrentXmlTest()或在@beforclass中保存您的参数。两者都在下面说明

@BeforeClass
  @Parameters({"testParam"})
  public void init(String suiteParam) {
    this.suiteParam = suiteParam;
  }


@DataProvider(name = "testDataProvider")
  public Object[][] createData1(ITestContext context) {
      String testParam = context.getCurrentXmlTest().getParameter("testParam");

       int theNums[][] = new int[][] {{10,20},{100,110}};


   return new Object[][] {

       { testParam, theNums }

  };

  }



  @Test(dataProvider = "testDataProvider")

  public  void testingParam( String DPparam1, int[][] DPparam2) {

      System.out.println("xml param through data provider: " + DPparam1);
      System.out.println("configured param from data provider: " + DPparam2[0][0]);
      System.out.println("xml param from @beforeclass: " + suiteParam);
      }

并且在xml中就像这样

<parameter name="testParam" value="test123"/>

以上打印

xml param through data provider: test123
configured param from data provider: 10
xml param from @beforeclass: test123