How to use testng annotations in java

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 an error in create_user()

, it is currently being thrown Exception

and finished with the test case.

But I need delete_user()

to execute regardless of any error in create_user()

oruser_actions()

+3


source to share


3 answers


Try it @AfterTest(alwaysRun = true)

.

From TestNG doc :



For the following methods (afterSuite, afterClass, ...): if set to true, this configuration method will run even if one or more of the previously called methods failed or were skipped.

+4


source


If a test exception is expected, you can use @expected to inform the test of the exception that the delete_user call would ensure. If it's an adhoc exception that finally blocks delete_user is good practice.



0


source


If the validator must 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 looking at your test setup / method.

0


source







All Articles