第十六章 TestNG设置-框架

TestNG框架

1.介绍:

TestNG 是一个来自 JUnit 和 NUnit 的测试框架,它具拥有更多的功能,提高了 执行的效率。

并且是一个开源的自动化测试框架 TestNG 去除了老框架的大部分限制,借助于简单的注释、分组、排序和参数化这些功能,使脚本开发人员能够编写更灵活、更强大的测试用例脚本。

TestNG 的主要优点是:

a:可以生成日志

b:注释使代码高效且易于管理

c:能够生成执行的 HTML 报告

d:测试用例可以分组并按优先级排序

e:可以实现并行测试

f:可以实现数据参数化

TestNG 的注解:

@BeforeSuite: 使用这个注解的方法会在 test suite 中的所有 test 运行之前运行

@AfterSuite: 使用这个注解的方法会在 test suite 中的所有 test 运行之后运行

@BeforeTest: 使用这个注解的方法会在 xml 文件中的 test 标签中的每个 test 方法运行之前运行 @AfterTest: 使用这个注解的方法会在 xml 文件中的 test 标签中的每个 test 方法运行之后运 行@BeforeClass: 使用这个注解的方法会在当前这个类的第一个 test 方法运行之前运行

@AfterClass: 使用这个注解的方法会在当前这个类的最后一个 test 方法运行之后运行 @BeforeMethod: 使用这个注解的方法会在每个 test 方法运行之前运行。

@AfterMethod: 使用这个注解的方法会在每个 test 方法运行之后运行。

@Test: 使用这个注解的方法就是我们要执行的测试用例的代码

使用注解的好处:

1.TestNG 通过查找注解来标识对应的方法。所以不依赖于方法名来识别

2.我们可以将参数传递给注解。

3.注解是强类型的,所以有任何错误编译器会立即给出提示。

