java实践:04_testng测试框架

总结

  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依赖包

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

        
            org.testng
            testng
            6.8
        

三、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注解才会出现这个菜单选项)


    java实践:04_testng测试框架_第1张图片
    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配置文件




    
        
            
            
        
    
    
        
            
            
        
    
    
    
        
        
        
        
            
            
            
        
    

代表一个套件,包含一个或多个标签
代表一个测试,包含一个或多个标签
代表类,可包含多个,中包含,代表要执行的测试方法
还有等等

testNG参数化

单条参数

testng.xml配置参数




   
       
      
         
      
   

@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


  
    
      
      
    
  

单个方法需要多线程,配置@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 tag is run.
6 @AfterTest The annotated method will be run after all the test methods belonging to the classes inside the 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测试框架)