美文网首页
java实践:04_testng测试框架

java实践:04_testng测试框架

作者: 果芽软件 | 来源:发表于2017-12-07 09:34 被阅读227次

    总结

    1. 加dependency依赖(获取注解和注解处理器)
    2. 安装myeclipse插件(调用注解处理器)
    3. 添加注解(5个)和断言(Assert)
    4. 设置优先级、方法依赖关系(标签属性里面)
    5. 所有测试类,使用testng.xml进行分组配置管理
    6. 单条记录参数化(testng.xml配parameter变量,@Parameter使用变量)
    7. 批量测试数据(@DataProvider注解返回Object[][]的方法,取个名字;在Test注解里加dataprovider属性,指定名字)

    一、testng帮我们做的事

    1. 写好了一套注解
    2. 写好了一个注解处理器
    3. 写好了一个插件,用插件来调用注解处理器,省去了main方法

    我们剩下的事情就是给代码添加注解

    二、testNG依赖包

    提供现成的注解、注解处理器等

            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.8</version>
            </dependency>
    

    三、testNG插件

    1. 安装

    用来给myeclipse添加右键菜单,调用testNG注解处理器,省去了写测试类和main方法来调用注解处理器
    点击下载

    ## 插件名称
    org.testng.eclipse_6.8.6.20130607_0745
    
    ## 安装路径
    C:\software\myeclipse14\dropins
    
    ## 下载地址
    https://pan.baidu.com/s/1pLxEKNh
    

    2. 使用方法

    • 先给代码的方法添加testng注解,如@Test
    • 右键(注意:一定要有testng注解才会出现这个菜单选项)


      image.png

    三、测试单个java文件

    1. 注解

    @BeforeTest
    @BeforeMethod
    @Test
    @AfterMethod
    @AterTest
    

    2. 代码方法添加注解

    package com.guoyasoft.testNG.test;
    
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Test;
    
    import com.guoyasoft.testNG.annotations.AfterTest;
    import com.guoyasoft.testNG.annotations.BeforeTest;
    
    public class Methods {
    
        @BeforeTest
        public void method1() {
            System.out.println("method1");
        }
    
        @AfterTest
        public void method2() {
            System.out.println("method2");
        }
    
        @BeforeMethod
        public void method3() {
            System.out.println("method3");
        }
    
        @AfterMethod
        public void method4() {
            System.out.println("method4");
        }
    
        @Test
        public void method5() {
            System.out.println("method5");
        }
    
        @Test
        public void method6() {
            System.out.println("method6");
        }
    }
    

    3. 右键执行run as——》testng cases测试

    [TestNG] Running:
      C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-937961879\testng-customsuite.xml
    
    method3
    method5
    method4
    method3
    method6
    method4
    PASSED: method5
    PASSED: method6
    
    ===============================================
        Default test
        Tests run: 2, Failures: 0, Skips: 0
    ===============================================
    
    ===============================================
    Default suite
    Total tests run: 2, Failures: 0, Skips: 0
    ===============================================
    

    4. 如何确定方法的优先级?

    @Test(priority=2)

    5.如何确定方法间的依赖关系?

    @Test(dependsOnMethods={method1,method2})

    四、同时执行多个测试类

    1. testng.xml配置文件

    <?xml version="1.0" encoding="UTF8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng1.0.dtd">
    <suite name="Suite1">
        <test name="testTestNG">
            <classes>
                <class name="com.guoyasoft.testng.Methods">
                </class>
            </classes>
        </test>
        <test name="testBaidu">
            <classes>
                <class name="com.guoyasoft.testng.BeforeAndAfter" />
                <class name="com.guoyasoft.testng.TestBaidu" />
            </classes>
        </test>
        
        <test name="testJD">
            <parameter name="driverPath" value="c:\\xxxxx" />
            <parameter name="url" value="http://www.jd.com" />
            
            <classes>
                <class name="com.guoyasoft.testng.BeforeAndAfter" />
                <class name="com.guoyasoft.testng.TestJD">
                </class>
            </classes>
        </test>
    </suite>
    

    <suite>代表一个套件,包含一个或多个<test>标签
    <test>代表一个测试,包含一个或多个<classes>标签
    <classes>代表类,可包含多个<class>,<class>中包含<method>,代表要执行的测试方法
    还有<parameter><group><include>等等

    testNG参数化

    单条参数

    testng.xml配置参数

    <?xml version = "1.0" encoding = "UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite name = "Suite1">
       <test name = "test1">
          <parameter name = "myName" value="manisha"/> 
          <classes>
             <class name = "ParameterizedTest1" />
          </classes>
       </test>
    </suite>
    

    @Parameters标签使用参数

    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;
    
    public class ParameterizedTest1 {
       @Test
       @Parameters("myName")
       public void parameterTest(String myName) {
          System.out.println("Parameterized value is : " + myName);
       }
    }
    

    一组参数

    @DataProvider配置参数
    @Test(dataProvider = "test1")属性使用参数

    import org.testng.Assert;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;
    
    public class ParamTestWithDataProvider1 {
       private PrimeNumberChecker primeNumberChecker;
    
       @BeforeMethod
       public void initialize() {
          primeNumberChecker = new PrimeNumberChecker();
       }
    
       @DataProvider(name = "test1")
       public static Object[][] primeNumbers() {
          return new Object[][] {{2, true}, {6, false}, {19, true}, {22, false}, {23, true}};
       }
    
       // This test will run 4 times since we have 5 parameters defined
       @Test(dataProvider = "test1")
       public void testPrimeNumberChecker(Integer inputNumber, Boolean expectedResult) {
          System.out.println(inputNumber + " " + expectedResult);
          Assert.assertEquals(expectedResult, primeNumberChecker.validate(inputNumber));
       }
    }
    

    公共数据

    设置成全局变量,各个方法共享
    private PrimeNumberChecker primeNumberChecker;

    public class PrimeNumberChecker {
       public Boolean validate(final Integer primeNumber) {
       
          for (int i = 2; i < (primeNumber / 2); i++) {
             if (primeNumber % i == 0) {
                return false;
             }
          }
          return true;
       }
    }
    
    import org.testng.Assert;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;
    
    public class ParamTestWithDataProvider1 {
       private PrimeNumberChecker primeNumberChecker;
    
       @BeforeMethod
       public void initialize() {
          primeNumberChecker = new PrimeNumberChecker();
       }
    
       @DataProvider(name = "test1")
       public static Object[][] primeNumbers() {
          return new Object[][] {{2, true}, {6, false}, {19, true}, {22, false}, {23, true}};
       }
    
       // This test will run 4 times since we have 5 parameters defined
       @Test(dataProvider = "test1")
       public void testPrimeNumberChecker(Integer inputNumber, Boolean expectedResult) {
          System.out.println(inputNumber + " " + expectedResult);
          Assert.assertEquals(expectedResult, primeNumberChecker.validate(inputNumber));
       }
    }
    

    循环和多线程

    所有方法都需要多线程,配置testng.xml

    <suite name="Test-class Suite" parallel="classes" thread-count="2" >
      <test name="Test-class test" >
        <classes>
          <class name="com.howtodoinjava.parallelism.ParallelClassesTestOne" />
          <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" />
        </classes>
      </test>
    </suite>
    

    单个方法需要多线程,配置@Test

    public class IndependentTest
    {
        @Test(threadPoolSize = 3, invocationCount = 6, timeOut = 1000)
        public void testMethod()
        {
            Long id = Thread.currentThread().getId();
            System.out.println("Test method executing on thread with id: " + id);
        }
    }
    

    testNG注解清单

    Sr.No. Annotation Description
    1 @BeforeSuite The annotated method will be run only once before all tests in this suite have run.
    2 @AfterSuite The annotated method will be run only once after all tests in this suite have run.
    3 @BeforeClass The annotated method will be run only once before the first test method in the current class is invoked.
    4 @AfterClass The annotated method will be run only once after all the test methods in the current class have run.
    5 @BeforeTest The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
    6 @AfterTest The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
    7 @BeforeGroups The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
    8 @AfterGroups The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
    9 @BeforeMethod The annotated method will be run before each test method.
    10 @AfterMethod The annotated method will be run after each test method.
    11 @DataProvider Marks a method as supplying data for a test method. The annotated method must return an Object[ ][ ], where each Object[ ] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
    12 @Factory Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[ ].
    13 @Listeners Defines listeners on a test class.
    14 @Parameters Describes how to pass parameters to a @Test method.
    15 @Test Marks a class or a method as a part of the test.

    相关文章

      网友评论

          本文标题:java实践:04_testng测试框架

          本文链接:https://www.haomeiwen.com/subject/thtlbxtx.html