在特定function之前/之后执行Cucumber步骤

我想为每个特定的function文件指定某些设置和拆除步骤。 我已经看过允许代码在每个场景之前执行的钩子,并在每个function之前挂钩执行代码,但我想指定在一个特定function运行所有场景之前和之后运行一次的代码。

这可能吗?

你用的是黄瓜-jvm吗? 我发现了一篇符合您要求的文章。

http://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/

基本上,不要使用JUnit @BeforeClass和@AfterClass,因为他们不知道Cucumber Hook标签。 您是否希望Init和Teardown方法仅适用于某些场景?

如果您使用junit来运行测试。 我们使用注释来创建unit testing类和单独的步骤类。 标准@Before的东西在steps类中,但@BeforeClass注释可以在主unit testing类中使用:

@RunWith(Cucumber.class) @Cucumber.Options(format = {"json", ""}, strict = false, glue = {" 

试试这个 :

在function文件中:

 @tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly Feature : My new feature .... 

在Stepdefinitions.java中

 @Before("@tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly") public void testStart() throws Throwable { } @After("@tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly") public void testStart() throws Throwable { } 

你可以使用somefeaturefile.feature

背景:鉴于我有一个苹果

情景:吃苹果当我吃苹果然后我的肚子应该长大

在这里,在后台编写的动作将在每个场景之前运行。