我们如何将参数和数据提供者传递给Testng中的相同方法

时间:2017-01-23 09:28:40

标签: java testing

您好我正在尝试用Java驱动数据。我读取excel并将值传递给变量“test2”和“test 3”进行测试。另外,我还需要一个参数“test1”,我从testing.xml发送它。现在执行下面的测试会抛出一个错误

org.testng.TestNGException: 数据提供程序正在尝试传递2个参数,但方法com.xxxxx.xxx需要3,而TestNG无法注入合适的对象

那么如何将参数和数据提供者传递给同一个方法。感谢

/* testdataprovider will return the test2 and test3 parameter value */
@Parameters("test1")
@Test(dataProvider = "testdataprovider")
public void testMethod(String test1, String test2, String test3){}

1 个答案:

答案 0 :(得分:1)

我可能非常错误(如果我是,我相信别人会给你一个更好的解决方案);但我不认为你可以使用testNG Guruswamy来实现你的目标。 我刚刚使用此dataProvider测试了您的尝试

@DataProvider(name = "login8users")
    public Object[][] login8usersForStackOverflow() {
      return new Object[][] {
       { "user1@gmail.com", "pass" },
       { "user2@gmail.com",  "pass"},
       { "user3@gmail.com",  "pass"},
       { "user4@gmail.com",  "pass"},
       { "user5@gmail.com",  "pass"},
       { "user6@gmail.com",  "pass"},
       { "user7@gmail.com",  "pass"},
       { "user8@gmail.com",  "pass"},      
      };
    }

并且从XML文件传递一个名为browser 的参数。我注意到当你在同一个@Test注释下使用dataProvider和Parameters 时;您是否会收到上面显示的错误或XML参数将被完全忽略(似乎dataProvider取代它们)。 然后是测试方法:

 @Test(dataProvider="login8users") 
 @Parameters({"browser"})
 public void login8(String dataProvider1, String dataProvider2) {         

            System.out.println("Email from data provider is..: "+dataProvider1);
            System.out.println("Pass from data provider is..: "+dataProvider2);     

        } 

这是输出:

[TestNG] Running:
  C:\projects\test\currentTest.xml    

Email from data provider is..: user1@gmail.com
Pass from data provider is..: pass
Email from data provider is..: user2@gmail.com
Pass from data provider is..: pass
Email from data provider is..: user3@gmail.com
Pass from data provider is..: pass
Email from data provider is..: user4@gmail.com
Pass from data provider is..: pass
Email from data provider is..: user5@gmail.com
Pass from data provider is..: pass
Email from data provider is..: user6@gmail.com
Pass from data provider is..: pass
Email from data provider is..: user7@gmail.com
Pass from data provider is..: pass
Email from data provider is..: user8@gmail.com
Pass from data provider is..: pass

===============================================
Regression
Total tests run: 8, Failures: 0, Skips: 0
===============================================

因此,您需要搜索替代解决方案。其中一个可能是加载参数,(甚至可能在@BeforeClass中)将参数作为全局变量传递给您,以便您可以访问它。所以这样的解决方案:

public static String globalVarScope;


       @BeforeClass
       @Parameters({"xmlParam"}) 
       public void initialSetup(String xmlParam) {

            System.out.println("Parameter from XML is..: "+xmlParam);
            globalVarScope= xmlParam;   
        }   


        @Test         
        public void useMeAnywhere() {         

            System.out.println("Hey, I passed the param earlier and I can use it anywhere now :) ..: "+globalVarScope);    
        }    


        @Test(dataProvider="login8users") 
        @Parameters({"browser"})
        public void login8(String dataProvider1, String dataProvider2) {      

            System.out.println("Email from data provider is..: "+dataProvider1);
            System.out.println("Pass from data provider is..: "+dataProvider2);     
        }  

所以希望你现在可以达到预期的效果,如果你遇到困难,请告诉我。以下是单独运行参数后的最终输出:

 [TestNG] Running:
      C:\projects\test\currentTest.xml

    Browser is..: ***chrome***
    Email from data provider is..: user1@gmail.com
    Pass from data provider is..: pass
    Email from data provider is..: user2@gmail.com
    Pass from data provider is..: pass
    Email from data provider is..: user3@gmail.com
    Pass from data provider is..: pass
    Email from data provider is..: user4@gmail.com
    Pass from data provider is..: pass
    Email from data provider is..: user5@gmail.com
    Pass from data provider is..: pass
    Email from data provider is..: user6@gmail.com
    Pass from data provider is..: pass
    Email from data provider is..: user7@gmail.com
    Pass from data provider is..: pass
    Email from data provider is..: user8@gmail.com
    Pass from data provider is..: pass

    ===============================================
    Regression
    Total tests run: 9, Failures: 0, Skips: 0
    ===============================================  

祝你好运!