有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在java中使用testng注释

我的代码

public class Test{

  @BeforeTest
  public void create_user throws Exception (){

  }

  @Test
    public void user_actions throws Exception (){

  }

  @AfterTest
  public void delete_user throws Exception(){

  }

}

上面是我的测试课。如果我在create_user()中得到任何错误,当前它抛出Exception并耗尽测试用例

但是我需要delete_user()应该被执行,而不管create_user()user_actions()中的任何错误


共 (3) 个答案

  1. # 1 楼答案

    如果测试异常是预期的,您可以使用@expected通知测试异常,这将确保调用delete_user。 如果是临时异常,则最终阻止删除用户是一种良好的做法

  2. # 2 楼答案

    试试@AfterTest(alwaysRun = true)

    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.

  3. # 3 楼答案

    如果测试方法预计会出现异常,请使用以下注释:

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

    如果测试方法引发意外异常,我建议您检查测试设置/方法