4.测试类不再需要扩展任何东西(例如,JUnit 3 的测试用例

Log4j定义了8个级别的log(除去OFF和ALL,可以说分为6个级别),优先级从高到低依次为:OFF、FATAL、ERROR、WARN、INFO、DEBUG、TRACE、 ALL。
2.TestNG注解

被测试类

package ClassTbc;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/19
 */
public class ClassToBeTest {
    public int sum(int a,int b){
        return (a+b);
    }
}

按照方法名字的字母顺序

package TestClass;

import ClassTbc.ClassToBeTest;
import org.apache.logging.log4j.LogManager;
import org.testng.annotations.Test;
import org.apache.logging.log4j.Logger;
/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/19
 */
public class TestDemo1 {
   private static  final  Logger Log = LogManager.getLogger(TestDemo1.class.getName());
    @Test
    public void test01() {
        ClassToBeTest classToBeTest = new ClassToBeTest();
        int sum = classToBeTest.sum(4, 5);
        System.out.println("sum=" + sum);
        Log.info("测试优秀");

    }
    @Test
    public void test02() {
        System.out.println();
    }

    @Test
    public void test03() {
        System.out.println();
    }

}
3.断言

常用断言:

Assert.assertEquals():判断是否相等,相等方法继续执行,否则方法执行结束

Assert.assertTrue(); 判断是否为真,为true,方法继续执行,否则方法执行结束

Assert.assertFalse();判断是否为假,为false,方法继续执行,否则方法执行结束

package TestClass;

import ClassTbc.ClassToBeTest;
import org.apache.logging.log4j.LogManager;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.apache.logging.log4j.Logger;
/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/19
 */
public class TestDemo1 {
   private static  final  Logger Log = LogManager.getLogger(TestDemo1.class.getName());
    @Test
    public void test01() {
        ClassToBeTest classToBeTest = new ClassToBeTest();
        int result =9;
        int sum = classToBeTest.sum(4, 5);
        Assert.assertEquals(sum,result);
        System.out.println("test01执行完成");

    }
    @Test
    public void test02() {
        ClassToBeTest classToBeTest02 = new ClassToBeTest();
        String destination = "Hello World";
        String sum = classToBeTest02.sum("Hello", "World");
        Assert.assertEquals(sum,destination);
        System.out.println("test02执行完成");
    }

    @Test
    public void test03() {
        ClassToBeTest classToBeTest03 = new ClassToBeTest();
        int[] a={1,2,3,4};
        int[] sum = classToBeTest03.nums();
        Assert.assertEquals(sum,a);
        System.out.println("test03执行完成");
    }

}
3.软断言(SoftAssert)

软断言即使断言失败依旧会继续将方法执行完成

softAssert.assertAll();将会将所有assert执行一遍,提出报错信息

package TestClass;

import ClassTbc.ClassToBeTest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/19
 */
public class TestDemo2 {
   private static  final  Logger Log = LogManager.getLogger(TestDemo2.class.getName());
   SoftAssert softAssert = new SoftAssert();//新建一个软断言类
    @Test
    public void test03() {
        ClassToBeTest classToBeTest03 = new ClassToBeTest();
        int[] a={1,2,3,4};
        int[] b={1,2,3};
        int[] sum = classToBeTest03.nums();
        softAssert.assertEquals(sum,a);
        System.out.println("test03第一个数组比较完成执行完成");
        softAssert.assertEquals(sum,b);
        System.out.println("test03执行完成");
        softAssert.assertAll();
    }
}
4.方法和类注释(Beforeclass Afterclass Beforemethod Aftermethod)
package TestClass;

import ClassTbc.ClassToBeTest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.*;
import org.testng.asserts.SoftAssert;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/19
 */
public class TestDemo3 {
   private static  final  Logger Log = LogManager.getLogger(TestDemo3.class.getName());
   SoftAssert softAssert = new SoftAssert();
   @BeforeClass
    public void beforeclass(){
       System.out.println("类执行之前");
   }

    @AfterClass
    public void afterclass(){
        System.out.println("类执行之后");
    }

    @BeforeMethod
    public void beforeMethod(){
        System.out.println("方法执行之前");
    }

    @AfterMethod
    public void afterMethod(){
        System.out.println("方法执行之后");
    }
   @Test
    public void test01() {
        SoftAssert softAssert = new SoftAssert();
        ClassToBeTest classToBeTest = new ClassToBeTest();
        int result =9;
        int sum = classToBeTest.sum(4, 5);
        softAssert.assertEquals(sum,result);
        System.out.println("test01执行完成");

    }
    @Test
    public void test02() {
        ClassToBeTest classToBeTest02 = new ClassToBeTest();
        String destination = "Hello World";
        String sum = classToBeTest02.sum("Hello", "World");
        softAssert.assertEquals(sum,destination);
        System.out.println("test02执行完成");
    }

    @Test
    public void test03() {
        ClassToBeTest classToBeTest03 = new ClassToBeTest();
        int[] a={1,2,3,4};
        int[] b={1,2,3};
        int[] sum = classToBeTest03.nums();
        softAssert.assertEquals(sum,a);
        System.out.println("test03第一个数组比较完成执行完成");
        softAssert.assertEquals(sum,b);
        System.out.println("test03执行完成");
        softAssert.assertAll();
    }
}

运行结果:

类执行之前
方法执行之前
test01执行完成
方法执行之后
方法执行之前
test02执行完成
方法执行之后
方法执行之前
test03第一个数组比较完成执行完成
test03执行完成
java.lang.AssertionError: The following asserts failed:
	 expected [4] but found [3]
Expected :4
Actual   :3
<Click to see difference>
	at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:43)
	at TestClass.TestDemo3.test03(TestDemo3.java:65)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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)
方法执行之后
类执行之后
===============================================
Default Suite
Total tests run: 3, Failures: 1, Skips: 0
===============================================
Process finished with exit code 0
5.将多个类加入到TestSuite中

通过xml创建suite testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression Test">
    <test name="Application name">
        <classes>
            <class name="TestClass.TestDemo3"></class>
            <class name="TestClass.TestDemo4"></class>
        </classes>

    </test>
</suite>

要执行的类:

package TestClass;

import ClassTbc.ClassToBeTest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.*;
import org.testng.asserts.SoftAssert;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/19
 */
