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文件 GroupsOnClass1GroupsOnClass2GroupsOnClass3
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文件如下配置
<?xml version="1.0" encoding="utf-8" ?>
<suite name="test">
    <test name="groupclass">
        <classes>
            <class name="com.course.testng.grops.GroupsOnClass1" />
            <class name="com.course.testng.grops.GroupsOnClass2" />
            <class name="com.course.testng.grops.GroupsOnClass3" />
        </classes>
    </test>

    <test name="onlyRunStu">
        <groups>
            <run>
                <include name="stu" />
            </run>
        </groups>
        <classes>
            <class name="com.course.testng.grops.GroupsOnClass1" />
            <class name="com.course.testng.grops.GroupsOnClass2" />
            <class name="com.course.testng.grops.GroupsOnClass3" />
        </classes>
    </test>
</suite>

多线程测试

实现方式一通过注解实现:

package com.course.testng.multiThread;

import org.testng.annotations.Test;

public class MultiThreadOnAnnotion {
    @Test(invocationCount = 10,threadPoolSize = 3)
    public void test(){
        System.out.println(1);
        System.out.printf("Thread id: %s%n",Thread.currentThread().getId());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值