2017-09-25 75 views
2

下面是我的XML文件和Demo類。在demo1()方法和postCondition()方法將在demo1()方法之後運行之前,Precondition()方法將運行。 demo2()的過程相同。但是當我運行代碼時,BeforeSuite和BeforeTest方法不會被調用。爲什麼。?如何給他們打電話?@Before Suite和@BeforeTest方法在TestNG中執行時不會被調用

Output :   
Before Method is executing              
DEMO -1 
After Method is executing 
Before Method is executing 
DEMO 2 
After Method is executing 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="Suite"> 
    <test name="Test"> 
     <groups> 
      <run> 
       <include name = "Hey"></include> 
      </run> 
     </groups> 
     <classes> 
      <class name="practicepackage.Demo"/> 
     </classes> 
    </test> <!-- Test --> 
</suite> <!-- Suite --> 
package practicepackage; 

import org.testng.annotations.AfterMethod; 
import org.testng.annotations.BeforeMethod; 
import org.testng.annotations.BeforeSuite; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 

public class Demo { 

    @BeforeSuite 
    public void beforeSuite(){ 
     System.out.println("Before Suite method is being launched"); 
    } 

    @BeforeTest 
    public void beforeTest(){ 
     System.out.println("Before Test Method is being luanched"); 
    } 

    @BeforeMethod(groups = {"Hey"}) 
    public void PreCondition(){ 
     System.out.println("Before Method is executing"); 
    } 

    @Test (groups = {"Hey"}) 
    public void demo1(){ 
     System.out.println("DEMO -1 "); 
    } 

    @Test(groups = {"Hey"}) 
    public void demo2(){ 
     System.out.println("DEMO 2"); 
    } 

    @AfterMethod(groups = {"Hey"}) 
    public void postCondition(){ 
     System.out.println("After Method is executing"); 
    } 
} 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="Suite"> 
    <test name="Test"> 
     <groups> 
      <run> 
       <include name = "Hey"></include> 
      </run> 
     </groups> 
     <classes> 
      <class name="practicepackage.Demo"/> 
     </classes> 
    </test> <!-- Test --> 
</suite> <!-- Suite --> 

回答

2

爲了確保@BeforeSuite@BeforeTest執行所有的時間,請啓用這些註解屬性alwaysRun=true

這是必需的,因爲當您運行組時,這些配置方法將不會被TestNG選中,除非它們是您選擇的組的一部分。

TestNG中的組選擇可以被視爲一種TestNG中的過濾機制,它可以讓TestNG在決定運行哪些測試時告訴過濾標準。

+0

謝謝你的幫助。 @Krishnan。這幫了我很多 – naqash

+0

如果能幫助回答你的問題,請接受我的回答。 –

+0

謝謝,找這個 –

相關問題