亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

 

Java自動化測試框架(TestNG)——分組測試

 

我們在測試時,可能會遇到以下幾種測試分組的場景:

  • 一個測試類當中有多個測試方法,只想執行其中的幾個相關的測試方法。
  • 當幾個相關的方法依賴相同的 setUp、tearDown操作。
  • 當某個方法依賴幾個相關的方法時,如幾個相關的方法執行通過后,才執行該方法。

我們可以通過設置 測試方法分組 的方式來解決上述問題。

分組測試是TestNG中的一個新的創新功能,它在JUnit框架中是不存在的。 在文中,我們將演示如何在TestNG中進行分組測試。

場景一:一個測試類當中有多個測試方法,只想執行其中的幾個相關的測試方法。

代碼示例如下,我們將TestDemo測試類的四個方法,通過@Test(groups = "") 方式,分成了兩個組,分別為apiTest、databaseTest。

package framework.parse;

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

public class TestDemo {

        @Test(groups = "apiTest")
        public void runApi1() {
            System.out.println("runApi1()");
        }

        @Test(groups = "apiTest")
        public void runApi2() {
            System.out.println("runApi2()1");
        }

        @Test(groups = "databaseTest")
        public void testOracle() {
            System.out.println("testOracle()");
        }

        @Test(groups = "databaseTest")
        public void testMySQL() {
            System.out.println("testMySQL");
        }
        
}

通過testng.xml文件進行運行配置管理,如下,我們僅運行framework.parse.TestDemo測試類中的databaseTest分組。

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

    <test name="testDemo">

        <groups>
            <run>
                <include name="databaseTest" />
            </run>
        </groups>

        <classes>
            <class name="framework.parse.TestDemo" />
        </classes>

    </test>

</suite>

運行結果如下:

Run testMySql
Run testOracle
===============================================
TestSuite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

場景二:當幾個相關的方法依賴對應的 setUp、tearDown操作。

我們可以通過@BeforeGroups、@AfterGroups方法實現用例組的setUp、tearDown操作,代碼示例如下:

package framework.parse;

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

public class TestDemo {

        @BeforeGroups("databaseTest")
        public void setUpDB() {
            System.out.println("Run init database");
        }

        @AfterGroups("databaseTest")
        public void tearDownDB() {
            System.out.println("Run clean database");
        }

        @Test(groups = "databaseTest")
        public void testOracle() {
            System.out.println("Run testOracle");
        }

        @Test(groups = "databaseTest")
        public void testMySql() {
            System.out.println("Run testMySql");
        }

}

運行結果如下,在執行databaseTest分組中的用例之前,執行了setUpDB 方法,在databaseTest分組分組中的用例執行完成后,執行了tearDownDB方法:

Run init database
Run testMySql
Run testOracle
Run clean database
===============================================
Default Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

場景三:當某個方法依賴幾個相關的方法時

我們可以通過 @Test(dependsOnGroups = { "" }) 方法實現優先執行依賴測試組,當依賴測試組執行通過后,則執行被依賴方法,否則跳過被依賴方法。代碼示例如下:

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

public class TestDemo {
        @Test(groups = "apiTest")
        public void runApi1() {
            System.out.println("Run runApi1");
        }

        @Test(groups = "apiTest")
        public void runApi2() {
            System.out.println("Run runApi2");
        }

        @Test(groups = "databaseTest")
        public void testOracle() {
            System.out.println("Run testOracle");
        }

        @Test(groups = "databaseTest")
        public void testMySql() {
            System.out.println("Run testMySql");
        }

        @Test(dependsOnGroups = { "databaseTest", "apiTest" })
        public void runFinal() {
            System.out.println("runFinal");
        }


}

上述代碼示例中,runFinal 方法依賴databaseTest組、apiTest組方法,需要在這兩個組的方法執行通過后,才可以執行runFinal方法,我們通過 @Test(dependsOnGroups = { "databaseTest", "apiTest" }) 注解的方式來實現,運行代碼,執行結果如下:

Run runApi1
Run runApi2
Run testMySql
Run testOracle
runFinal
===============================================
Default Suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================

如果當依賴的測試組里在運行過程中存在失敗的用例,則runFinal方法將被跳過,示例代碼如下:

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

public class TestDemo {
        @Test(groups = "apiTest")
        public void runApi1() {
            System.out.println("Run runApi1");
          	// 運行失敗
            Assert.assertEquals(1,2);
        }

        @Test(groups = "apiTest")
        public void runApi2() {
            System.out.println("Run runApi2");
        }

        @Test(groups = "databaseTest")
        public void testOracle() {
            System.out.println("Run testOracle");
        }

        @Test(groups = "databaseTest")
        public void testMySql() {
            System.out.println("Run testMySql");
        }

        @Test(dependsOnGroups = { "databaseTest", "apiTest" })
        public void runFinal() {
            System.out.println("runFinal");
        }


}

如上,示例代碼中apiTest分組中的runApi1測試用例將斷言失敗,此時運行測試用例,執行結果如下,我們會看到runFinal方法被沒有被執行,而是跳過。

Run runApi1

JAVA.lang.AssertionError: expected [2] but found [1]
Expected :2
Actual   :1
	at org.testng.Assert.fail(Assert.java:94)
	at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

Run runApi2
Run testMySql
Run testOracle
Test ignored.

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

分享到:
標簽:Java
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定