以编程方式退出 testNG 测试 [英] quitting testNG tests programmatically

查看:34
本文介绍了以编程方式退出 testNG 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果满足特定条件,我如何以编程方式退出整个测试套件?

How can I quit the complete test suite programmatically if a certain condition is met?

我正在检查 @AfterMethod 中的条件并使用 fail() 方法使 AfterMethod 注释失败.但是 AfterMethod 注释的执行次数与我的测试套件中的测试次数一样多.但是会跳过这些测试.

I am checking for the condition in @AfterMethod and using the fail() method to fail the AfterMethod annotation. But the AfterMethod annotation gets executed as many times as the number of tests in my Test suite. The tests are skipped however.

有没有办法做到这一点?

Is there a way to do this?

谢谢,

约翰

推荐答案

@AfterMethod 注释在测试中的每个测试方法之后触发.但是如果你想在每个测试方法之后验证条件,这是正确的注释.

The @AfterMethod annotation triggers after every test method in the test. But if you want to validate conditions after each test method this is the right annotation.

但是如果你想在所有方法都被执行后使用@AfterClass

But if you want to do it after all methods being executed use @AfterClass

只放置 System.exit() 不是一个好习惯.一旦退出测试,它就不会产生任何测试拆卸操作和报告生成.干净的方法是一旦发现这种情况就跳过测试.

Just putting System.exit() is a not good practice. Once you exit test it will not produce any test teardown actions and report generation. Clean way to do this is skipping the test once you find such a scenario.

你可以使用 testng SkipException .请参阅下面的代码示例

You can use testng SkipException for that . Please see the code below for sample

  @AfterMethod
      public void afterMethod() throws Throwable {
          if(CONDITION_MET==true)
          {
             throw new SkipException ("Skipping Test: ");
          } 
      }

谢谢,佛法

这篇关于以编程方式退出 testNG 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