dependsOnGroup不使用beforeclass

时间:2015-06-25 05:32:11

标签: testng

dependsonGroup是否与beforeClass一起使用?

我的代码就像:

@BeforeClass(dependsOnGroups = { "init" })
public void setup() throws Exception
{
//some code
}

@Test(dependsOnGroups = { "init" })
public void test() throws Exception
{
//some code
}

init group有一些来自不同类的测试方法。

在我的情况下,如果init失败,则会跳过标有dependsOnGroups = {“init”}的Test方法,但不会跳过beforeClass方法。

还有其他方法可以获得理想的结果吗?

提前致谢

3 个答案:

答案 0 :(得分:1)

想出beforeclass(或afterclass)不接受dependsonGroup参数......我们可以使用beforemethod(如果适用的话)。

我通过将init方法中的标志设置为false来解决我的问题,如果它们失败并在beforeclass中使用它:

@BeforeClass
public void setup() throws Exception
{
 if(flag==true)
 {
 //some code
 }
}

答案 1 :(得分:0)

这是预期的行为。

我认为你能做的是

@beforeClass(dependsOnGroups= {"init"})
public setUp(){
}

对于beforeClass也是如此。哪个应该解决你的问题

您可以做的另一件事是使用afterClass并执行runAlways = true。在那个aftetClass中你可以撤消你在beforeClass中所做的任何事情,即使实际的测试都会跳过。 (这里我假设你的问题是你在beforeClass中做了一些事情并且他们没有在实际的测试方法中使用)

答案 2 :(得分:0)

This seems to be expected behavior..

@BeforeClass annotation:- Note- methods tagged with @Before/After will not be part of the final report. setup() is not a '@Test' and hence, its not skipped even though dependent group method init fails.

@Test annotation:- If a method depended upon(method which is part of a group, here 'init') fails, and you have a hard dependency on it (alwaysRun=false, which is by default), the methods that depend on it(here test()) are not marked as FAIL but as SKIP. Skipped @Test methods will be reported as such in the testNG report; because it is important since skipped methods may not be failures.

if you want @Test method to run even though dependent group method fails, then use a soft dependency. It is obtained by adding "alwaysRun=true" in your @Test annotation.

syntax:- @Test(dependsOnGroups = { "init" }, alwaysRun=true)

  • Here test() will always run after the groups you depend on, even if some of them have failed.