2016-11-15 51 views
-1

如果我有一個帶有組的beforeMethod,並且我運行了一個不同的組,但在該組內,存在一個測試,其中包含我與beforeMethod一起運行以及組,我希望該測試能夠運行beforemethod。因此,例如:當方法*包含*組時,運行@BeforeMethod testNG批註

@BeforeMethod(groups = "a") 
public void setupForGroupA() { 
... 
} 

@Test(groups = {"supplemental", "a"}) 
public void test() { 
... 
} 

當我運行組TestNG的補充=,我仍然希望beforeMethod測試之前運行,但由於該組是補充,而不是「A」,它不會。

對我來說,這看起來像是一個明顯的特徵,我覺得我必須錯誤地使用這些組,所以我也想解釋我的工作流程,以防萬一這是我的問題所在。

我使用組來定義不同層次的測試,以及他們是否需要創建自己的帳戶,或者他們是否需要使用代理來訪問他們的數據等。我將有一組煙,補充和迴歸以及uniqueAccount,proxy等組。我不需要針對第一組的具體設置,但那些是我通過maven運行的組。我需要針對後者的特定設置,但我絕不希望僅運行需要代理的測試,或者需要一個唯一的帳戶。

回答

1

組運行時不計算組配置。 test方法將不會激活setupForGroupA方法。

該功能用於查找要運行的方法。 根據下面的例子:

@BeforeMethod(groups = "a") 
public void setupForGroupA() { 
... 
} 

@Test(groups = {"supplemental", "a"}) 
public void test() { 
... 
} 

@Test(groups = {"supplemental"}) 
public void test2() { 
... 
} 

如果你運行這個類組「A」,它將運行setupForGroupAtest方法,因爲它們都標有「A」組。

如果您使用組「補充」運行此類,它將運行testtest2方法,因爲它們標記爲「補充」組。

它看起來對於某些方法有不同的行爲,所以一個好的方法是將不同類中的方法分開,並按類選擇測試,而不是按組來選擇測試。

public class A { 
    @BeforeMethod 
    public void setupForGroupA() { 
    ... 
    } 

    @Test 
    public void test() { 
    ... 
    } 
} 

public class Supplemental { 
    @Test 
    public void test2() { 
    ... 
    } 
} 

運行類A只會setupForGroupAtest運行。 運行類補充將僅運行test2。運行這兩個類將運行一切。

如果你想運行別的東西兩個類和過濾器,你可以用a method interceptor實現自己的邏輯:

@MyCustomAnnotation(tags = "a", "supplemental") 
public class A { 
    ... 
} 

@MyCustomAnnotation(tags = "supplemental") 
public class Supplemental { 
    ... 
} 

public class MyInterceptor implements IMethodInterceptor { 

    public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { 
    // for each method, if its class contains the expected tag, then add it to the list 
    // expect tag can be passed by a system property or in a parameter from the suite file (available from ITestContext) 
    } 
} 
1

如果我知道它是正確的,那麼您希望每次都運行before方法。在這種情況下,你可能會喜歡這個 -

@BeforeMethod(alwaysRun = true, groups = "a") 
public void setupForGroupA() { 
... 
} 

你想要的解決方案,這其中設置alwaysRun =真爲你的前方法。

+0

所以擴大的例子有點我可以用組添加一個方法「測試2」補充,但沒有組「a」。在這種情況下,我希望before方法在'test'之前運行,但不要在'test2'之前運行。 – RankWeis

相關問題