public class TestDemo3 {
   private static  final  Logger Log = LogManager.getLogger(TestDemo3.class.getName());
   SoftAssert softAssert = new SoftAssert();
   @BeforeClass
    public void beforeclass(){
       System.out.println("TestDemo3-类执行之前");
   }

    @AfterClass
    public void afterclass(){
        System.out.println("TestDemo3-类执行之后");
    }

    @BeforeMethod
    public void beforeMethod(){
        System.out.println("TestDemo3-beforeMethod()-方法执行之前");
    }

    @AfterMethod
    public void afterMethod(){
        System.out.println("TestDemo3-AfterMethod-方法执行之后");
    }
   @Test
    public void test01() {
        SoftAssert softAssert = new SoftAssert();
        ClassToBeTest classToBeTest = new ClassToBeTest();
        int result =9;
        int sum = classToBeTest.sum(4, 5);
        softAssert.assertEquals(sum,result);
        System.out.println("TestDemo3-test01执行完成");

    }
    @Test
    public void test02() {
        ClassToBeTest classToBeTest02 = new ClassToBeTest();
        String destination = "Hello World";
        String sum = classToBeTest02.sum("Hello", "World");
        softAssert.assertEquals(sum,destination);
        System.out.println("TestDemo3-test02执行完成");
    }

    @Test
    public void test03() {
        ClassToBeTest classToBeTest03 = new ClassToBeTest();
        int[] a={1,2,3,4};
        int[] b={1,2,3};
        int[] sum = classToBeTest03.nums();
        softAssert.assertEquals(sum,a);
        System.out.println("TestDemo3-test03第一个数组比较完成执行完成");
        softAssert.assertEquals(sum,b);
        System.out.println("TestDemo3-test03执行完成");
        softAssert.assertAll();
    }
}

package TestClass;

import ClassTbc.ClassToBeTest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.*;
import org.testng.asserts.SoftAssert;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/19
 */
public class TestDemo4 {
    private static  final  Logger Log = LogManager.getLogger(TestDemo3.class.getName());
    SoftAssert softAssert = new SoftAssert();
    @BeforeClass
    public void beforeclass(){
        System.out.println("TestDemo4-类执行之前");
    }

    @AfterClass
    public void afterclass(){
        System.out.println("TestDemo4-类执行之后");
    }

    @BeforeMethod
    public void beforeMethod(){
        System.out.println("TestDemo4-beforeMethod()-方法执行之前");
    }

    @AfterMethod
    public void afterMethod(){
        System.out.println("TestDemo4-AfterMethod-方法执行之后");
    }
    @Test
    public void test01() {
        SoftAssert softAssert = new SoftAssert();
        ClassToBeTest classToBeTest = new ClassToBeTest();
        int result =9;
        int sum = classToBeTest.sum(4, 5);
        softAssert.assertEquals(sum,result);
        System.out.println("TestDemo4-test01执行完成");

    }
    @Test
    public void test02() {
        ClassToBeTest classToBeTest02 = new ClassToBeTest();
        String destination = "Hello World";
        String sum = classToBeTest02.sum("Hello", "World");
        softAssert.assertEquals(sum,destination);
        System.out.println("TestDemo4-test02执行完成");
    }

    @Test
    public void test03() {
        ClassToBeTest classToBeTest03 = new ClassToBeTest();
        int[] a={1,2,3,4};
        int[] b={1,2,3};
        int[] sum = classToBeTest03.nums();
        softAssert.assertEquals(sum,a);
        System.out.println("TestDemo4-test03第一个数组比较完成执行完成");
        softAssert.assertEquals(sum,b);
        System.out.println("TestDemo4-test03执行完成");
        softAssert.assertAll();
    }
}

执行结果:

TestDemo3-类执行之前
TestDemo3-beforeMethod()-方法执行之前
TestDemo3-test01执行完成
TestDemo3-AfterMethod-方法执行之后
TestDemo3-beforeMethod()-方法执行之前
TestDemo3-test02执行完成
TestDemo3-AfterMethod-方法执行之后
TestDemo3-beforeMethod()-方法执行之前
TestDemo3-test03第一个数组比较完成执行完成
TestDemo3-test03执行完成
java.lang.AssertionError: The following asserts failed:
	 expected [4] but found [3]
