java testng - 在 testng.xml 中将列表作为参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5310045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:33:14  来源:igfitidea点击:

testng - passing list as parmeters in testng.xml

javaxmltestng

提问by Siva

Would it be possible to pass a list in the testNG parameters. Below is sample code

是否可以在 testNG 参数中传递一个列表。下面是示例代码

Example: Trying to pass list of numbers in XML. Not sure if TestNG does not support this feature. Or am i missing anything?

示例:尝试在 XML 中传递数字列表。不确定 TestNG 是否不支持此功能。或者我错过了什么?

 <suite name="Suite" parallel="none">  
     <test name="Test" preserve-order="false">  
         <parameter name="A" value="1"/>   
         <parameter name="B" value="2"/>   
         <parameter name="C" value="3"/>   
         <parameter name="D" value="{4,5}"/>   
         <classes>  
             <class name="TestNGXMLData"/>  
         </classes>  
     </test>  
 </suite>  


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.thoughtworks.selenium.Selenium;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;

public class TestNGXMLData {

    @Test
    @Parameters(value = { "A", "B", "C", "D" })
    public void xmlDataTest(String A, String B, String C, ArrayList<String> ls) {

        System.out.println("Passing Three parameter to Test " + A + " and " + B + " and " + C);

        Iterator it = ls.iterator();
        while (it.hasNext()) {
            String value = (String) it.next();
        }
    }
}

Thanks, Siva

谢谢,西瓦

回答by Cedric Beust

You can only pass basic types like this, so you should declare your last parameter as a "String" and then convert "{3, 4}" to a List. I suggest using "3 4" instead and simply parse it with String#split.

您只能传递这样的基本类型,因此您应该将最后一个参数声明为“字符串”,然后将“{3, 4}”转换为列表。我建议改用“3 4”并简单地用 String#split 解析它。

If you want to pass more complex parameters and you don't want to bother with converting, switch to using a @DataProvider.

如果您想传递更复杂的参数并且不想费心进行转换,请切换到使用@DataProvider。

回答by Raghuram

From the manual, @Parametercan be used for simple parameters. For complex objects, you should look at @Dataprovider

来自手册@Parameter可用于简单参数。对于复杂的对象,你应该看看@Dataprovider