如果我的第一个测试通过,为什么TestNG会将@AfterMethod作为@Test执行?

时间:2012-01-29 12:55:42

标签: java maven selenium testng

如果我的第一个测试通过,为什么TestNG会将@AfterMethod作为@Test执行?

例如,如果我的第一次测试失败,testoutput是:

1 test passed, 1 test failed.(3599,0 s)
    TestSuite FAILED
        run FAILED: check5=3 Expected: <3> got: <5>
        run passed (1.0s)

但是,如果我只是切换测试用例的顺序,那么第一个将通过,我得到这个:

3 test passed, 2 test failed.(2867,0 s)
    TestSuite FAILED
        run passed (1.0s)
        run FAILED: check5=3 Expected: <3> got: <5>
        AfterMethod FAILED (4,0 s) // <--- wtf, this is not an @Test
        AfterTest passed (5,0 s)
        AfterSuite passed (15,0 s)

发生了什么事?我的testngsuite.xml:

<suite name="TestSuite_03">
    <test name="TestCase_17">
        <groups>
            <run><include name="functest"/></run>
        </groups>
        <classes>
            <class name="TestStep_003" desc="will pass" />
            <class name="TestStep_012" desc="will fail" />
        </classes> ...

我通过NetBeans使用Maven,TestNG和Java

我的结构:

public abstract class TestCommon
{
    @BeforeSuite(groups={"functest"})
    public void BeforeSuite()
    {
        // clean report folder
    }
    @BeforeTest(groups={"functest"})
    public void BeforeTest()
    {
        // start selenium browser
    }
    @AfterMethod(groups={"functest"}) // this is not a @test, still gets shown as failed
    public void AfterMethod()
    {
        // check for failure and capture screenshot
    }
    @AfterTest(groups={"functest})
    public void AfterTest()
    {
        // close browser
    }
}


public class TestStep_003 extends TestCommon
{
    @Test(groups = {"functest"})
    public void run()
    {
        assertThat(5, Matchers.equalTo(5)); // will pass
    }
}
public class TestStep_012 extends TestCommon
{
    @Test(groups = {"functest"})
    public void run()
    {
        assertThat(5, Matchers.equalTo(3)); // will fail
    }
}

另一个问题是testoutput没有按顺序排列,时间戳是 不老 - &gt;更新,旧的之间有更新的: 多数民众赞成我的输出如何:

1327840359762:  TestStep_012.AfterMethod // this is not the oldest timestamp!
1327840359763:  TestStep_003.run 
1327840359765:  TestStep_003.AfterMethod
1327840357189:  TestStep_012.BeforeSuite
1327840357192:  TestStep_012.BeforeTest
1327840359758:  TestStep_012.run 
1327840359762:  TestStep_012.AfterMethod
1327840359763:  TestStep_003.run 
1327840359765:  TestStep_003.AfterMethod 

此外,只有2种方法TestStep_003.run和TestStep_012.run, 并且它仍然显示AfterMethod 4x

0 个答案:

没有答案