测试数据提供者获取错误 "Data provider mismatch"

Testng data provider Getting error "Data provider mismatch"

我在这里分配数据

public static Object[][][] SiteUrlForSignin = {
    {{"https://google.com"},{"username"},{"mypassword"}}
};

这是我的测试class

public class SignInTest{

    @DataProvider(name = "SigninLink")
    public Object[][] getData() {
        Object[][][] data = SiteConfig.SiteUrlForSignin;
        return data;
    }    

    @Test(priority = 1, alwaysRun = true, dataProvider = "SigninLink")
    public void mytest(String url,String username,String password,Method method,ITestContext ctx) throws InterruptedException, IOException {

        System.out.println(url+"-"+username+"-"+password);
    }
}

出现以下错误

has no parameters defined but was found to be using a data provider (either explicitly specified or inherited from class level annotation). Data provider mismatch

我不确定我错过了什么

我能够重现这个问题。以下错误...

  Data provider mismatch
  Method: mytest([Parameter{index=0, type=java.lang.String, 
    declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String,   
    declaredAnnotations=[]}, Parameter{index=2, type=java.lang.String, 
    declaredAnnotations=[]}, Parameter{index=3, ype=java.lang.reflect.Method, 
    declaredAnnotations=[]}, Parameter{index=4, type=org.testng.ITestContext, declaredAnnotations=[]}])
    
  Arguments: 
   [([Ljava.lang.Object;) [https://google.com],
   ([Ljava.lang.Object;) [username],
   ([Ljava.lang.Object;) [mypassword]]
        

...说“该方法需要 {String, String, String} 输入,但您传递的是 {Object, Object, Object}

工作代码:

@Test(priority = 1, alwaysRun = true, dataProvider = "SigninLink")
public void mytest(Object url, Object username, Object password, Method method, ITestContext ctx)
        throws InterruptedException, IOException {
    System.out.println(Array.get(url, 0) + "-" + Array.get(username, 0) + "-" + Array.get(password, 0));
}

输出:

https://google.com-username-mypassword

除此之外,您还可以像下面这样传递数据。

@DataProvider(name = "SigninLink")
    public Object[][] getData() {
        return new Object[][] { { "https://google.com", "username", "mypassword" } };
    }

@Test(priority = 1, alwaysRun = true, dataProvider = "SigninLink")
public void mytest(String url, String username, String password, Method method, ITestContext ctx)
        throws InterruptedException, IOException {
    System.out.println(url + "-" + username + "-" + password);
}

代码中几乎没有问题。

  1. 您的单个测试用例需要 url、用户名和密码。所以应该是单数组{"https://google.com","username","mypassword"}
  2. 数据提供者也期望二维数组,但您的 SiteUrlForSignin 是三维数组。将其转换为:
public static Object[][] SiteUrlForSignin = {{"https://google.com","username","mypassword"}};

注意: 根据文档,您不能将 Method 注入到用 @Test 注释的方法中。它仅适用于 @BeforeMethod@AfterMethod@DataProvider,但现在可以:-)

不确定是bug还是文档没有更新。