java 如何根据特征文件位置在黄瓜java中创建动态胶水?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36479461/
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-11-03 01:30:44  来源:igfitidea点击:

How to create dynamic glue in cucumber java based on the feature file location?

javacucumbercucumber-jvmcucumber-junitcucumber-java

提问by googytech

I am having a java project which is built using maven.

我有一个使用 maven 构建的 java 项目。

I have multiple packages,feature files based on different functionalities. The project test structure is as below.

我有多个包,基于不同功能的功能文件。项目测试结构如下。

src
  ->test
    ->java
      ->com
        ->usercreation
          TestStepDef.java
        ->uservalidation
          TestStepDef.java
   ->resources
        ->usercreation
           usercreation.feature
        ->uservaliation
           uservalidatin.feature

I have only one RunCukesTest.java file

我只有一个 RunCukesTest.java 文件

@RunWith(Cucumber.class)
@CucumberOptions(format = { "html:target/cucumber-html-report",
                        "json:target/cucumber-json-report.json" }, 
                        features = "src/test/resources/",
                        glue = "??????",
                        tags = {"~@ignore"}
            )
 public class RunCukesTest {

 }

In this case it runs all my feature files. But it is not able to find my specific Step definitions for the feature. So I have to give the glue option as "com.usercreation". But if I do so when it runs the uservalidation feature file it will not be able to pick up the appropriate step definition. In my use case I don't want both the step def file to be in the same package, as they have many steps with different functionalities.

在这种情况下,它运行我所有的功能文件。但它无法找到我对该功能的特定步骤定义。所以我必须将胶水选项指定为“com.usercreation”。但是,如果我在运行 uservalidation 功能文件时这样做,它将无法获取适当的步骤定义。在我的用例中,我不希望 step def 文件位于同一个包中,因为它们有许多具有不同功能的步骤。

Is there a possible way where I can give the glue option dynamically based on the package name the feature file is running. Or am I missing any other approach to this project.

有没有一种可能的方法可以根据功能文件正在运行的包名称动态地提供胶水选项。或者我错过了这个项目的任何其他方法。

回答by MikeJRamsey56

glue = { "classpath:com/usercreation", "classpath:com/uservalidation" },

回答by Thomas Sundberg

Move your RunCukesTestto a package above all the steps. Cucumber will search its classpath and find any step in the same package or a subpackage.

将您移至RunCukesTest所有步骤之上的包。Cucumber 将搜索它的类路径并找到同一个包或子包中的任何步骤。

Setting the location as suggested by MikeJRamsey56 is another option.

按照 MikeJRamsey56 的建议设置位置是另一种选择。

回答by Peter

Whilst some good alternative solutions have been provided, I haven't seen anyone answer your question directly.

虽然已经提供了一些很好的替代解决方案,但我还没有看到有人直接回答您的问题。

Yes it is possible to dynamically change @CucumberOptions (cucumber annotation), however it require the use of Java reflection since annotations cant be parameterized.

是的,可以动态更改@CucumberOptions(黄瓜注​​释),但是它需要使用 Java 反射,因为注释不能被参数化。

To see an example of cucumber annotations being dynamically modified, please refer to this project: https://github.com/workpeter/ARGOS

看一个黄瓜注解动态修改的例子,请参考这个项目:https: //github.com/workpeter/ARGOS

In particular the runner class. Which as of today can be found in this location: https://github.com/workpeter/ARGOS/blob/master/src/test/java/integrationTests/cucumber/Runner.java

特别是跑步类。截至今天,可以在此位置找到:https: //github.com/workpeter/ARGOS/blob/master/src/test/java/integrationTests/cucumber/Runner.java