java—在没有assert.assume(..)的情况下可以跳过/忽略测试用例

tvmytwxo  于 2021-07-04  发布在  Java
关注(0)|答案(2)|浏览(170)

我有cumber.feature文件,其中包含多个测试用例(使用“示例”)。
在某些情况下,我想跳过几个测试用例,只运行其中的几个。
注意:用户应该能够选择动态跳过哪个测试用例
例如,他可以在第一次运行时决定跳过第1个测试用例,下一次则决定跳过第2个测试用例。

Examples:
  | SERIAL_NO | ID                |
  | 1         | Create-Customer A |
  | 2         | Create-Customer B |
  | 3         | Create-Customer C |

我设法用

Assume.assumeTrue(...)

唯一的问题是-代码抛出异常,我想保持日志清晰。
是否有任何选项可以避免打印异常,而忽略测试用例?或者用另一种方法跳过它?
谢谢

blpfk2vs

blpfk2vs1#

最后,我找到了一个简单的解决方案,使用与我前面提到的assert.assume(…)相同的方法,只需清除异常堆栈跟踪,然后重新抛出它。
在下面的代码中,您可以看到实际的更改只是我添加了catch块:

try
{
    Assume.assumeTrue("Some Condition...");
}
catch (AssumptionViolatedException e)
{
    // clearing stack trace, so it will keep logs clear, just print the name of exception
    e.setStackTrace(new StackTraceElement[] {});
    throw e;
}

现在,异常堆栈跟踪没有打印到日志中,因此日志保持干净,我只看到以下内容:
org.junit.assumptionviolatedexception:获取:,应为:is
这对我来说已经足够了。

5cg8jx4n

5cg8jx4n2#

我会把你的例子分为每一个你想跳过测试和标记的场景 @todo ,就像这样:

Scenario Outline: [test-scenario-001] Send a new form with request type  
   Given I preload the from using "request"
   And I select the 'Submit' button
   Then the response message "hello" is returned
   Examples:
    | request |
    | POST    |
   @todo
   Examples:
    | request | 
    | GET     |
    | PUT     |
    | DELETE  |

然后运行第一个场景 Example 仅调用不作为功能一部分运行的标记:

-Dcucumber.options="--tags ~@todo"

全部运行 Example 场景中,不要使用标记

相关问题