3

My code

public class Test{

  @BeforeTest
  public void create_user throws Exception (){

  }

  @Test
    public void user_actions throws Exception (){

  }

  @AfterTest
  public void delete_user throws Exception(){

  }

}

Above is my test class. If i get any error in create_user(), currently its throws Exception and ran out of the test case.

But i need delete_user() should be executed irrespective of any error in create_user() or user_actions()

Community
  • 1
  • 1
Galet
  • 5,853
  • 21
  • 82
  • 148

3 Answers3

4

Try @AfterTest(alwaysRun = true).

From the TestNG doc:

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.

peetya
  • 3,578
  • 2
  • 16
  • 30
0

If the test exception is expected you can use @expected to inform the test about the exception which would ensure calling delete_user. If it is an adhoc exception having finally block to delete_user is a good practice.

jags
  • 176
  • 5
0

If the test method should expect an exception, use the following annotation:

@Test(expectedExceptions=<your exceptions here>)
public void user_actions throws Exception (){
    // 
}

If the test method throws an unexpected exception, I would recommend to review your test setup/method.

swinkler
  • 1,703
  • 10
  • 20