TestNG中BeforeClass和BeforeTest方法的區別


一個TestNG類可以包含各種TestNG方法,例如@BeforeTest,@AfterTest,@BeforeSuite,@BeforeClass,@BeforeMethod,@test等。根據執行順序,@BeforeTest首先執行,然後@BeforeClass執行。但是,如果有多個TestNG類並且每個類中有多個測試,則這些方法的行為就會很明顯。

@BeforeTest

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

@BeforeClass

此方法每個類執行一次,即@BeforeClass的執行次數等於要執行的TestNG類的數量。根據testing.xml,如果需要執行10個TestNG類,則@BeforeClass將在每個類開始執行之前每次執行。每個類中存在多少個@Test標籤並不重要。

根據最佳實踐,建議為所有通用的@Before方法建立一個單獨的類,以便可以根據需要執行這些方法,並避免重複呼叫。

這些差異中的關鍵點是

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

  • @BeforeClass在每個類之前執行。

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

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

場景1

假設有兩個TestNG類,每個類都有兩個@Test標籤。每個類都擴充套件了包含@BeforeTest@BeforeClass方法的公共類。在這種情況下,@BeforeTest將只執行一次,但是相同的@BeforeClass方法將執行兩次,每次在每個類之前。

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

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

  • 步驟2 - 建立兩個TestNG類,每個類都擴充套件公共類(beforemethods) - OrderofTestExecutionInTestNGNewTestngClass

  • 步驟3 - 在這兩個類中的每一箇中編寫兩個不同的@Test方法 - OrderofTestExecutionInTestNGNewTestngClass

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

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

示例

對公共TestNG類beforemethods使用以下程式碼 -

import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
public class beforemethods {
   @BeforeClass
   public void beforeClass() {
      System.out.println("in beforeClass");
   }
   @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 beforeClass
in test case 1 of OrderofTestExecutionInTestNG
in test case 2 of OrderofTestExecutionInTestNG
in beforeClass
in test case 1 of NewTestngClass
in test case 2 of NewTestngClassin afterSuite

場景2

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

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

  • 步驟1 - 建立兩個TestNG類,OrderofTestExecutionInTestNGNewTestngClass

  • 步驟2 - 在這兩個類中的每一箇中編寫兩個不同的@Test方法,OrderofTestExecutionInTestNGNewTestngClass

  • 步驟3 - 在這兩個類中的每一箇中編寫不同的@BeforeTest方法,OrderofTestExecutionInTestNGNewTestngClass

  • 步驟4 - 在這兩個類中的每一箇中編寫不同的@BeforeClass方法,OrderofTestExecutionInTestNG和NewTestngClass。

  • 步驟5 - 現在建立如下所示的testNG.xml來執行這兩個TestNG類。

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

示例

對TestNG類OrderofTestExecutionInTestNG使用以下程式碼 -

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
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");
   }
   @BeforeClass
   public void beforeClass() {
      System.out.println("in beforeClass of OrderofTestExecutionInTestNG");
   }
   @BeforeTest
   public void beforeTest() {
      System.out.println("in beforeTest of OrderofTestExecutionInTestNG");
   }
}

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

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
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 beforTest of NewTestngClass");
   }
   @BeforeClass
   public void beforeClass() {
      System.out.println("in beforeClass 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 beforTest of NewTestngClass
in beforeTest of OrderofTestExecutionInTestNG
in beforeClass of OrderofTestExecutionInTestNG
in test case 1 of OrderofTestExecutionInTestNG
in test case 2 of OrderofTestExecutionInTestNG
in beforeClass of NewTestngClass
in test case 1 of NewTestngClass
in test case 2 of NewTestngClassin

更新於: 2022年1月12日

5K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.