Expected :4
Actual   :3
<Click to see difference>
	at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:43)
	at TestClass.TestDemo3.test03(TestDemo3.java:65)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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)
TestDemo3-AfterMethod-方法执行之后
TestDemo3-类执行之后
TestDemo4-类执行之前
TestDemo4-beforeMethod()-方法执行之前
TestDemo4-test01执行完成
TestDemo4-AfterMethod-方法执行之后
TestDemo4-beforeMethod()-方法执行之前
TestDemo4-test02执行完成
TestDemo4-AfterMethod-方法执行之后
TestDemo4-beforeMethod()-方法执行之前
TestDemo4-test03第一个数组比较完成执行完成
TestDemo4-test03执行完成
java.lang.AssertionError: The following asserts failed:
	 expected [4] but found [3]
Expected :4
Actual   :3
<Click to see difference>
	at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:43)
	at TestClass.TestDemo4.test03(TestDemo4.java:65)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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)
TestDemo4-AfterMethod-方法执行之后
TestDemo4-类执行之后
===============================================
Regression Test
Total tests run: 6, Failures: 2, Skips: 0
===============================================
Process finished with exit code 0
6.BeforeSuite AfterSuite BeforeTest AfterSuite

Basemethod

package BaseMethod;

import org.testng.annotations.*;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/19
 */
public class Basemethod {
    @BeforeSuite
    public void BeforeSuite(){
        System.out.println("BaseMethod-BeforeSuite-方法执行之前");
    }

    @AfterSuite
    public void AfterSuite(){
        System.out.println("BaseMethod-AfterSuite-方法执行之后");
    }
    @BeforeClass
    public void BeforeClass(){
        System.out.println("BaseMethod-BeforeClass-方法执行之前");
    }
    @AfterClass
    public void AfterClass(){
        System.out.println("BaseMethod-AfterClass-方法执行之后");
    }

    @BeforeTest
    public void BeforeTest(){
        System.out.println("BaseMethod- BeforeTest-方法执行之前");
    }

    @AfterTest
    public void AfterTest(){
        System.out.println("BaseMethod-AfterTest-方法执行之后");
    }
}

Testdemo1

package TestBaseMethod;

import BaseMethod.Basemethod;
import ClassTbc.ClassToBeTest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/19
 */
public class TestDemo1 extends Basemethod {
   private static  final  Logger Log = LogManager.getLogger(TestDemo1.class.getName());
    SoftAssert softAssert = new SoftAssert();
    @Test
    public void test01() {
        ClassToBeTest classToBeTest = new ClassToBeTest();
        int result =9;
        int sum = classToBeTest.sum(4, 5);
        Assert.assertEquals(sum,result);
        System.out.println("TestDemo2-test01执行完成");

    }
    @Test
    public void test02() {
        ClassToBeTest classToBeTest02 = new ClassToBeTest();
        String destination = "Hello World";
        String sum = classToBeTest02.sum("Hello", "World");
        Assert.assertEquals(sum,destination);
        System.out.println("TestDemo2-test02执行完成");
    }

    @Test
    public void test03() {
        ClassToBeTest classToBeTest03 = new ClassToBeTest();
        int[] a={1,2,3,4};
        int[] sum = classToBeTest03.nums();
        Assert.assertEquals(sum,a);
        System.out.println("TestDemo2-test03执行完成");
    }

}

TestDemo2

package TestBaseMethod;

import BaseMethod.Basemethod;
import ClassTbc.ClassToBeTest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/19
 */
