zoukankan      html  css  js  c++  java
  • testNG参数传递方式

    testNG传参数的两种方式(xml文件,@DataProvider)

    • 使用testng.xml设置参数

     参数在xml文件中可以在suite级别定义,也可以在test级别定义;testNG会尝试先在包含当前类的test标签中寻找参数,如果没找到则在上层的suite标签中查找。即在test标签中相同的参数对当前类当前方法的优先级比较高。 testNG支持这种传参方式的类型如下:String、 int/Integer、boolean/Boolean、 byte/Byte、 char/Character、 double/Double、 float/Float、 long/Long、 short/Short。对于非上述类型TestNG无法通过这种方式进行传参,可以通过@DataProvider方式传参

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    public class ParameterTest {
        /**
         * Following method takes one parameter as input. Value of the
         * said parameter is defined at suite level.
         */
        @Parameters({ "suite-param" })
        @Test
        public void prameterTestOne(String param) {
            System.out.println("Test one suite param is: " + param);
        }
     
        /**
         * Following method takes one parameter as input. Value of the
         * said parameter is defined at test level.
         */
        @Parameters({ "test-two-param" })
        @Test
        public void prameterTestTwo(String param) {
            System.out.println("Test two param is: " + param);
        }
     
        /**
         * Following method takes two parameters as input. Value of the
         * test parameter is defined at test level. The suite level
         * parameter is overridden at the test level.
         */
        @Parameters({ "suite-param""test-three-param" })
        @Test
        public void prameterTestThree(String param, String paramTwo) {
            System.out.println("Test three suite param is: " + param);
            System.out.println("Test three param is: " + paramTwo);
        }
    }

    xml文件配置:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    <suite name="Parameter test Suite" verbose="1">
        <!-- This parameter will be passed to every test in this suite -->
        <parameter name="suite-param" value="suite level parameter" />
        <test name="Parameter Test one">
            <classes>
                <class name="com.howtodoinjava.test.ParameterTest">
                    <methods>
                        <include name="prameterTestOne" />
                    </methods>
                </class>
            </classes>
        </test>
        <test name="Parameter Test two">
            <!-- This parameter will be passed this test only -->
            <parameter name="test-two-param" value="Test two parameter" />
            <classes>
                <class name="com.howtodoinjava.test.ParameterTest">
                    <methods>
                        <include name="prameterTestTwo" />
                    </methods>
                </class>
            </classes>
        </test>
        <test name="Parameter Test three">
            <!-- Overriding suite level parameter -->
            <parameter name="suite-param" value="overiding suite parameter" />
            <!-- Test specific parameter -->
            <parameter name="test-three-param" value="test three parameter" />
            <classes>
                <class name="com.howtodoinjava.test.ParameterTest">
                    <methods>
                        <include name="prameterTestThree" />
                    </methods>
                </class>
            </classes>
        </test>
    </suite>

    也可以使用可选参数,如果xml里没有给参数传入值,那么测试方法将会调用可选的参数值:

    1
    2
    3
    4
    5
    6
    7
    8
    public class ParameterTest
    {
        @Parameters({ "optional-value" })
        @Test
        public void optionTest(@Optional("optional value") String value) {
            System.out.println("This is: " + value);
        }
    }

    对应的xml文件配置:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <suite name="Optional test Suite" verbose="1">
      <test name="Optional Test one">
        <classes>
          <class name="test.parameter.OptionalTest" />
        </classes>
      </test>
      <test name="Optional Test two">
        <parameter name="optional-value" value="passed from xml" />
        <classes>
          <class name="test.parameter.OptionalTest" />
        </classes>
      </test>
    </suite>

    这里配置了两个test,第一个执行option方法时,由于没有在xml文件里找到option-value对应的值,所以将会输出方法中的@Optional的值;而第二个test中有设置option-value的值为passed........那么将会输出对应的值。

    结果:

    This is: optional value

    This is: passed from xml

     

    ===============================================

    Optional test Suite

    Total tests run: 2, Failures: 0, Skips: 0

    ===============================================

     

    •  DataProvider方式传参

          只提供了一个字符串属性:名称,供测试方法作为传递参数的annotation使用两种DataProvider,一种是返回一个二维数组对象(Object[][],第一个数组是数据集,第二个数组代表参数值),如上面的例子;另外一种DataProvider是返回一个Iterator,DataProvider可以向测试方法传递任意类型任意数目的参数,利用DataProvider提供不同的参数集合对一个测试方法进行多次调用。

        dataProvider可以进行数据驱动的测试。

    在同一个类中使用dataProvider:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public class SameClassDataProvider
    {
        @DataProvider(name = "data-provider")
        public Object[][] dataProviderMethod() {
            return new Object[][] { { "data one" }, { "data two" } };
        }
      
        @Test(dataProvider = "data-provider")
        public void testMethod(String data) {
            System.out.println("Data is: " + data);
        }
    }

    结果:

    Data is: data one
    Data is: data two
    PASSED: testMethod("data one")
    PASSED: testMethod("data two")

    在不同的类中使用:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    public class DataProviderClass
    {
        @DataProvider(name = "data-provider")
        public static Object[][] dataProviderMethod()
        {
            return new Object[][] { { "data one" }, { "data two" } };
        }
    }
    public class TestClass
    {
        @Test(dataProvider = "data-provider", dataProviderClass = DataProviderClass.class)
        public void testMethod(String data)
        {
            System.out.println("Data is: " + data);
        }
    }

    结果和上面的一样,只是要注意,在使用数据集的方法中需要添加dataProviderClass的属性,指明是那个类提供数据。​

  • 相关阅读:
    ini_set /ini_get函数功能-----PHP
    【转】那个什么都懂的家伙
    word 2007为不同页插入不同页眉页脚
    August 26th 2017 Week 34th Saturday
    【2017-11-08】Linux与openCV:opencv版本查看及库文件位置等
    August 25th 2017 Week 34th Friday
    August 24th 2017 Week 34th Thursday
    August 23rd 2017 Week 34th Wednesday
    August 22nd 2017 Week 34th Tuesday
    August 21st 2017 Week 34th Monday
  • 原文地址:https://www.cnblogs.com/silence-hust/p/4541005.html
Copyright © 2011-2022 走看看