java 在其他项目中重用 Cucumber-JVM 步骤定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12867492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:34:16  来源:igfitidea点击:

Reusing Cucumber-JVM step definitions in other projects

javacucumber-jvm

提问by kazakovs

How can I reuse Cucumber-JVM Step Definitions in other projects to test some typical web actions. The point is that I've created some java project just with Step Definition Implementations of the typical scenario actions like:

如何在其他项目中重用 Cucumber-JVM Step Definitions 来测试一些典型的 Web 操作。关键是我已经创建了一些 Java 项目,其中包含典型场景操作的 Step Definition Implementations,例如:

When I follow the link "*some_link*"
Then I should see following content "*some_content*" on page

And I want to reuse these definitions in other projects (include in classpath), just to write their own simple scenarios. But when I run the scenario (as JUnit test), Cucumber cant find Step Definitions. And when I try to extend Step Definitions class it gives me an error, that I can't extent Step Definition class. So, is it possible to reuse Step Definitions and if so than how?

而我想在其他项目中(包括在classpath中)重用这些定义,只是为了编写自己的简单场景。但是当我运行场景(作为 JUnit 测试)时,Cucumber 找不到步骤定义。当我尝试扩展 Step Definition 类时,它给了我一个错误,我无法扩展 Step Definition 类。那么,是否可以重用步骤定义,如果可以,如何重用?

回答by kazakovs

Well, I've found solution. It appears quite easy.

嗯,我找到了解决方案。看起来很容易。

There is annotation @CucumberOptions which has parameter "glue". Pointing with this parameter to the base package that has the step definitions forces Cucumber to search in that base package. The test runner will look like:

有一个注释@CucumberOptions,它有参数“glue”。使用此参数指向具有步骤定义的基本包会强制 Cucumber 在该基本包中进行搜索。测试运行程序将如下所示:

@RunWith(Cucumber.class)
@CucumberOptions(glue = { "com.example.stepdefspackage" })
public class Run{
}