public class TestDemo2 extends Basemethod {
   private static  final  Logger Log = LogManager.getLogger(TestDemo2.class.getName());
   SoftAssert softAssert = new SoftAssert();
   @Test
    public void test01() {
        ClassToBeTest classToBeTest = new ClassToBeTest();
        int result =9;
        int sum = classToBeTest.sum(4, 5);
        softAssert.assertEquals(sum,result);
        System.out.println("TestDemo2-test01执行完成");

    }
    @Test
    public void test02() {
        ClassToBeTest classToBeTest02 = new ClassToBeTest();
        String destination = "Hello World";
        String sum = classToBeTest02.sum("Hello", "World");
        softAssert.assertEquals(sum,destination);
        System.out.println("TestDemo2--test02执行完成");
    }

    @Test
    public void test03() {
        ClassToBeTest classToBeTest03 = new ClassToBeTest();
        int[] a={1,2,3,4};
        int[] b={1,2,3};
        int[] sum = classToBeTest03.nums();
        softAssert.assertEquals(sum,a);
        System.out.println("TestDemo2--test03第一个数组比较完成执行完成");
        softAssert.assertEquals(sum,b);
        System.out.println("TestDemo2--test03执行完成");
        softAssert.assertAll();
    }
}

testng-Complete:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression Test">
    <test name="Application test1">
        <classes>
            <class name="TestBaseMethod.TestDemo1"></class>
        </classes>
    </test>
    <test name="Application test2">
        <classes>
            <class name="TestBaseMethod.TestDemo1"></class>
        </classes>
    </test>
</suite>

执行结果:

BaseMethod-BeforeSuite-方法执行之前

BaseMethod- BeforeTest-方法执行之前

BaseMethod-BeforeClass-方法执行之前

TestDemo2-test01执行完成

TestDemo2-test02执行完成

java.lang.AssertionError: arrays don't have the same size.  expected [4] but found [3]
Expected :4
Actual   :3
<Click to see difference>
	at org.testng.Assert.fail(Assert.java:96)
	at org.testng.Assert.failNotEquals(Assert.java:776)
	at org.testng.Assert.assertEqualsImpl(Assert.java:137)
	at org.testng.Assert.assertEquals(Assert.java:118)
	at org.testng.Assert.assertEquals(Assert.java:652)
	at org.testng.Assert.checkRefEqualityAndLength(Assert.java:430)
	at org.testng.Assert.assertEquals(Assert.java:250)
	at org.testng.Assert.assertEquals(Assert.java:238)
	at TestBaseMethod.TestDemo1.test03(TestDemo1.java:42)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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)
BaseMethod-AfterClass-方法执行之后
BaseMethod-AfterTest-方法执行之后
BaseMethod- BeforeTest-方法执行之前
BaseMethod-BeforeClass-方法执行之前
TestDemo2-test01执行完成
TestDemo2-test02执行完成
java.lang.AssertionError: arrays don't have the same size.  expected [4] but found [3]
Expected :4
Actual   :3
<Click to see difference>
	at org.testng.Assert.fail(Assert.java:96)
	at org.testng.Assert.failNotEquals(Assert.java:776)
	at org.testng.Assert.assertEqualsImpl(Assert.java:137)
	at org.testng.Assert.assertEquals(Assert.java:118)
	at org.testng.Assert.assertEquals(Assert.java:652)
	at org.testng.Assert.checkRefEqualityAndLength(Assert.java:430)
	at org.testng.Assert.assertEquals(Assert.java:250)
	at org.testng.Assert.assertEquals(Assert.java:238)
	at TestBaseMethod.TestDemo1.test03(TestDemo1.java:42)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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)
BaseMethod-AfterClass-方法执行之后
BaseMethod-AfterTest-方法执行之后
BaseMethod-AfterSuite-方法执行之后
===============================================
Regression Test
Total tests run: 6, Failures: 2, Skips: 0
===============================================
Process finished with exit code 0

7.Test(priority = ?)

按照优先级进行

package TestClass;

import ClassTbc.ClassToBeTest;
import org.apache.logging.log4j.LogManager;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.apache.logging.log4j.Logger;
/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/19
 */
