zoukankan      html  css  js  c++  java
  • 三、TestNG的基本注解(1)

    Before类别和After类别注解

    举例说明

    创建两个TestNGAnnotationTest.java和TestNGAnnotationTest2.java的类

    TestNGAnnotationTest.java

    package com.lc.testngTest;
    
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.AfterSuite;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.BeforeSuite;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    
    public class TestNGAnnotationTest {
    
     
    
        @BeforeSuite
        public void beforeSuite() {
    
            System.out.println(this.getClass().getName() + " beforeSuite");
    
        }
    
     
    
        @AfterSuite
        public void afterSuite() {
    
            System.out.println(this.getClass().getName() + " afterSuite");
    
        }
    
     
    
        @BeforeTest
        public void beforeTest() {
    
            System.out.println(this.getClass().getName() + " beforeTest");
    
        }
    
     
    
        @AfterTest
        public void afterTest() {
    
            System.out.println(this.getClass().getName() + " afterTest");
    
        }
    
     
    
        @BeforeClass
        public void beforeClass() {
    
            System.out.println(this.getClass().getName() + " beforeClass");
    
        }
    
     
    
        @AfterClass
        public void afterClass() {
    
            System.out.println(this.getClass().getName() + " afterClass");
    
        }
    
     
    
        @BeforeMethod
        public void beofreMethod() {
    
            System.out.println(this.getClass().getName() + " beforeMethod");
    
        }
    
     
    
        @AfterMethod
        public void afterMethod() {
    
            System.out.println(this.getClass().getName() + " afterMethod");
    
        }
    
     
    
        @Test
        public void test1() {
    
            System.out.println(this.getClass().getName() + " test1");
    
        }
    
     
    
        @Test
        public void test2() {
    
            System.out.println(this.getClass().getName() + " test2");
    
        }
    
     
    
    }

    TestNGAnnotationTest2.java

    package com.lc.testngTest;
    
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.AfterSuite;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.BeforeSuite;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    
    public class TestNGAnnotationTest2 {
    
     
    
        @BeforeSuite
        public void beforeSuite() {
    
            System.out.println(this.getClass().getName() + " beforeSuite");
    
        }
    
        @AfterSuite
        public void afterSuite() {
    
            System.out.println(this.getClass().getName() + " afterSuite");
    
        }
    
        @BeforeTest
        public void beforeTest() {
    
            System.out.println(this.getClass().getName() + " beforeTest");
    
        }
    
        @AfterTest
        public void afterTest() {
    
            System.out.println(this.getClass().getName() + " afterTest");
    
        }
    
        @BeforeClass
        public void beforeClass() {
    
            System.out.println(this.getClass().getName() + " beforeClass");
    
        }
    
        @AfterClass
        public void afterClass() {
    
            System.out.println(this.getClass().getName() + " afterClass");
    
        }
    
        @BeforeMethod
        public void beofreMethod() {
    
            System.out.println(this.getClass().getName() + " beforeMethod");
    
        }
    
        @AfterMethod
        public void afterMethod() {
    
            System.out.println(this.getClass().getName() + " afterMethod");
    
        }
    
        @Test
        public void test1() {
    
            System.out.println(this.getClass().getName() + " test1");
    
        }
    
        @Test
        public void test2() {
    
            System.out.println(this.getClass().getName() + " test2");
    
        }
    
    }

    创建运行项目的xml文件

    testng2.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="suite1" verbose="1" >
        <test name="test1">
            <classes>
                <class name="com.lc.testngTest.TestNGAnnotationTest"></class>
                <class name="com.lc.testngTest.TestNGAnnotationTest2"></class>            
            </classes>
        </test>
        
    </suite>

    运行结果

    [TestNG] Running:
      D:workspacewebTestNGWebContent	estng2.xml
    
    com.lc.testngTest.TestNGAnnotationTest beforeSuite
    com.lc.testngTest.TestNGAnnotationTest2 beforeSuite
    com.lc.testngTest.TestNGAnnotationTest beforeTest
    com.lc.testngTest.TestNGAnnotationTest2 beforeTest
    com.lc.testngTest.TestNGAnnotationTest beforeClass
    com.lc.testngTest.TestNGAnnotationTest beforeMethod
    com.lc.testngTest.TestNGAnnotationTest test1
    com.lc.testngTest.TestNGAnnotationTest afterMethod
    com.lc.testngTest.TestNGAnnotationTest beforeMethod
    com.lc.testngTest.TestNGAnnotationTest test2
    com.lc.testngTest.TestNGAnnotationTest afterMethod
    com.lc.testngTest.TestNGAnnotationTest afterClass
    com.lc.testngTest.TestNGAnnotationTest2 beforeClass
    com.lc.testngTest.TestNGAnnotationTest2 beforeMethod
    com.lc.testngTest.TestNGAnnotationTest2 test1
    com.lc.testngTest.TestNGAnnotationTest2 afterMethod
    com.lc.testngTest.TestNGAnnotationTest2 beforeMethod
    com.lc.testngTest.TestNGAnnotationTest2 test2
    com.lc.testngTest.TestNGAnnotationTest2 afterMethod
    com.lc.testngTest.TestNGAnnotationTest2 afterClass
    com.lc.testngTest.TestNGAnnotationTest afterTest
    com.lc.testngTest.TestNGAnnotationTest2 afterTest
    com.lc.testngTest.TestNGAnnotationTest afterSuite
    com.lc.testngTest.TestNGAnnotationTest2 afterSuite
    
    ===============================================
    suite1
    Total tests run: 4, Failures: 0, Skips: 0
    ===============================================

     其中beforeSuite,afterSuite和beforeTest,afterTest区分不明显,下面修改xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="suite1" verbose="1" >
        <test name="test1">
            <classes>
                <class name="com
    [TestNG] Running:
      D:workspacewebTestNGWebContent	estng.xml
    
    com.lc.testngTest.TestNGAnnotationTest beforeSuite
    com.lc.testngTest.TestNGAnnotationTest2 beforeSuite
    com.lc.testngTest.TestNGAnnotationTest beforeTest
    com.lc.testngTest.TestNGAnnotationTest beforeClass
    com.lc.testngTest.TestNGAnnotationTest beforeMethod
    com.lc.testngTest.TestNGAnnotationTest test1
    com.lc.testngTest.TestNGAnnotationTest afterMethod
    com.lc.testngTest.TestNGAnnotationTest beforeMethod
    com.lc.testngTest.TestNGAnnotationTest test2
    com.lc.testngTest.TestNGAnnotationTest afterMethod
    com.lc.testngTest.TestNGAnnotationTest afterClass
    com.lc.testngTest.TestNGAnnotationTest afterTest
    com.lc.testngTest.TestNGAnnotationTest2 beforeTest
    com.lc.testngTest.TestNGAnnotationTest2 beforeClass
    com.lc.testngTest.TestNGAnnotationTest2 beforeMethod
    com.lc.testngTest.TestNGAnnotationTest2 test1
    com.lc.testngTest.TestNGAnnotationTest2 afterMethod
    com.lc.testngTest.TestNGAnnotationTest2 beforeMethod
    com.lc.testngTest.TestNGAnnotationTest2 test2
    com.lc.testngTest.TestNGAnnotationTest2 afterMethod
    com.lc.testngTest.TestNGAnnotationTest2 afterClass
    com.lc.testngTest.TestNGAnnotationTest2 afterTest
    com.lc.testngTest.TestNGAnnotationTest afterSuite
    com.lc.testngTest.TestNGAnnotationTest2 afterSuite
    
    ===============================================
    suite1
    Total tests run: 4, Failures: 0, Skips: 0
    ===============================================
    
    
    
    .lc.testngTest.TestNGAnnotationTest"></class>
            </classes>
        </test>
        
        <test name="test2">
            <classes>
                <class name="com.lc.testngTest.TestNGAnnotationTest2"></class>            
            </classes>
        </test>
    </suite>

    运行结果

  • 相关阅读:
    基于域名的虚拟主机
    用户认证
    部署lnmp
    django开发流程
    sed 和awk的执行方式
    将文本行倒序排列
    《深入理解JAVA虚拟机》----------第二章 JAVA内存区域与内存溢出异常,笔记(上)
    《深入理解JAVA虚拟机》----------第三章 垃圾收集器与内存分配策略,笔记(下)
    《深入理解JAVA虚拟机》----------第三章 垃圾收集器与内存分配策略,笔记(上)
    洛谷P3783 [SDOI2017]天才黑客(前后缀优化建图+虚树+最短路)
  • 原文地址:https://www.cnblogs.com/fanfancs/p/13917429.html
Copyright © 2011-2022 走看看