栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java接口自动化【第三篇】自动化框架设计及TestNG注解的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java接口自动化【第三篇】自动化框架设计及TestNG注解的使用

自动化框架设计

显示层:测试报告
控制层:逻辑验证
持久层:测试用例存储(数据驱动)

TestNG介绍
  • 比Junit涵盖功能更全面的测试框架
  • 比Junit更适合隔离性较强的单元测试
  • TestNG更适合复杂的集成测试
  • TestNG使用
TestNG基本注解
import org.testng.annotations.*;

public class Base {
    @Test
    public void testcase1(){
        System.out.println("test1");
    }
    @Test
    public void testcase2(){
        System.out.println("test2");
    }
    @BeforeMethod
    public void beforeMethod(){
        System.out.println("before method");
    }
    @AfterMethod
    public void afterMethod(){
        System.out.println("after method");
    }
    @BeforeClass
    public void beforeClass(){
        System.out.println("before class");
    }
    @AfterClass
    public void afterClass(){
        System.out.println("after class");
    }
//    suite可以包含多个class,suite是在类运行前和运行后执行的
    @BeforeSuite
    public void beforeSuite(){
        System.out.println("before suite");
    }
    @AfterSuite
    public void afterSuite(){
        System.out.println("after suite");
    }
    @BeforeTest
    public void beforeTest(){
        System.out.println("before test");
    }
    @AfterTest
    public void afterTest(){
        System.out.println("after test");
    }

}
基于类运行
===================================================
before suite
before test
before class
before method
test1
after method
before method
test2
after method
after class
after test
after suite

===============================================
只运行某个testcase
before suite
before test
before class
before method
test1
after method
after class
after test
after suite

===============================================
套件测试

创建如下工程

忽略测试
import org.testng.annotations.Test;

public class Ignore {

    @Test
    public void ignore1(){
        System.out.println("ignore1 success");
    }
    @Test(enabled = false)
    public void ignore2(){
        System.out.println("ignore1 success");
    }
}
# ignore2没有被执行
组测试中的方法分组测试
package com.course.testng.groups;

import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class GropsOnMethod {
    @Test(groups = "server")
    public void test1(){
        System.out.println("test1 server1");
    }
    @Test(groups = "server")
    public void test2(){
        System.out.println("test2 server1");
    }
    @Test(groups = "client")
    public void test3(){
        System.out.println("test1 client");
    }
    @Test(groups = "client")
    public void test4(){
        System.out.println("test2 client");
    }
    @BeforeGroups("server")
    public void test5(){
        System.out.println("group server before");
    }
    @AfterGroups("server")
    public void test6(){
        System.out.println("group server after");
    }
    @BeforeGroups("client")
    public void test7(){
        System.out.println("group client before");
    }
    @AfterGroups("client")
    public void tes8(){
        System.out.println("group client after");
    }
}

组测试中的类分组测试
#分别添加三个java文件 GroupsOnClass1 、GroupsOnClass2、GroupsOnClass3
package com.course.testng.groups;

import org.testng.annotations.Test;

@Test(groups = "stu")

public class GroupsOnClass1 {
    public void stu1(){
        System.out.println("Group on class1 : stu1");
    }
    public void stu2(){
        System.out.println("Group on class1 : stu2");
    }
}

package com.course.testng.groups;

import org.testng.annotations.Test;

@Test(groups = "stu")
public class GroupsOnClass2 {
    public void stu1(){
        System.out.println("Group on class2 : stu1");
    }
    public void stu2(){
        System.out.println("Group on class2 : stu2");
    }
}

package com.course.testng.groups;

import org.testng.annotations.Test;

@Test(groups = "teacher")
public class GroupsOnClass3 {
    public void stu1(){
        System.out.println("Group on class3 : teacher1");
    }
    public void stu2(){
        System.out.println("Group on class3 : teacher2");
    }
}


groups.xml文件如下配置


    
        
            
            
            
        
    

    
        
            
                
            
        
        
            
            
            
        
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936841.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号