public class TestDemo1 {
   private static  final  Logger Log = LogManager.getLogger(TestDemo1.class.getName());
    @Test(priority = 2)
    public void test01() {
        ClassToBeTest classToBeTest = new ClassToBeTest();
        int result =9;
        int sum = classToBeTest.sum(4, 5);
        Assert.assertEquals(sum,result);
        System.out.println("test01执行完成");

    }
    @Test(priority = 1)
    public void test02() {
        ClassToBeTest classToBeTest02 = new ClassToBeTest();
        String destination = "Hello World";
        String sum = classToBeTest02.sum("Hello", "World");
        Assert.assertEquals(sum,destination);
        System.out.println("test02执行完成");
    }

    @Test(priority = 3)
    public void test03() {
        ClassToBeTest classToBeTest03 = new ClassToBeTest();
        int[] a={1,2,3,4};
        int[] sum = classToBeTest03.nums();
        Assert.assertEquals(sum,a);
        System.out.println("test03执行完成");
    }

}

执行结果

test02执行完成
test01执行完成
java.lang.AssertionError: arrays don't have the same size.  expected [4] but found [3]
Expected :4
Actual   :3
<Click to see difference>
	at org.testng.Assert.fail(Assert.java:96)
	at org.testng.Assert.failNotEquals(Assert.java:776)
	at org.testng.Assert.assertEqualsImpl(Assert.java:137)
	at org.testng.Assert.assertEquals(Assert.java:118)
	at org.testng.Assert.assertEquals(Assert.java:652)
	at org.testng.Assert.checkRefEqualityAndLength(Assert.java:430)
	at org.testng.Assert.assertEquals(Assert.java:250)
	at org.testng.Assert.assertEquals(Assert.java:238)
	at TestClass.TestDemo1.test03(TestDemo1.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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)
===============================================
Default Suite
Total tests run: 3, Failures: 1, Skips: 0
===============================================
Process finished with exit code 
7.给测试方法分组

(alwaysRun = true )不管是什么分组执行它都执行

(groups = {“cars”,“SUV”})添加分组属性,可以有几个属性

package TestBaseMethod;

import BaseMethod.Basemethod;
import ClassTbc.ClassToBeTest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/19
 */
public class TestNG_Grouping {
    @BeforeClass(alwaysRun = true )
    public void BeforeClass(){
        System.out.println("TestNG_Grouping-BeforeClass-方法执行之前");
    }
    @AfterClass(alwaysRun = true )
    public void AfterClass(){
        System.out.println("TestNG_Grouping-AfterClass-方法执行之后");
    }

    @Test(groups = {"cars"})
    public void testPasst() {
        System.out.println("TestNG_Grouping---testPasst");
    }
    @Test(groups = {"cars"})
    public void testyema() {
        System.out.println("TestNG_Grouping---testyema");
    }
    @Test(groups = {"SUV"})
    public void testDaG() {
        System.out.println("TestNG_Grouping---testDaG");
    }
    @Test(groups = {"SUV"})
    public void testBadao() {
        System.out.println("TestNG_Grouping---testBadao");
    }

}
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression TestSuite">
    <test name ="GroupsDemo">
        <groups>
            <run>
                <include name ="SUV"></include>
            </run>
        </groups>
        <classes>
            <class name="TestBaseMethod.TestNG_Grouping"></class>
        </classes>
    </test>
</suite>

执行结果:

TestNG_Grouping-BeforeClass-方法执行之前
TestNG_Grouping---testBadao
TestNG_Grouping---testDaG
TestNG_Grouping-AfterClass-方法执行之后
8.给测试方法分组2
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression TestSuite">
    <test name ="GroupsDemo">
        <groups>
            <define name="all">
                <include name="SUV"></include>
                <include name="cars"></include>
                <include name="bikes"></include>
            </define>
            <define name="No-bike">
                <include name="SUV"></include>
                <include name="cars"></include>
            </define>
            <run>
                <include name ="No-bike"></include>
            </run>
        </groups>
        <classes>
            <class name="TestBaseMethod.TestNG_Grouping"></class>
        </classes>
    </test>
</suite>

运行结果:

TestNG_Grouping-BeforeClass-方法执行之前
TestNG_Grouping---testBadao
TestNG_Grouping---testDaG
TestNG_Grouping---testPasst
TestNG_Grouping-AfterClass-方法执行之后
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值