TestNG中BeforeTest和BeforeMethod的區別


一個 TestNG 類可以包含各種 TestNG 方法。例如:@BeforeTest @AfterTest @BeforeSuite @BeforeClass @BeforeMethod @test 等。

根據執行順序,@BeforeTest 首先執行,然後是 @BeforeMethod。

但是,如果一個類中有多個 TestNG 測試,這些方法的行為就會很明顯,因為 @BeforeTest 只在第一個 @Test 方法執行之前執行一次,而 @BeforeMethod 在每個 @Test 執行之前都會執行。

  • @BeforeTest:此方法在呼叫第一個 @Test 方法之前,在整個執行過程中只執行一次。無論存在多少個 @Test 標籤,或者有多少個類包含 @Test 標籤,或者多個類有多個測試標籤,都無關緊要。根據 testing.xml 檔案,一旦執行開始,@BeforeTest 方法只在第一個 @Test 方法以及 @BeforeClass 和 @BeforeMethod(如果存在)的執行之前執行一次。

  • @BeforeMethod:此方法每個 @Test 執行一次,即 @BeforeMethod 的執行次數 = 要執行的 @Test 方法的數量。根據 testing.xml,如果需要執行 10 個 @Test,則 @BeforeMethod 會在每個 @Test 開始執行之前執行。

這些差異的關鍵點是

  • @BeforeTest 方法只在第一個 @Test 方法之前執行一次。

  • @BeforeMethod 在每個 @Test 方法之前執行。

  • 如果在不同的類中存在單獨的 @BeforeTest 和 @BeforeMethod 方法,則所有 @BeforeTest 方法將首先執行,但 @BeforeMethod 方法將根據各自的類及其 @Test 方法執行。

  • 如果所有測試類都擴充套件了單獨類中存在的公共 @BeforeTest 和 @BeforeMethod 方法,則 @BeforeTest 將只執行一次,但是相同的 @BeforeMethod 方法將在每個 @Test 之前執行。

場景 1

當有兩個 TestNG 類,每個類都有兩個 @Test 標籤時。每個類都擴充套件了包含 @BeforeTest 和 @BeforeMethod 的公共類。在這種情況下,@BeforeTest 只執行一次,但是相同的 @BeforeMethod 將執行 2*2 次,每次在每個 @Test 之前。

解決此問題的方法/演算法

  • 步驟 1  建立一個公共的 TestNG 類 - beforemethods 並編寫 @BeforeTest 和 @BeforeMethod 方法。

  • 步驟 2 − 建立 2 個 TestNG 類,每個類都擴充套件公共類 (beforemethods) - OrderofTestExecutionInTestNG 和 NewTestngClass

  • 步驟 3  在每個類中編寫 2 個不同的 @Test 方法 - OrderofTestExecutionInTestNG 和 NewTestngClass。

  • 步驟 4  現在建立如下所示的 testNG.xml 以執行 2 個 TestNG 類,而不是公共類。

  • 步驟 5  現在,執行 testNG.xml 或在 IDE 中直接執行 TestNG 類,或者使用命令列編譯並執行它。

示例

以下程式碼用於公共 TestNG 類 - beforemethods。

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;

public class beforemethods {
    @BeforeMethod
    public void beforeMethod() {
        System.out.println("in beforeMethod");
    }
    @BeforeTest
    public void beforeTest() {
        System.out.println("in beforeTest");
    }
} 

以下程式碼用於 TestNG 類 - OrderofTestExecutionInTestNG   

import org.testng.annotations.Test;

public class OrderofTestExecutionInTestNG extends beforemethods {
    // test case 1
    @Test
    public void testCase1() {
        System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
    }
    // test case 2
    @Test
    public void testCase2() {
        System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
    }
}  

以下程式碼用於公共 TestNG 類 - NewTestngClass   

import org.testng.annotations.Test;

