Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alwaysRun parameter in TestNG

Can someone please explain me when @AfterMethod(alwaysRun = true) should execute. Will it execute when @test method is skipped. In documentation is written so, but I have observed different behavior.

Example: Code:

public class testing{

    @Test
    public void testCase2(){
        System.out.println("in test case 2");
        Assert.assertEquals(1,2);
    }

    @Test(dependsOnMethods = { "testCase2" })
    public void testcase3(){
        System.out.println("OK");
    }

    @AfterMethod(alwaysRun = true)
    public void afterMethod2() {
        System.out.println("in afterMethod");
    }
}

Output:

in test case 2
in afterMethod
FAILED: testCase2
java.lang.AssertionError: expected [2] but found [1]
   at org.testng.Assert.fail(Assert.java:94)
   at org.testng.Assert.failNotEquals(Assert.java:494)
   at org.testng.Assert.assertEquals(Assert.java:123)
   at org.testng.Assert.assertEquals(Assert.java:370)
   at org.testng.Assert.assertEquals(Assert.java:380)
   at app.testing.testCase2(testing.java:22)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:606)
   at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
   at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
   at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
   at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
   at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
   at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
   at org.testng.TestRunner.privateRun(TestRunner.java:767)
   at org.testng.TestRunner.run(TestRunner.java:617)
   at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
   at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
   at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
   at org.testng.SuiteRunner.run(SuiteRunner.java:240)
   at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
   at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
   at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
   at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
   at org.testng.TestNG.run(TestNG.java:1057)
   at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
   at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
   at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

SKIPPED: testcase3

===============================================
Default test
Tests run: 2, Failures: 1, Skips: 1
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 1, Skips: 1
===============================================

Shouldn't @After method be executed twice, also after skipped @Test? Thanks for answer in advance :)

like image 433
Konrad Kaliściak Avatar asked Oct 08 '14 08:10

Konrad Kaliściak


2 Answers

The documentation says

For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.

So I think it should execute after skipped test method.

like image 181
Konrad Kaliściak Avatar answered Oct 18 '22 07:10

Konrad Kaliściak


Even though the documentation has stated that

For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.

However, in configfailurepolicy annotations, the following is stated when there's any test failures

configfailurepolicy: Whether TestNG should continue to execute the remaining tests in the suite or skip them if an @before* method fails. Default behavior is skip.

BTW, @AfterMethod is not a test method but a configuration one.

like image 44
chu.r Avatar answered Oct 18 '22 05:10

chu.r