2017-09-25 59 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

谢谢,找这个 –

相关问题