简体   繁体   English

测试数据提供者获取错误“数据提供者不匹配”

[英]Testng data provider Getting error "Data provider mismatch"

Here I am assigning data我在这里分配数据

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

Here is my test 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);
    }
}

Getting following error得到以下错误

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数据提供者不匹配

I am not sure where I missed我不确定我错过了哪里

I am able to reproduce the issue.我能够重现这个问题。 The below error...下面的错误...

  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]]
        

...is saying "The method requires {String, String, String} input but you are passing {Object, Object, Object} ...说“该方法需要{String, String, String}输入,但您正在传递{Object, Object, Object}

Code :代码

@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));
}

Output :输出

https://google.com-username-mypassword

Instead of that simply you can pass the data like below.而不是简单地您可以传递如下数据。

@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);
}

There are few issues in the code.代码中的问题很少。

  1. Your single test case expects url, username and password.您的单个测试用例需要 url、用户名和密码。 So it should be a single array {"https://google.com","username","mypassword"}所以它应该是一个单一的数组{"https://google.com","username","mypassword"}
  2. Also data provider expected 2-d array, but your SiteUrlForSignin is a 3-d array.数据提供者也需要SiteUrlForSignin数组,但您的SiteUrlForSignin是一个 3 维数组。 Convert it as:将其转换为:
public static Object[][] SiteUrlForSignin = {{"https://google.com","username","mypassword"}};

Note: According to the documentation, you cannot inject Method into a method annotated with @Test .注意:根据文档,您不能将Method注入使用@Test注释的方法中。 It is applicable only to @BeforeMethod , @AfterMethod and @DataProvider , But it is possible now :-)它仅适用于@BeforeMethod@AfterMethod@DataProvider ,但现在可以了:-)

Not sure if it is a bug or if the documentation is not updated.不确定这是错误还是文档未更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM