【问题标题】:How to use testng annotations in java如何在java中使用testng注解
【发布时间】:2015-05-07 12:38:52
【问题描述】:

我的代码

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() 中的任何错误

【问题讨论】:

    标签: java annotations testng selendroid


    【解决方案1】:

    试试@AfterTest(alwaysRun = true)

    来自TestNG doc

    对于 after 方法 (afterSuite, afterClass, ...):如果设置为 true,即使之前调用的一个或多个方法失败或被跳过,此配置方法也会运行。

    【讨论】:

      【解决方案2】:

      如果测试异常是预期的,你可以使用@expected 通知测试异常,这将确保调用delete_user。 如果它是一个临时异常,最终阻止 delete_user 是一个好习惯。

      【讨论】:

        【解决方案3】:

        如果测试方法应该出现异常,请使用以下注释:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-09
          • 2018-11-06
          • 2014-07-21
          • 1970-01-01
          • 2017-12-07
          • 2011-07-26
          相关资源
          最近更新 更多