可以跳过/忽略Cucumber测试案例而不使用Assert.Assume(..)。

huangapple go评论71阅读模式
英文:

It is possible to skip / ignore Cucumber test case without Assert.Assume(..)

问题

我有一个带有多个测试用例(使用“Examples”)的Cucumber .feature文件。
在某些情况下,我想跳过一些测试用例,只运行其中的一些。

注意:
用户应该能够动态选择要跳过的测试用例。
例如,他可以在第一次运行时决定跳过测试用例1,下一次跳过测试用例2。

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

我设法使用以下方式来做到:

Assume.assumeTrue(...)

唯一的问题是 - 代码会抛出异常,而我想要保持日志清晰

有没有选项可以避免打印异常,只是忽略测试用例?
或者通过其他解决方案跳过它?

谢谢

英文:

I have Cucumber .feature file with multiple test cases (using "Examples").<br>
In some situation i want to skip few of the test cases and run only few of them.

Note:
User should be able to select which test case to skip dynamically<br>
For example, he can decide in first run to skip test case number 1, next time to skip number 2.

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

I managed to do that using

Assume.assumeTrue(...)

The only issue is - code throws Exception, and i want to keep logs clear.

There is any option to avoid print the exception, and just ignore the test case ?
or skip it by another solution ?

Thanks

答案1

得分: 2

我会为您为每个要跳过测试并标记为 @todo 的情景拆分示例,如下所示:

情景大纲:[测试情景-001] 发送带有请求类型的新表单
假设 我使用“request”预加载表单
并且 我选择“提交”按钮
那么 响应消息“hello”被返回
示例:
| request |
| POST    |
@todo
示例:
| request | 
| GET     |
| PUT     |
| DELETE  |

然后,要仅运行第一个“示例”的情景,请排除标签以不作为功能的一部分运行:

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

要运行所有“示例”情景,请不要使用该标签。

请注意,这是您提供的内容的翻译。如果您有任何其他需要翻译的内容,请随时告诉我。

英文:

I would split your Examples for each scenario where you want to skip tests and tag with @todo, like so:

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

Then to run the scenario for the 1st Example only, call out the tag not to be run as part of the feature:

-Dcucumber.options=&quot;--tags ~@todo&quot;

To run all Example scenarios, do not use the tag

答案2

得分: 0

Finally I found a simple solution, by using the same method I have mentioned Assert.assume(...), just need to clear the exception stack trace, and re-throw it.

In the below code you can see the actual change is just I added the catch block:

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

Now the exception stack trace is not printed to the log, so logs are kept clean, and I just see this instead:

> org.junit.AssumptionViolatedException: got: <false>, expected: is <true>

This is good enough for me.

英文:

Finally I found a simple solution, by using the same method i have mentioned Assert.assume(...), just need to clear the exception stack trace, and re-throw it.

In below code you can see the actual change is just i added the catch block:

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

Now exception stack trace is not printed to log, so logs kept clean, and i just see this instead:

> org.junit.AssumptionViolatedException: got: &lt;false>, expected: is &lt;true>

This is good enough for me.

huangapple
  • 本文由 发表于 2020年9月6日 16:16:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63762108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定