当第一个场景失败时如何跳过所有 cucumber-jvm 场景 [英] How to skip all cucumber-jvm scenarios when first scenario is failed

查看:25
本文介绍了当第一个场景失败时如何跳过所有 cucumber-jvm 场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 8 个 cucumber-jvm 场景,第一个场景测量页面加载时间和环境可用性.为了避免不必要的运行,如果第一个场景失败——例如,环境不可用,或者加载太慢——应该跳过所有其他场景.

我该怎么做?

我的CucumberOptions:

@RunWith(Cucumber.class)@黄瓜选项(严格=真,特征 = {"src/test/resources/features"},胶水= {stepDefinitions"},格式= {进度",html:目标/结果","json:target/Results/cucumber.json"},标签 = {"@test"})公共类TestRunner {}

谢谢!

解决方案

你可以利用 Assume.assumeTrue(false) 跳过测试.但这需要对测试运行程序和代码进行一些更改.

  1. 为检查环境细节是否正常工作的场景创建一个单独的运行器.让我们称它为 RunFirstTest.java 并给 tags 一个 @Smoke 的值.其他选项值默认为您拥有的值.

<块引用>

@RunWith(Cucumber.class)@CucumberOptions(plugin={ }, tags={"@Smoke"}, 胶水="", features="")公共类 RunFirstTest {

  1. @Smoke 标签添加到检查环境等的功能文件中的场景中.您也可以考虑拆分功能文件.

  2. 创建一个新类来保存一个静态标志.这是一个简单的实现,您可能会考虑使其更加健壮.

<块引用>

公共类 SkipFlag {公共静态布尔skipFlag = false;}

  1. 创建一个 After hook,并将 value 选项设置为 @Smoke.因此它只会在烟雾场景中运行.

<块引用>

@After(value={"@Smoke"})公共无效后跳过(场景场景){如果(场景.isFailed())SkipFlag.skipFlag = true;}

  1. 为主要测试创建第二个运行器.我们将其命名为 RunMainTest.java 并为其 tags 值 @MainTests.其他选项值默认为您拥有的值.

<块引用>

@RunWith(Cucumber.class) @CucumberOptions(plugin={" "},tags={"@MainTests"},glue="", features="") 公共类 RunMainTest{
@课前公共静态无效之前(){如果(SkipFlag.skipFlag)假设.assumeTrue(false);}}

  1. @MainTests 标签添加到功能文件中的其他场景中.或者,您可以查看拆分功能文件并在 features 选项值中给出功能文件的名称.

  2. 使用 maven 故障安全插件运行它.在这个插件的配置中,在 pom.xml 中添加这 2 个运行器.

<块引用>

 <配置><包括><include>RunFirstTest</include><include>RunMainTest</include></包括><runOrder>按字母顺序排列</runOrder></配置>

如果您只有 2 个跑步者,则包含部分可能是可选的.最重要的一点是RunFirstTest应该是第一个运行的,所以按字母顺序应该是第一个.

  1. 使用 maven 运行它.

希望它有效.

I have 8 cucumber-jvm scenarios and the very first scenario measures the page load time and environment availability. In order to avoid unnecessary runs, if the first scenario is failed - for instance, the environment is not available, or loading too slowly - all other scenarios should be skipped.

How can I do that?

My CucumberOptions:

@RunWith(Cucumber.class)
@CucumberOptions(
        strict = true,
        features = {"src/test/resources/features"},
        glue = {"stepDefinitions"},
        format = {  "progress", "html:target/Results",
                "json:target/Results/cucumber.json"},
        tags = {"@test"})
        public class TestRunner {
}

Thanks!

解决方案

You can make use of Assume.assumeTrue(false) to skip tests. But this will require some changes in test runner and code changes.

  1. Create a separate runner for the scenario which checks the environment details are all working. Let's call it RunFirstTest.java and give tags a value of @Smoke. Other option values default to what you have.

@RunWith(Cucumber.class)
@CucumberOptions(plugin={ }, tags={"@Smoke"}, glue=" ", features=" ")
public class RunFirstTest {

  1. Add the @Smoke tag to the scenario in the feature file that checks the environment etc. Optionally you could look at splitting the feature files.

  2. Create a new class to hold a static flag. It is a simple implementation you might look at making this more robust.

public class SkipFlag {   
     public static boolean skipFlag = false; }

  1. Create an After hook with the value option set to @Smoke. Thus it will run only for the smoke scenario.

@After(value={"@Smoke"})  
public void afterSkip(Scenario scen) {        
   if(scen.isFailed())            
      SkipFlag.skipFlag = true;   
}

  1. Create a second runner for the main tests. Let's call it RunMainTest.java and give its tags value of @MainTests. Other option values default to what you have.

@RunWith(Cucumber.class) @CucumberOptions(plugin={" "}, tags={"@MainTests"}, glue=" ", features=" ") public class RunMainTest {
@BeforeClass public static void before() { if(SkipFlag.skipFlag) Assume.assumeTrue(false); } }

  1. Add the @MainTests tag to the other scenarios in the feature file. Optionally you could look at splitting the feature files and give the name of the feature file in the features option value.

  2. Run this by using maven failsafe plugin. In the configuration of this plugin add the inclusion of these 2 runners in the pom.xml.

 <configuration>
      <includes>
          <include>RunFirstTest</include>
          <include>RunMainTest</include>
      </includes>
        <runOrder>alphabetical</runOrder>
 </configuration>

The includes part might be optional if you only have 2 runners. The most important point is that the RunFirstTest should be the first to run, so alphabetically should be first.

  1. Run it with maven.

Hope it works.

这篇关于当第一个场景失败时如何跳过所有 cucumber-jvm 场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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