简体   繁体   English

在 Cucumber 中的场景大纲挂钩之后

[英]After Scenario Outline hook in Cucumber

I'm using Java and Cucumber.我正在使用 Java 和 Cucumber。 I need to do some actions after every scenario outline.我需要在每个场景大纲之后做一些动作。 I know there are @Before and @After hooks, but they are applicable to every scenario in Scenario Outline.我知道有 @Before 和 @After 钩子,但它们适用于场景大纲中的每个场景。 Is there any possibility to launch some actions exactly after all scenarios in outline and not after every scenario?是否有可能在大纲中的所有场景之后而不是在每个场景之后完全启动某些操作?

Example:例子:

   Scenario Outline: Some scenario
   Given given actions
   When when actions
   Then display <value>
Examples:
|value|
|a    |
|b    |

I want to make execution in following way:我想通过以下方式执行:

@Before actions @Before 行动

a value一个值

b value b值

@After actions @After 动作

@Before actions @Before 行动

//another scenario outline output //另一个场景大纲输出

@After actions @After 动作

Yes, you can use Tags.是的,您可以使用标签。

   @tag1
   Scenario Outline: Some scenario
   Given given actions
   When when actions
   Then display <value>
Examples:
|value|
|a    |
|b    |

Here we have specified tag1 as a tag.这里我们将 tag1 指定为标签。

Now in step definition, you can use something like this:现在在步骤定义中,您可以使用以下内容:

@Before("@tag1")
public void testSetup(){
    ...
} 

@After("@tag1")
public void testEnd(){
    ...
} 

These @before and @after are specific to tag1 now.'这些@before 和@after 现在特定于 tag1。 I hope this helps.我希望这有帮助。

One way of doing this is to make use of the BeforeClass and AfterClass methods of either junit or testng for the runner.这样做的一种方法是为运行程序使用junit 或testng 的BeforeClass and AfterClass方法。 The problem is there can be only one scenariooutline in the feature file and it will need a unique runner class for it.问题是功能文件中只能有一个场景大纲,并且需要一个唯一的运行程序类。


Having a method run only for the first scenario run off a scenario outline is pretty easy.只为第一个场景运行的方法运行场景大纲是很容易的。 Create a before hook to run once only using a static flag.创建一个 before 钩子,只使用静态标志运行一次。 Modify the Before hook to run for certain tags etc..修改 Before 钩子以针对某些标签等运行。

private static boolean skipFlag = false;

@Before
public void beforeHook() {

    if(!skipFlag) {
        do stuff
        skipFlag=true;
    }
}

Though this works well with one scenario outline in a feature file.尽管这适用于功能文件中的一个场景大纲。 Else you will need to replicate flags fro each scenariooutline.否则,您将需要从每个场景大纲复制标志。 For parallel running it may get messy.对于并行运行,它可能会变得混乱。


A better but a little complex solution which requires to split the examples table into 3 and give the top and bottom part tags.一个更好但有点复杂的解决方案,需要将示例表分成 3 个并给出顶部和底部的标签。

@AftBef
Scenario Outline:
When User Selects <Origin>, <Destination>

@StartIt
Examples:
    | Origin        | Destination        |
    | London        | New York           |

Examples:
    | Origin        | Destination        |
    | Munich        | Moscow             |
    | Rome          | Shanghai           |

@EndIt
Examples:
    | Origin        | Destination        |
    | Miami         | San Francisco      |

Add the below hook methods with tags.添加以下带有标签的钩子方法。 You can also add tag(s) to the middle part if there is any custom logic.如果有任何自定义逻辑,您还可以在中间部分添加标签。

@Before(value={"@StartIt"})
public void startItAll() {

    System.out.println("-----START IT BIRTH BIRTH----");
}

@After(value={"@EndIt"})
public void endItAll() {

    System.out.println("-----END IT DIE DIE---------");
}

If you have only one example just add both tags to the examples like below.如果您只有一个示例,只需将两个标签添加到如下示例中即可。

 @StartIt @EndIt
    Examples:

Need to be careful in creating the table to make sure only one example row is present for the @StartIt and @EndIt tags.在创建表格时需要小心,以确保 @StartIt 和 @EndIt 标签只存在一个示例行。

One of the reasons this is difficult and not supported by Cucumber is because it is a really bad idea.这很困难并且不受 Cucumber 支持的原因之一是因为它是一个非常糟糕的主意。 Good testing practice stipulates that you start from a clean slate before you run each test.良好的测试实践规定,在运行每个测试之前,您必须从头开始。 If you link tests together like you are doing then when something goes wrong you have to work out whether the test has failed, or something in the previous tests has caused a problem.如果您像现在一样将测试链接在一起,那么当出现问题时,您必须确定测试是否失败,或者之前测试中的某些内容导致了问题。 This quickly becomes a tremendous PITA.这很快就变成了一个巨大的 PITA。

TLDR don't do this you will live to regret it ;) TLDR 不要这样做,你会后悔的 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM