【自用】3.测试框架TestNG

1.TestNG基本介绍

2.注解

2.1 @Test

package com.course.testng;
import org.testng.annotations.Test;
public class BasicAnnotation {
    //最基本的注解,用来把方法标记为测试的一部分
    @Test
    public void testCase1(){
        System.out.println("这是测试用例1");

    }
}

运行结果:

这是测试用例1

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

2.2 BeforeClass和AfterClass

package com.course.testng;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class BasicAnnotation {
    //最基本的注解,用来把方法标记为测试的一部分
    @Test
    public void testCase1(){
        System.out.println("这是测试用例1");

    }
    @BeforeMethod
    public void beforeMethod(){
        System.out.println("在测试方法之前运行");
    }
    @AfterMethod
    public void afterMethod(){
        System.out.println("在测试方法之后运行");
    }
}

运行结果:

在测试方法之前运行
这是测试用例1
在测试方法之后运行

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

跑两个用例的情况:

package com.course.testng;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class BasicAnnotation {
    //最基本的注解,用来把方法标记为测试的一部分
    @Test
    public void testCase1(){
        System.out.println("这是测试用例1");

    }
    @Test
    public void testCase2(){
        System.out.println("这是测试用例2");

    }
    @BeforeMethod
    public void beforeMethod(){
        System.out.println("在测试方法之前运行");
    }
    @AfterMethod
    public void afterMethod(){
        System.out.println("在测试方法之后运行");
    }
}

运行结果:

在测试方法之前运行
这是测试用例1
在测试方法之后运行
在测试方法之前运行
这是测试用例2
在测试方法之后运行

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


Process finished with exit code 0

类前后:

package com.course.testng;
import org.testng.annotations.*;

public class BasicAnnotation {
    //最基本的注解,用来把方法标记为测试的一部分
    @Test
    public void testCase1(){
        System.out.println("这是测试用例1");

    }
    @Test
    public void testCase2(){
        System.out.println("这是测试用例2");

    }
    @BeforeMethod
    public void beforeMethod(){
        System.out.println("在测试方法之前运行");
    }
    @AfterMethod
    public void afterMethod(){
        System.out.println("在测试方法之后运行");
    }
    @BeforeClass
    public void beforeClass(){
        System.out.println("类运行之前运行的方法");
    }
    @AfterClass
    public void afterClass(){
        System.out.println("类运行之后运行的方法");
    }
}

运行结果:

类运行之前运行的方法
在测试方法之前运行
这是测试用例1
在测试方法之后运行
在测试方法之前运行
这是测试用例2
在测试方法之后运行
类运行之后运行的方法

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


Process finished with exit code 0

2.3 BeforeSuite和AfterSuite

类运行之前运行的方法
在测试方法之前运行
这是测试用例1
在测试方法之后运行
在测试方法之前运行
这是测试用例2
在测试方法之后运行
类运行之后运行的方法

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


Process finished with exit code 0

运行结果:

beforesuite测试套件
类运行之前运行的方法
在测试方法之前运行
这是测试用例1
在测试方法之后运行
在测试方法之前运行
这是测试用例2
在测试方法之后运行
类运行之后运行的方法
aftersuite测试套件

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


Process finished with exit code 0

3.套件测试

package com.course.testng.suite;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class SuiteConfig {
    @BeforeSuite
    public void beforeSuite(){
        System.out.println("before suite运行");
    }
    @AfterSuite
    public void afterSuite(){
        System.out.println("after suite运行");
    }
}

package com.course.testng.suite;

import org.testng.annotations.Test;

public class LoginTest {
    @Test
    public void loginTaoBao(){
        System.out.println("淘宝登录成功");
    }
}

package com.course.testng.suite;

import org.testng.annotations.Test;

public class PayTest {
    @Test
    public void paySuccess(){
        System.out.println("支付成功");
    }
}

<?xml version="1.0" encoding="UTF-8" ?>
<suite name="test">
    <test name="login">
        <classes>
            <class name="com.course.testng.suite.SuiteConfig"/>
            <class name="com.course.testng.suite.LoginTest"/>


        </classes>
    </test>
    <test name="pay">
        <classes>
            <class name="com.course.testng.suite.SuiteConfig"/>
            <class name="com.course.testng.suite.PayTest"/>


        </classes>

    </test>

</suite>
before suite运行
淘宝登录成功
支付成功
after suite运行

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


Process finished with exit code 0

4.忽略测试

package com.course.testng;

import org.testng.annotations.Test;

public class IgnoreTest {
    @Test
    public void ignore1(){
        System.out.println("ignore1 执行");
    }
    @Test(enabled = false)
    public void ignore2(){
        System.out.println("ignore2 执行");
    }
}

运行结果:

ignore1 执行

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

默认enable=true

package com.course.testng;

import org.testng.annotations.Test;

public class IgnoreTest {
    @Test
    public void ignore1(){
        System.out.println("ignore1 执行");
    }
    @Test(enabled = false)
    public void ignore2(){
        System.out.println("ignore2 执行");
    }
    @Test(enabled = true)
    public void ignore3(){
        System.out.println("ignore3 执行");
    }
}
ignore1 执行
ignore3 执行

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


Process finished with exit code 0

5.组测试中中的方法分组测试

package com.course.testng.groups;

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

public class GroupsOnMethod {
    @Test(groups = "server")
    public void test1(){
        System.out.println("服务端组测试方法1");
    }
    @Test(groups = "server")
    public void test2(){
        System.out.println("服务端组测试方法2");
    }
    @Test(groups = "client")
    public void test3(){
        System.out.println("客户端组测试方法3");
    }
    @Test(groups = "client")
    public void test4(){
        System.out.println("客户端组测试方法4");
    }
    @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("客户端组运行后运行的方法");
    }
}

服务端组运行前运行的方法
服务端组测试方法1
服务端组测试方法2
服务端运行后运行的方法
客户端组运行前运行的方法
客户端组测试方法3
客户端组测试方法4
客户端组运行后运行的方法

===============================================
Default Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

6.组测试中的类分组测试

package com.course.testng.groups;

import org.testng.annotations.Test;

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

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

package com.course.testng.groups;

import org.testng.annotations.Test;

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

package com.course.testng.groups;

import org.testng.annotations.Test;

@Test(groups = "teacher")
public class GroupsOnClass3 {
    public void teacher1(){
        System.out.println("GroupsOnClass3中的teacher1运行");
    }
    public void teacher2(){
        System.out.println("GroupsOnClass3中的teacher2运行");
    }
}

<?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="onlRunStu">
        <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>
GroupsOnClass1中的stu1运行
GroupsOnClass1中的stu2运行
GroupsOnClass2中的stu1运行
GroupsOnClass2中的stu2运行
GroupsOnClass3中的teacher1运行
GroupsOnClass3中的teacher2运行
GroupsOnClass1中的stu1运行
GroupsOnClass1中的stu2运行
GroupsOnClass2中的stu1运行
GroupsOnClass2中的stu2运行

===============================================
suitename
Total tests run: 10, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

7.异常测试

package com.course.testng.suite;

import org.testng.annotations.Test;

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();
    }
}
这是一个成功的异常测试

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

8.依赖测试

package com.course.testng.suite;

import org.testng.annotations.Test;

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 run
test2 run

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


Process finished with exit code 0
package com.course.testng.suite;

import org.testng.annotations.Test;

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 run

java.lang.RuntimeException
	at com.course.testng.suite.DependTest.test1(DependTest.java:9)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.privateRun(TestRunner.java:648)
	at org.testng.TestRunner.run(TestRunner.java:505)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
	at org.testng.SuiteRunner.run(SuiteRunner.java:364)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
	at org.testng.TestNG.runSuites(TestNG.java:1049)
	at org.testng.TestNG.run(TestNG.java:1017)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)


Test ignored.

9.参数化测试

9.1xml文件参数化

package com.course.testng.paramter;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParameterTest {
    @Test
    @Parameters({"name","age"})
    public void parameterTest1(String name,int age){
        System.out.println("name:"+name+",age"+age);
    }
}

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

<suite name="parameter">

    <test name="para">
        <classes>
            <parameter name="name" value="zhangsan"/>
            <parameter name="age" value= "10"/>
            <class name="com.course.testng.paramter.ParameterTest"/>
        </classes>
    </test>
</suite>
name:zhangsan,age10

===============================================
parameter
Total tests run: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

9.2DataProvider参数化

package com.course.testng.paramter;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

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 } ,
                {"zhangshan", 11 } ,
                {"zhangsang", 101 }
        } ;
        return o;
    }
}
name =zhangsan,age =10
name =zhangshan,age =11
name =zhangsang,age =101

===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0
package com.course.testng.paramter;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

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 } ,
                {"zhangshan", 11 } ,
                {"zhangsang", 101 }
        } ;
        return o;
    }
    @Test(dataProvider = "methodData")
    public void test1(String name,int age){
        System.out.println("test1方法 name="+name+",age ="+age);
    }
    @Test(dataProvider = "methodData")
    public void test2(String name,int age){
        System.out.println("test2方法 name="+name+",age ="+age);
    }
    @DataProvider(name="methodData")
    public Object [] [] methodDataTest(Method method){
        Object [][] result= null;
        if (method.getName().equals("test1")){
            result= new Object[][]{
                    {"zhangsan", 10 } ,
                    {"shan", 17 }
            };
        }else if (method.getName().equals("test2")){
            result= new Object[][]{
                    {"zhang", 33 } ,
                    {"zshan", 87 }
            };
        }
        return result;
    }
}

test1方法 name=zhangsan,age =10
test1方法 name=shan,age =17
test2方法 name=zhang,age =33
test2方法 name=zshan,age =87
name =zhangsan,age =10
name =zhangshan,age =11
name =zhangsang,age =101

===============================================
Default Suite
Total tests run: 7, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

10.多线程测试

10.1注解方式实现

package com.course.testng.multiThread;

import org.testng.annotations.Test;

public class MultiThreadOnAnnotation {
    @Test(invocationCount = 5)
    public void test(){
        System.out.println(1);
    }
}
1
1
1
1
1

===============================================
Default Suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

package com.course.testng.multiThread;

import org.testng.annotations.Test;

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

1
Thread Id: 15
1
Thread Id: 14
1
Thread Id: 14
1
Thread Id: 14
1
Thread Id: 14
1
Thread Id: 14
1
Thread Id: 14
1
Thread Id: 14
1
Thread Id: 14
1
Thread Id: 13

===============================================
Default Suite
Total tests run: 10, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

10.2xml文件实现

package com.course.testng.multiThread;

import org.testng.annotations.Test;

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

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

<suite name="thread" parallel="classes" thread-count="2">
    <!--
    tests级别:不同的test tag下的用例可以在不同的线程下执行
            相同的test tag下的用例只能在同一个线程中去执行
    classes:相同的class tag下的用例在同一个线程中去执行
            不同的class tag下的用例可以在不同的线程下执行
    methods级别:所有用例都可以在不同的线程下去执行
    thread-count: 最大并发线程数
    xml文件配置这种方式不能指定线程池,只有方法上才可以指定线程池
    -->
    <test name="demo1">
        <classes>
            <class name="com.course.testng.multiThread.MultiThreadOnXml"/>
        </classes>
    </test>

    <test name="demo2">
        <classes>
            <class name="com.course.testng.multiThread.MultiThreadOnXml"/>
        </classes>

    </test>
</suite>

11.超时测试

package com.course.testng.suite;

import org.testng.annotations.Test;

public class TimeOutTest {
    @Test(timeOut = 3000)
    public void testSuccess() throws InterruptedException{
        Thread.sleep(2000);

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

    }
}

org.testng.internal.thread.ThreadTimeoutException: Method com.course.testng.suite.TimeOutTest.testFailure() didn't finish within the time-out 2000

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


Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值