zoukankan      html  css  js  c++  java
  • testNG 注释实例

    1. 单个测试用例文件

    新建TestDBConnection.java文件

    import org.testng.annotations.*;
    
    public class TestDBConnection {
    
        @Test
        public void runOtherTest1() {
            System.out.println("@Test - runOtherTest1");
        }
    
        @Test
        public void runOtherTest2() {
            System.out.println("@Test - runOtherTest2");
        }
    
        @Test(groups = "haha")
        public void runOtherTest3() {
            System.out.println("@Test - runOtherTest3");
        }
    
        @BeforeClass
        public void beforeClass() {
            System.out.println("@Test - BeforeClass");
        }
        @BeforeGroups(groups = "haha")
        public void beforeGroups() {
            System.out.println("@Test - BeforeGroups");
        }
        @BeforeMethod
        public void beforeMethod() {
            System.out.println("@Test - BeforeMethod");
        }
        @AfterMethod
        public void afterMethod() {
            System.out.println("@Test - AfterMethod");
        }
        @AfterGroups(groups = "haha")
        public void afterGroups() {
            System.out.println("@Test - AfterGroups");
        }
    
        @AfterClass
        public void afterClass() {
            System.out.println("@Test - AfterClass");
        }
    }

    配置并运行

    点击运行,结果如下:

    [TestNG] Running:
      /Users/sunmin/Library/Caches/IdeaIC2017.3/temp-testng-customsuite.xml
    @Test - BeforeClass
    @Test - BeforeMethod
    @Test - runOtherTest1
    @Test - AfterMethod
    @Test - BeforeMethod
    @Test - runOtherTest2
    @Test - AfterMethod
    @Test - BeforeGroups
    @Test - BeforeMethod
    @Test - runOtherTest3
    @Test - AfterMethod
    @Test - AfterGroups
    @Test - AfterClass
    
    ===============================================
    Default Suite
    Total tests run: 3, Failures: 0, Skips: 0
    ===============================================
    
    
    Process finished with exit code 0

     2. Test Suit

    新建TestDBConfig.java文件,内容如下:

    import org.testng.annotations.AfterSuite;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeSuite;
    import org.testng.annotations.BeforeTest;
    
    public class TestDBConfig {
    
        @BeforeSuite()
        public void beforeSuite() {
            System.out.println("@BeforeSuite");
        }
    
        @AfterSuite()
        public void afterSuite() {
            System.out.println("@AfterSuite");
        }
    
        @BeforeTest()
        public void beforeTest() {
            System.out.println("@BeforeTest");
        }
    
        @AfterTest()
        public void afterTest() {
            System.out.println("@AfterTest");
        }
    
    }

    新建testng.xml文件,位置放在pom.xml同级,内容如下。

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <!-- @BeforeSuite -->
    <suite name="TestAll">
    
        <!-- @BeforeTest -->
        <test name="case1">
            <classes>
                <class name="TestDBConfig" />
            </classes>
        </test>
        <!-- @AfterTest -->
    
        <!-- @BeforeTest -->
        <test name="case2">
            <classes>
                <class name="TestDBConnection" />
            </classes>
        </test>
        <!-- @AfterTest -->
    </suite> <!-- @AfterSuite -->

    配置及运行

    Test kind需选择Suite,Suite的配置文件选择我们自己的testng.xml文件。点击运行,结果如下:为啥@AfterTest会出现在这个地方?

    [TestNG] Running:
      /Users/sunmin/Downloads/TestHelloWorld/testng.xml
    @BeforeSuite
    @BeforeTest
    @AfterTest
    @Test - BeforeClass
    @Test - BeforeMethod
    @Test - runOtherTest1
    @Test - AfterMethod
    @Test - BeforeMethod
    @Test - runOtherTest2
    @Test - AfterMethod
    @Test - BeforeGroups
    @Test - BeforeMethod
    @Test - runOtherTest3
    @Test - AfterMethod
    @Test - AfterGroups
    @Test - AfterClass
    @AfterSuite
    
    ===============================================
    TestAll
    Total tests run: 3, Failures: 0, Skips: 0
    ===============================================
    
    
    Process finished with exit code 0
  • 相关阅读:
    微信公众平台消息接口开发(32)空气质量指数查询
    微信公众平台消息接口开发(33)智能快递查询
    微信公众平台消息接口开发(34)桃花运测试
    微信5.0体验贴:即将横“扫”一切
    微信官方首次表态:微信公众平台不是营销工具
    微信公众平台消息接口开发(34)微信墙
    微信 5.0 内部测试版下载地址
    PHP和MySQL Web开发 经典书籍
    字符数组在C++、C#等语言中的操作
    CF555B 贪心
  • 原文地址:https://www.cnblogs.com/sunada2005/p/10857948.html
Copyright © 2011-2022 走看看