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

自动化框架设计

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

TestNG介绍

  • 比Junit涵盖功能更全面的测试框架
  • 比Junit更适合隔离性较强的单元测试
  • TestNG更适合复杂的集成测试
  • TestNG使用
    java接口自动化【第三篇】自动化框架设计及TestNG注解的使用_第1张图片

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

===============================================

套件测试

创建如下工程
java接口自动化【第三篇】自动化框架设计及TestNG注解的使用_第2张图片

忽略测试

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());
    }
}

你可能感兴趣的:(java接口自动化,java,自动化,单元测试)