如何避免 Cucumber 重新创建或刷新应用上下文

How to avoid Cucumber from recreating or refreshing the app context

我的 Cucumber 测试在每次通过时都会刷新 spring 启动上下文,我有一些数据库缓存正在完成,这会降低编译过程的性能。

我的抽象测试被标记为 @SpringBootTest(defined_port) 和 @ContextConfiguration(loader=SpringBootContextLoader.class).

我已经尝试添加 DirtiesContext 但它没有用...有什么想法吗?

如果您使用最新的 Cucumber (v5.7.0) 让 Cucumber 知道您的测试配置,您可以使用 @CucumberContextConfiguration 和其中之一在您的粘合路径上注释配置 class以下注释:@ContextConfiguration@ContextHierarchy@BootstrapWith。如果您使用的是 SpringBoot,则可以将配置 class 注释为 @SpringBootTest

例如:

import com.example.app;
import org.springframework.boot.test.context.SpringBootTest;
import io.cucumber.spring.CucumberContextConfiguration; 

@CucumberContextConfiguration 
@SpringBootTest(classes = TestConfig.class) public class 
CucumberSpringConfiguration { }

然后您可以 @Autowire 将应用程序上下文中的组件放入任何步骤定义文件中。不需要进一步的 spring 配置。例如:

package com.example.app;

public class MyStepDefinitions { 

 @Autowired
 private MyService myService;

 @Given("feed back is requested from my service")
 public void feed_back_is_requested(){ 
    myService.requestFeedBack(); 
  }
}

唯一的要求是 MyStepDefinitionsCucumberSpringConfiguration 都在粘合路径上的包中。因此,要么您已明确配置 @CucumberOptions(glue="com.example"),要么您的测试运行器 class 与您的步骤定义 (com.example).

在同一个包中

您可以在 github 的 cucumber-spring 模块中找到更多信息。 https://github.com/cucumber/cucumber-jvm/tree/master/spring