天天看点

JAVA接口自动化(五)TestNG测试

(1)套件测试

suiteTest

package com.suning.basicAnnotation.suite;

import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;

public class suiteTest {
	@BeforeSuite
	public void beforeSuite(){
		System.out.println("----beforeSuite执行----");
	}
	
	@org.testng.annotations.AfterSuite
	public void AfterSuite(){
		System.out.println("----AfterSuite执行----");
	}
	
	@BeforeTest
	public void beforeTest(){
		System.out.println("----beforeTest执行----");
	}
	
	@org.testng.annotations.AfterTest
	public void AfterTest(){
		System.out.println("----AfterTest执行----");
	}
}	
           

loginTest

package com.suning.basicAnnotation.suite;

import org.testng.annotations.Test;

public class loginTest {
	@Test
	public void login(){
		System.out.println("苏宁易购登陆中----");
	}
}
           

payTest

package com.suning.basicAnnotation.suite;

import org.testng.annotations.Test;

public class payTest {
	@Test
	public void pay(){
		System.out.println("易付宝支付中----");
	}
}
           

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  	<test  name="login">
  		<classes>
  			<class name="com.suning.basicAnnotation.suite.suiteTest"/>
  			<class name="com.suning.basicAnnotation.suite.loginTest"/>	
  		</classes>
  	</test>
  
    <test  name="pay">
    	<classes>
  			<class name="com.suning.basicAnnotation.suite.suiteTest"/>
  			<class name="com.suning.basicAnnotation.suite.payTest"/>	
  		</classes>
	</test>
	
</suite> 
           

测试结果

[RemoteTestNG] detected TestNG version 6.14.2
----beforeSuite执行----
----beforeTest执行----
苏宁易购登陆中----
----AfterTest执行----
----beforeTest执行----
易付宝支付中----
----AfterTest执行----
----AfterSuite执行----

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
           

(2)忽略测试

ignoreTest

package com.suning.basicAnnotation.suite;

import org.testng.annotations.Test;

public class ignoreTest {
	
	@Test
	public void ignoreTest1(){
		System.out.println("忽略测试;ignoreTest1");
	}
	
	@Test(enabled=false)
	public void ignoreTest2(){
		System.out.println("忽略测试;ignoreTest2");
	}
	
	@Test(enabled=true)
	public void ignoreTest3(){
		System.out.println("忽略测试;ignoreTest3");
	}
}

           

测试结果

[RemoteTestNG] detected TestNG version 6.14.2
忽略测试;ignoreTest1
忽略测试;ignoreTest3
PASSED: ignoreTest1
PASSED: ignoreTest3

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
           

(3)分组测试

GroupsOnMethod

public class GroupsOnMethod {

    @Test(groups = "server")
    public void test1(){
        System.out.println("这是服务端组的测试方法11111");
    }

    @Test(groups = "server")
    public void test2(){
        System.out.println("这是服务端组的测试方法2222");
    }

    @Test(groups = "client")
    public void test3(){
        System.out.println("这是客户端组的测试方法33333");
    }
    @Test(groups = "client")
    public void test4(){
        System.out.println("这是客户端组的测试方法4444");
    }

    @BeforeGroups("server")
    public void beforeGroupsOnServer(){
        System.out.println("这是服务端组运行之前运行的方法");
    }

    @AfterGroups("server")
    public void afterGroupsOnServer(){
        System.out.println("这是服务端组运行之后运行的方法!!!!!");
    }

    @BeforeGroups("client")
    public void beforeGroupsOnClient(){
        System.out.println("这是客户端组运行之前运行的方法");
    }

    @AfterGroups("client")
    public void afterGroupsOnClient(){
        System.out.println("这是客户端组运行之后运行的方法!!!!!");
    }


}
           

GroupsOnClass1

@Test(groups = "stu")
public class GroupsOnClass1 {

    public void stu1(){
        System.out.println("GroupsOnClass1中的stu1111运行");
    }

    public void stu2(){
        System.out.println("GroupsOnClass1中的stu2222运行");
    }
}
           

GroupsOnClass2

@Test(groups = "stu")
public class GroupsOnClass2 {
    public void stu1(){
        System.out.println("GroupsOnClass222中的stu1运行");
    }

    public void stu2(){
        System.out.println("GroupsOnClass222中的stu2运行");
    }

}
           

GroupsOnClass3

@Test(groups = "teacher")
public class GroupsOnClass3 {

    public void teacher1(){
        System.out.println("GroupsOnClass3中的teacher1运行");
    }

    public void teacher2(){
        System.out.println("GroupsOnClass3中的teacher2运行");
    }

}
           

groupsOnClass.xml

<?xml version="1.0" encoding="UTF-8" ?>

<suite name="suitename">
    <test name="runAll">
        <classes>
            <class name="com.course.testng.groups.GroupsOnClass1"/>
            <class name="com.course.testng.groups.GroupsOnClass2"/>
            <class name="com.course.testng.groups.GroupsOnClass3"/>
        </classes>

    </test>

    <test name="onlyRunStu">
        <groups>
            <run>
                <include name="stu"/>
            </run>

        </groups>

        <classes>
            <class name="com.course.testng.groups.GroupsOnClass1"/>
            <class name="com.course.testng.groups.GroupsOnClass2"/>
            <class name="com.course.testng.groups.GroupsOnClass3"/>
        </classes>

    </test>
