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

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

        <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.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,198评论 6 514
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,334评论 3 398
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,643评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,495评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,502评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,156评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,743评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,659评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,200评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,282评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,424评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,107评论 5 349
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,789评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,264评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,390评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,798评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,435评论 2 359

推荐阅读更多精彩内容