在Spring Boot应用中使用两个不同的步骤定义运行相同的功能文件

huangapple go评论65阅读模式
英文:

Running same feature file with two different Step definitions in a Springboot application

问题

我正在使用Cucumber(v7.2.3)测试一个Springboot应用程序。我有一个功能文件,使用@SpringBootTest和@Suite(来自junit-platform)来测试应用程序,我使用@ConfigurationParameter引用包含步骤定义类的包。

我还有@DataTableType注解,将功能数据转换为Java对象,这些对象与我的步骤定义类在同一个包中。

现在,我想要另一组步骤定义(在另一个文件中),这将连接到在dev环境中运行的应用程序(通过REST)。我想重用我的功能文件,确切地在运行中的应用程序上测试相同的功能。

我尝试在不同的包中创建一个新的步骤定义Java类,并将现有的步骤定义Java类放在另一个包中,但功能数据到Java对象的创建失败了。可能Cucumber期望在与步骤定义类相同的包中有带有@DataTableType注解的类。

有没有办法实现使用相同功能文件的两个不同步骤定义?

注意:我希望本地步骤定义作为Maven构建的一部分运行,而远程步骤定义作为部署后任务运行。请还指导我如何实现这一点(可能使用标签)。

使用的版本
junit-platform - 1.8.2
junit-jupiter - 5.8.2
springboot - 2.7.4

英文:

I am testing a Springboot application using cucumber (v7.2.3). I have a feature file to test application using @SpringBootTest and @Suite (from junit-platform) I am using @ConfigurationParameter to refer the package where I have my step definition class.

I also have @DataTableType annotations to convert feature data into java objects, which are in the same package as my step-definitions.

Now I want to have another set of step definitions (in a separate file) which will connect to the application running in dev environment (over REST). I want to re-use my feature files as testing exactly same feature but on a running application.

I tried to create a new step definition java class in a different package and placed existing step definition java class in another package, but feature data to java object creation failed. Probably cucumber is expecting to have class which has @DataTableType annotations in the same package as step definition class.

Any idea how can I achieve 2 different step definitions using same feature file.

Note: I want to run local step definitions to run as part of maven build and remote step definitions as post deployment task. Please also guide how can I achieve that (probably using tags).

Versions used
junit-platform - 1.8.2
junit-jupiter - 5.8.2
springboot - 2.7.4

答案1

得分: 1

假设您将代码组织成三个包:`com.example.local`,`com.example.remote`,`com.example.shared`。然后将本地测试的步骤定义放在`.local`远程测试的步骤定义放在`.remote`数据表类型和其他共享代码放在`.shared`包中

然后您可以为粘合代码创建多个具有不同配置的套件

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("com/example")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.local, com.example.shared")
public class RunLocalCucumberTest {
}

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("com/example")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.remote, com.example.shared")
public class RunRemoteCucumberTest {
}
英文:

Suppose you organize your code into three packages com.example.local, com.example.remote, com.example.shared. Then you would put the step definitions for the local tests in .local, those for remote in .remote and the data table types and other shared code in the .shared package.

Then you can create multiple suites with different configurations for the glue.

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("com/example")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.local, com.example.shared")
public class RunLocalCucumberTest {
}

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("com/example")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.remote, com.example.shared")
public class RunRemoteCucumberTest {
}

huangapple
  • 本文由 发表于 2023年4月6日 21:13:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定