</suite>

           

(4)异常测试

什么时候需要异常测试:在我们期望结果为某一个异常的时候,比如:我们传入了某些不合法的参数,程序抛出了异常,也就是说我的语气结果就是这个异常。

public class ExpectedException {

//    这是一个测试结果会失败的异常测试

    @Test(expectedExceptions = RuntimeException.class)
    public void runTimeExceptionFailed(){
        System.out.println("这是一个失败的异常测试");
    }

//    这是一个成功的异常测试

    @Test(expectedExceptions = RuntimeException.class)
    public void runTimeExceptionSuccess(){
        System.out.println("这是我的异常测试");
        throw new RuntimeException();
    }

}
           

(5)依赖测试

public class DependTest {

    @Test
    public void test1(){
        System.out.println("test1 run");
        throw new RuntimeException();

    }

    @Test(dependsOnMethods = {"test1"})
    public void test2(){
        System.out.println("test2 run");
    }

}
           

test1方法执行失败,test2就不会执行

(6)参数化测试

ParamterTest

public class ParamterTest {

    @Test
    @Parameters({"name","age"})
    public void paramTest1(String name,int age){
        System.out.println("name = " + name + ";  age = " + age);
    }
}

           

DataProviderTest

public class DataProviderTest {

    @Test(dataProvider = "data")
    public void testDataProvider(String name,int age){
        System.out.println("name =" + name +"; age=" + age);
    }

    @DataProvider(name="data")
    public Object[][] providerData(){
        Object[][] o = new Object[][]{
                {"zhangsan",10},
                {"lisi",20},
                {"wangwu",30}
        };

        return o;
    }

    @Test(dataProvider = "methodData")
    public void test1(String name,int age){
        System.out.println("test111方法 name="+name+";age="+age);
    }
    @Test(dataProvider = "methodData")
    public void test2(String name,int age){
        System.out.println("test222方法 name="+name+";age="+age);
    }

    @DataProvider(name="methodData")
    public Object[][] methodDataTest(Method method){
        Object[][] result=null;

        if(method.getName().equals("test1")){
            result = new Object[][]{
                    {"zhangsan",20},
                    {"lisi",25}
            };
        }else if(method.getName().equals("test2")){
            result = new Object[][]{
                    {"wangwu",50},
                    {"zhaoliu",60}
            };
        }

        return result;
    }
}
           

Parameter.xml

<?xml version="1.0" encoding="UTF-8" ?>

<suite name="parameter">
    <test name="param">

        <classes>
            <parameter name="name" value="zhangsan"/>
            <parameter name="age" value="10"/>


            <class name="com.course.testng.paramter.ParamterTest"/>
        </classes>

    </test>

</suite>

           

(7)多线程测试

MultiThreadOnXml

public class MultiThreadOnXml {

    @Test
    public void test1(){
        System.out.printf("Thread Id : %s%n",Thread.currentThread().getId());
    }

    @Test
    public void test2(){
        System.out.printf("Thread Id : %s%n",Thread.currentThread().getId());
    }

    @Test
    public void test3(){
        System.out.printf("Thread Id : %s%n",Thread.currentThread().getId());
    }

}
           

MultiThreadOnAnnotion

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

}

           

MultiThread.xml

<?xml version="1.0" encoding="UTF-8" ?>

<suite name="thread" parallel="methods" thread-count="3">

    <!--
    tests级别:不同的test tag下的用例可以在不同的线程下执行
            相同的test tag下的用例只能在同一个线程中去执行
    classs级别:相同的class tag 下的用例在同一个线程中执行
                不同的class tag 下的用例可以在不同的线程中执行
    methods级别:所有用例都可以在不同的线程下去执行

    thread-count:代表了最大并发线程数

    xml文件配置这种方式不能指定线程池,只有方法上才可以指定线程池
    -->

    <test name = "demo1">
        <classes name="d">
            <class name="com.course.testng.multiThread.MultiThreadOnXml"/>
            <class name="com.course.testng.BasicAnnotation"/>
            <class name="com.course.testng.multiThread.MultiThreadOnXml"/>
        </classes>
        <classes name="d1">
            <class name="com.course.testng.multiThread.MultiThreadOnXml"/>
            <class name="com.course.testng.BasicAnnotation"/>
            <class name="com.course.testng.multiThread.MultiThreadOnXml"/>
        </classes>

    </test>

    <test name = "demo2">
        <classes name="d3">
            <class name="com.course.testng.BasicAnnotation"/>
        </classes>

    </test>
</suite>

           

(8)超时测试

TimeOutTest

public class TimeOutTest {

    @Test(timeOut = 3000)//单位为毫秒值
    public void testSuccess() throws InterruptedException {
        Thread.sleep(2000);
    }

    @Test(timeOut = 2000)
    public void testFailed() throws InterruptedException {
        Thread.sleep(3000);
    }
}