why does TestNG execute @AfterMethod like @Test if my first test is passed?
For example, testoutput if my first test fails:
1 test passed, 1 test failed.(3599,0 s)
TestSuite FAILED
run FAILED: check5=3 Expected: <3> got: <5>
run passed (1.0s)
But if I just switch the order of the test cases, so that the first passes, I get the following:
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)
what's happening? my 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> ...
I use Maven, TestNG and Java through NetBeans
my structure:
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
}
}
The problem with Annother is that the test output is out of order, the timestamps are not older than → newer, there are newer between the old ones: Here is my conclusion:
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
in addition, there are only 2 methods TestStep_003.run and TestStep_012.run, and yet it shows AfterMethod 4x