简体   繁体   中英

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 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()

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM