尽管“注释在找到的胶水类上有所不同”,但如何在其自己的Spring上下文配置中使用通用的Cucumber步骤定义

neX*_*Xus 5 spring cucumber cucumber-java

我有一个经过Cucumber测试的应用程序,但是由于升级(Cucumber 1.1.6到1.2.5,java 1.6到1.8,Spring 3.2.0到4.2.6),它不再起作用,因为它抱怨 Annotations differs on glue classes found

结构如下:

  • 一些常见的stepdef,需要一些属性值
  • 更具体的stepdef,需要一些 @ContextConfiguration

这两个都应该共享一个bean。

公用部分永远不会单独运行。但是我有多个测试,每个测试都使用自己的特定stepdef。现在由于拒绝运行Annotations differs on glue classes found
有没有一种方法可以使它运行而不会用具体上下文的细节污染公共上下文?


步骤定义:

@ContextConfiguration("classpath:cucumber-common.xml")
public class CommonStepdefs {
    @Autowired
    private SharedBean sharedBean;
    @Value("${some.property}")
    private String someProperty;

    // actual step def methods
}

@ContextConfiguration("classpath:cucumber-concrete.xml")
public class ConcreteStepdefs {
    @Autowired
    private SharedBean sharedBean;
    @Autowired
    private OtherBean otherBean;

    // actual step def methods
}
Run Code Online (Sandbox Code Playgroud)

常见的Spring配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:com/example/cucumber-common.properties"/>
    <context:spring-configured/>
    <context:annotation-config/>

    <bean id="glueCodeScope" class="cucumber.runtime.java.spring.GlueCodeScope"/>
    <bean id="glueCodeScopeConfigurer" class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="cucumber-glue" value-ref="glueCodeScope"/>
            </map>
        </property>
    </bean>

    <bean id="sharedBean" class="com.example.SharedBean" scope="cucumber-glue"/>
</beans>
Run Code Online (Sandbox Code Playgroud)

另一种Spring配置(导入通用配置):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="classpath:cucumber-common.xml"/>

    <context:spring-configured/>
    <context:annotation-config/>
    <context:component-scan base-package="com.example.rest"/>

    <!-- more bean definitions -->
</beans>
Run Code Online (Sandbox Code Playgroud)

使用以下命令运行测试:

@RunWith(Cucumber.class)
@CucumberOptions(
    format = { "html:target/cucumber-html-report", "json:target/cucumber-json-report.json" },
    glue = { "com.example.common", "com.example.concrete" },
    monochrome = true,
    strict = true)
Run Code Online (Sandbox Code Playgroud)

use*_*594 0

您可以使用qaf-gherkin来简化此类需求的实现。使用 QAF,您无需使用 spring 即可实现。它还支持来自打包 jar 的步骤。如果您在一个包中具有通用步骤,而在不同包中具有特定于平台的步骤,则您的配置可能如下所示:

step.provider.pkg=com.myapp.steps.common;com.myapp.steps.web
Run Code Online (Sandbox Code Playgroud)

如果您想一起运行,可以在 TestNG xml 配置文件中指定,如下所示:

<test name="Test-web">
   <parameter name="step.provider.pkg" value="com.myapp.steps.common;com.myapp.steps.web" />
   <parameter name="scenario.file.loc" value="resources/features" />
   <classes>
      <class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
   </classes>
</test>
<test name="Test-mobile">
   <parameter name="step.provider.pkg" value="com.myapp.steps.common;com.myapp.steps.mobile" />
   <parameter name="scenario.file.loc" value="resources/features" />
   <classes>
      <class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
   </classes>
</test>
Run Code Online (Sandbox Code Playgroud)

好处之一是,常见步骤可以放在 jar 中,您可以在多个项目之间共享。与 spring 一样@Autowired,QAF 支持@Inject注释。