TestNG 的 @BeforeMethodAnnotation Type,其作用是標明所注解的方法在每一個測試方法運行之前會執(zhí)行一次挫酿。例如
@BeforeMethod
void FuncBefore()
@Test
void TestCase1()
@Test
void TestCase2()
執(zhí)行的順序為
FuncBefore
TestCase1
FuncBefore
TestCase2
但是,在將TestCase放入某一個Group之后愕难,@BeforeMethod失效了:
@BeforeMethod
void FuncBefore()
@Test(groups = "GroupA")
void TestCase1()
@Test(groups = "GroupA")
void TestCase2()
當(dāng)我們執(zhí)行GroupA的測試腳本時早龟,執(zhí)行順序為
TestCase1
TestCase2
原因:@BeforeMethod或@BeforeTest未被包含進GroupA中,因此運行GroupA時無法執(zhí)行猫缭,看似“失效”了葱弟。當(dāng)未指定Group時,@Test同時被視作處于一個“全局group”猜丹,因此會執(zhí)行芝加。
解決方法:
1、將@BeforeMethod同樣加入GroupA
2射窒、設(shè)置@BeforeMethod的屬性alwaysRun=true:
@BeforeMethod(alwaysRun = true)
void FuncBefore()