1.6 testng参数传递的几种方式

1、通过jenkins传递或者<systemPropertyVariables>传递,jenkins传递的值也是会被<systemPropertyVariables>

标签获取,在代码中可以通过System.getProperty()方法获取

https://blog.csdn.net/LetsStudy/article/details/114189463

2、通过Parameters注解及xml配置传参

<classes>
            <class name="org.example.api.LoginTest">
                <parameter name="hello"  value=""/>
                <methods>
                    <include name="login"/>
                    <include name="testParm"/>
                    <include name="get"/>
                </methods>
            </class>
        </classes>
  @Test
    @Parameters({"hello","test11"})
    public void getParm(String name, @Optional("test22")String name2){
        System.out.println(name);
    }

注:testng.xml测试标签有<suite>、<test>、<classes>、<class>、<group>等,当在不同层级配置了相同的parameters参数,

方法会从本层标签向上层标签寻找参数。

@Optionoal表示缺省,如果没有该参数,则取设定的默认值

3、通过@DataProvider传参

a、指定DataProvider的名称,在@Test注解中直接引用,如果没有指定DataProvider的名称,则取其方法名为名称

  @DataProvider(name = "test")
    public Object[][] test() {
        return new Object[][]{{"hello"}, {"world"}};
    }

    @Test(dataProvider = "test",groups = "testB")
    public void ret(Object obj) {
        System.out.println(obj);
    }

b、通过class方式引入

public class Data {
    @DataProvider
    public Object[][] getData(){
        return new Object[][]{{"test"},{"testdata"}};
    }
}
   @Test(dataProvider = "getData",dataProviderClass = org.example.data.Data.class,groups = "testB")
    public void ret(Object obj) {
        System.out.println(obj);
    }

c、以实体类方式通过@DataProvider传递,也可以直接在方法中使用

package org.example.po;
import com.alibaba.fastjson.*;
import com.alibaba.fastjson.annotation.JSONField;

public class User {
    @JSONField(name = "name")
    private String name;

    @JSONField(name = "age")
    private int id;
    public String getName() {
        return name;
    }

    public User(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


}
 @DataProvider(name = "user")
    public Object[] test() {
        return new Object[]{
                new User("xiaowang",21),
                new User("xiaoming",22)
        };
    }
    @Test(dataProvider = "user",groups = "testB")
    public void testUser(User user){
        System.out.println(JSON.toJSONString(user));
    }

d、根据不同的@Test方法传不同的参数

 @DataProvider(name = "user")
    public Object[] testsss(Method m) {
        if(m.getName()=="testUser"){
            return new Object[]{
                    new User("xiaowang",21),
                    new User("xiaoming",22)
            };
        }
        else {
            return new Object[]{
                    "this is test",
                    "this is test"
            };
        }

    }
    @Test(dataProvider = "user",groups = "testB")
    public void testUser(User user){
        System.out.println(JSON.toJSONString(user));
    }
    @Test(dataProvider = "user",groups = "testB")
    public void testThis(Object obj){
        System.out.println(obj);
    }

4、通过properties引入

a、新建test下面resources目录,并用idea标记为Test Resources

b、新建配置文件config.properties

host=www.baidu.com
port=8080

c、新建工具类

 public static String getProperties(String key) {

        Properties properties = new Properties();
        try {
            properties = PropertiesLoaderUtils.loadAllProperties("config.properties");
        } catch (IOException e) {
            e.printStackTrace();

        }
        return properties.getProperty(key);
    }

d、pom的build下添加打包配置

 <testResources>
      <testResource>
        <directory>${basedir}/src/test/java/org/example/resources</directory>
      </testResource>
<!--      <testResource>-->
<!--        <directory>${basedir}/src/main/java/org/example/resources</directory>-->
<!--      </testResource>-->
    </testResources>

如果注释中的放开,则使用的是main目录下resources中的配置文件

e、在@Test方法中使用

 @Test(groups = "testB")
    public void login(ITestContext context) {
        int code = HttpRequest.get(baseUrl)
                .execute().getStatus();
        System.out.println(baseUrl);
    }

5、通过ITestContext context传递

    @Test(groups = "testB")
    public void login(ITestContext context) {
        context.setAttribute("time",new Date());
    }

    @AfterMethod
    public void af(ITestContext context){
        System.out.println(context.getAttribute("time"));
    }

需要注意方法的执行顺序,确保setAttribute()比getAttribulte()先执行

官方文档:https://testng.org/doc/documentation-main.html#running-testng-programmatically

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值