public class NewTestngClass extends beforemethods{
    @Test
    public void testCase1() {
        System.out.println("in test case 1 of NewTestngClass");
    }
    @Test
    public void testCase2() {
        System.out.println("in test case 2 of NewTestngClass");
    } 
} 

testng.xml

這是一個配置檔案,用於組織和執行 TestNG 測試用例。

當需要執行有限的測試而不是完整的套件時,它非常方便。

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "OrderofTestExecutionInTestNG"/>
    <class name = "NewTestngClass"/>
      </classes>
   </test>
</suite>

輸出

in beforeTest
in beforeMethod
in test case 1 of OrderofTestExecutionInTestNG
in beforeMethod
in test case 2 of OrderofTestExecutionInTestNG
in beforeMethod
in test case 1 of NewTestngClass
in beforeMethod
in test case 2 of NewTestngClass

===============================================
Suite1
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

場景 2

當有兩個 TestNG 類,每個類都有兩個 @Test 標籤和單獨的 @BeforeTest 以及 @BeforeMethod 時。在這種情況下,所有 @BeforeTest 方法將首先執行,但 @BeforeMethod 將根據各自的類和 @Test 執行。

解決此問題的方法/演算法

  • 步驟 1    建立 2 個 TestNG 類 - OrderofTestExecutionInTestNG 和 NewTestngClass

  • 步驟 2  在每個類中編寫 2 個不同的 @Test 方法 - OrderofTestExecutionInTestNG 和 NewTestngClass。

  • 步驟 3  在每個類中編寫不同的 @BeforeTest 方法 - OrderofTestExecutionInTestNG 和 NewTestngClass。

  • 步驟 4  在每個類中編寫不同的 @BeforeMethod - OrderofTestExecutionInTestNG 和 NewTestngClass。

  • 步驟 5  現在建立如下所示的 testNG.xml 以執行 2 個 TestNG 類

  • 步驟 6  現在,執行 testNG.xml 或在 IDE 中直接執行 TestNG 類,或者使用命令列編譯並執行它。

示例

以下程式碼用於 TestNG 類 - OrderofTestExecutionInTestNG。

import org.testng.annotations.*;

public class OrderofTestExecutionInTestNG {
    // test case 1
    @Test
    public void testCase1() {
        System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
    }
    // test case 2
    @Test
    public void testCase2() {
        System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
    }
    @BeforeMethod
    public void beforeMethod() {
        System.out.println("in beforeMethod of OrderofTestExecutionInTestNG");
    }
    @BeforeTest
    public void beforeTest() {
        System.out.println("in beforeTest of OrderofTestExecutionInTestNG");
    }
}

以下程式碼用於公共 TestNG 類 - NewTestngClass。

import org.testng.annotations.*;

public class NewTestngClass {

    @Test
    public void testCase1() {
        System.out.println("in test case 1 of NewTestngClass");
    }
    @Test
    public void testCase2() {
        System.out.println("in test case 2 of NewTestngClass");
    }

    @BeforeTest
    public void beforeTest() {
        System.out.println("in beforeTest of NewTestngClass");
    }
    @BeforeMethod
    public void beforeMethod() {
        System.out.println("in beforeMethod of NewTestngClass");
 }
} 

testng.xml

這是一個配置檔案,用於組織和執行 TestNG 測試用例。

當需要執行有限的測試而不是完整的套件時,它非常方便。

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "OrderofTestExecutionInTestNG"/>
    <class name = "NewTestngClass"/>
      </classes>
   </test>
</suite>

輸出

in beforeTest of NewTestngClass
in beforeTest of OrderofTestExecutionInTestNG
in beforeMethod of OrderofTestExecutionInTestNG
in test case 1 of OrderofTestExecutionInTestNG
in beforeMethod of OrderofTestExecutionInTestNG
in test case 2 of OrderofTestExecutionInTestNG
in beforeMethod of NewTestngClass
in test case 1 of NewTestngClass
in beforeMethod of NewTestngClass
in test case 2 of NewTestngClass

===============================================
Suite1
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
=============================================== 

更新於: 2023年8月16日

3K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告