• testNG的常用用法Parameters,DataProvider


    testNG的常用用法Parameters,DataProvider

    一、Parameters

    在testng.xml中配置 里面的name值对应写到 Parameters中然后,在test中输出这个参数名,就可以得到配置中的value值

    实例:
    <suite name="Suite" parallel="classes" thread-count="3">
        <test verbose="2" preserve-order="true" name="TestDebug">
            <parameter name="Name" value="asdfsfa" />
            <classes>
                <class name="com.seventhDay.TestNG1" />
            </classes>
        </test> <!-- Test -->
    </suite> <!-- Suite -->
    
        @Parameters("Name")
        @Test
        public void parametersTest(String Name){
            System.out.println(Name);
        }
    

    结果:

    asdfsfa
    

    二、DataProvider

    a.对象数据:
    @Test(dataProvider = "testdb")
        public void parameterIntTest(Class clzz, String str,String line3, int no) {
    		System.out.println(Thread.currentThread().getName());
           System.out.println("Parameterized Number is : " + clzz);
           System.out.println("Parameterized Number is : " + str);
           System.out.println("Parameterized Number is : " + line3);
           System.out.println("Parameterized Number is : " + no);
        }
    
    @DataProvider(name = "testdb",parallel = false
        		)
        public Object[][] parameterIntTestProvider() {
            return new Object[][]{
                       {Vector.class, "test1","a",1},
                       {String.class, "test2","b",2},
                       {Integer.class, "test3","c",3},
                       {String.class, "test3","c",4},
                       {String.class, "test3","c",4}
                      };
        }
    

    结果:

    [RemoteTestNG] detected TestNG version 7.0.0
    main
    Parameterized Number is : class java.util.Vector
    Parameterized Number is : test1
    Parameterized Number is : a
    Parameterized Number is : 1
    main
    Parameterized Number is : class java.lang.String
    Parameterized Number is : test2
    Parameterized Number is : b
    Parameterized Number is : 2
    main
    Parameterized Number is : class java.lang.Integer
    Parameterized Number is : test3
    Parameterized Number is : c
    Parameterized Number is : 3
    main
    Parameterized Number is : class java.lang.String
    Parameterized Number is : test3
    Parameterized Number is : c
    Parameterized Number is : 4
    main
    Parameterized Number is : class java.lang.String
    Parameterized Number is : test3
    Parameterized Number is : c
    Parameterized Number is : 4
    PASSED: parameterIntTest(class java.util.Vector, "test1", "a", 1)
    PASSED: parameterIntTest(class java.lang.String, "test2", "b", 2)
    PASSED: parameterIntTest(class java.lang.Integer, "test3", "c", 3)
    PASSED: parameterIntTest(class java.lang.String, "test3", "c", 4)
    PASSED: parameterIntTest(class java.lang.String, "test3", "c", 4)
    
    b.迭代器:
    public class TestBean {
    	
    	private  String name;
    	private  String msg;
    	private  String line3;
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getMsg() {
    		return msg;
    	}
    	public void setMsg(String msg) {
    		this.msg = msg;
    	}
    }
    
    public class TestBean2 {
    	
    	private  String name;
    	private  String msg;
    	private  String line3;
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getMsg() {
    		return msg;
    	}
    	public void setMsg(String msg) {
    		this.msg = msg;
    	}
    }
    
    public class DataProviderTest2 {
    	
    	
    	@Test(dataProvider = "mytest")
        public void parameterIntTest(TestBean bean,TestBean2 bean2) {
    	   System.out.println(Thread.currentThread().getName()+" bean "+bean);
           System.out.println("Parameterized Number is : " + bean.getName());
           System.out.println("Parameterized Number is : " + bean2.getMsg());
        }
    
        @DataProvider(name = "mytest", parallel = true)
        public Iterator<Object[]> parameterIntTestProvider() {
        	List<Object[]> dataProvider = new ArrayList<Object[]>();
        	for(int i=0;i<2;i++){
        		TestBean bean = new TestBean();
        		bean.setName("testname"+i);
        		bean.setMsg("--"+i);
        		TestBean2 bean2 = new TestBean2();
        		dataProvider.add(new Object[] { bean,bean2});
        	}
            return dataProvider.iterator();
        }
    
    }
    

    结果:

    [RemoteTestNG] detected TestNG version 7.0.0
    TestNG-PoolService-0 bean testng.TestBean@56d87a9
    TestNG-PoolService-1 bean testng.TestBean@232aa3b3
    Parameterized Number is : testname1
    Parameterized Number is : testname0
    Parameterized Number is : null
    Parameterized Number is : null
    PASSED: parameterIntTest(testng.TestBean@56d87a9, testng.TestBean2@18a96ba9)
    PASSED: parameterIntTest(testng.TestBean@232aa3b3, testng.TestBean2@411e52d7)
    

    作者:我是刘先生
    地址:https://www.cnblogs.com/cekaigongchengshi/
    文章转载请标明出处,如果,您认为阅读这篇博客让您有些收获,不妨点击一下推荐按钮,据说喜欢分享的,后来都成了大神

    欢迎扫码关注微信公众号
  • 相关阅读:
    android Notification 通知栏点击不能跳转(转自:http://www.oschina.net/question/778954_212394)
    Android使用ContentObserver监听数据库变化(转自:http://www.blogjava.net/zhaojianhua/archive/2011/10/27/362204.html)
    cenos 6.4 samba 服务器安装与配置
    centos下nginx+tomcat部署java web全过程(包括在线离线安装Mysql等)
    python读取csv文件
    linux文件实时同步
    Linux 上安装 mysql
    Mysql5.7.20安装文档
    Cassanfra、Hbase和MongoDB的选取
    org.springframework.http.converter.HttpMessageConversionException: T
  • 原文地址:https://www.cnblogs.com/cekaigongchengshi/p/13445786.html
Copyright © 2020-2023  润新知