2016-02-19 31 views
0

我一直在玩TestNG並遇到一些行爲,我一直無法理解,因爲@BeforeGroups(inheritGroups = true)似乎不起作用。其中@BeforeSuite(inheritGroups = true),@BeforeTest(inheritGroups = true),@BeforeClass(inheritGroups = true)@BeforeMethod(inheritGroups = true)按照文檔中的描述工作。TestNG @BeforeGroups(inheritGroups = true)用法

在下面的代碼片段中,我明確行使了各種@Before*(inheritGroups = true)註釋參數。 @BeforeGroups是測試運行時未被調用的唯一註釋。此外,每個註釋源代碼都具有inheritGroups = true作爲默認值。即使未在註釋中明確設置它,@BeforeGroups也應該繼承默認設置的類級別組。

代碼

@Test(groups = "acceptance") 
public class InheritTest { 
    @BeforeSuite(inheritGroups = true) 
    public void beforeSuite() { 
     System.out.println("I am @BeforeSuite"); 
    } 

    @BeforeTest(inheritGroups = true) 
    public void beforeTest() { 
     System.out.println("I am @BeforeTest"); 
    } 

    @BeforeGroups 
    public void beforeGroupsNoGroup() { 
     System.out.println("I am @BeforeGroups"); 
    } 

    @BeforeGroups(inheritGroups = true) 
    public void beforeGroupsInherit() { 
     System.out.println("I am @BeforeGroups(inheritGroups = true)"); 
    } 

    @BeforeGroups(groups = { "acceptance" }) 
    public void beforeGroupsGroups() { 
     System.out.println("I am @BeforeGroups(groups = {\"acceptance\"}"); 
    } 

    @BeforeClass(inheritGroups = true) 
    public void beforeClass() { 
     System.out.println("I am @BeforeClass"); 
    } 

    @BeforeMethod(inheritGroups = true) 
    public void beforeMethod() { 
     System.out.println("I am @BeforeMethod"); 
    } 

    @Test 
    public void test() { 
     System.out.println("I am @Test"); 
    } 
} 

的testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="Test Suite" verbose="1"> 
    <test name="Run Inherit Test"> 
     <groups> 
      <run> 
       <include name="acceptance" /> 
      </run> 
     </groups> 
     <classes> 
      <class name="InheritTest" /> 
     </classes> 
    </test> 
</suite> 

輸出

I am @BeforeSuite 
I am @BeforeTest 
I am @BeforeClass 
I am @BeforeGroups(groups = {"acceptance"} 
I am @BeforeMethod 
I am @Test 

回答