是否可以将 Cucumber 配置为 运行 具有不同 spring 配置文件的相同测试?
Is it possible to configure cucumber to run the same test with different spring profiles?
我有一个应用程序,我正在 运行试用不同的技术。我有一组与每种技术一起实现的接口,我使用 spring 配置文件来决定使用哪种技术 运行。每项技术都有自己的 Spring java 配置,并用它们所针对的配置文件进行注释。
我 运行 我的黄瓜测试定义了哪个配置文件是活动的,但这迫使我每次想测试不同的配置文件时都手动更改字符串,从而无法 运行 自动化测试所有这些。无论如何,黄瓜中是否有提供一组配置文件,因此每个测试 运行 一次?
谢谢!
你有两种可能
- 标准 - 使用 Cucumber runners
的一些测试 类 运行
- 编写支持多种配置的自定义 Cucumber jUnit 运行程序(或准备一个)。
在第一种情况下,它将如下所示。缺点是您必须为每个运行器定义不同的报告,并且每个配置都有几乎相同的 Cucumber 运行器。
下面是 类 的样子:
CucumberRunner1.java
@RunWith(Cucumber.class)
@CucumberOptions(glue = {"com.abc.def", "com.abc.common"},
features = {"classpath:com/abc/def/",
"classpath:com/abc/common.feature"},
format = {"json:target/cucumber/cucumber-report-1.json"},
tags = {"~@ignore"},
monochrome = true)
public class CucumberRunner1 {
}
StepAndConfig1.java
@ContextConfiguration(locations = {"classpath:/com/abc/def/configuration1.xml"})
public class StepsAndConfig1 {
@Then("^some useful step$")
public void someStep(){
int a = 0;
}
}
CucumberRunner2.java
@RunWith(Cucumber.class)
@CucumberOptions(glue = {"com.abc.ghi", "com.abc.common"},
features = {"classpath:com/abc/ghi/",
"classpath:com/abc/common.feature"},
format = {"json:target/cucumber/cucumber-report-2.json"},
tags = {"~@ignore"},
monochrome = true)
public class CucumberRunner2 {
}
OnlyConfig2.java
@ContextConfiguration(classes = JavaConfig2.class)
public class OnlyConfig2 {
@Before
public void justForCucumberToPickupThisClass(){}
}
第二种方法是使用支持多种配置的自定义 Cucumber runner。你可以自己写,也可以拿现成的,比如我的-CucumberJar.java and root of project cucumber-junit。
在这种情况下,Cucumber runner 将如下所示:
CucumberJarRunner.java
@RunWith(CucumberJar.class)
@CucumberOptions(glue = {"com.abc.common"},
tags = {"~@ignore"},
plugin = {"json:target/cucumber/cucumber-report-common.json"})
@CucumberGroupsOptions({
@CucumberOptions(glue = {"com.abc.def"},
features = {"classpath:com/abc/def/",
"classpath:com/abc/common.feature"}
),
@CucumberOptions(glue = {"com.abc.ghi"},
features = {"classpath:com/abc/ghi/",
"classpath:com/abc/common.feature"}
)
})
public class CucumberJarRunner {
}
我有一个应用程序,我正在 运行试用不同的技术。我有一组与每种技术一起实现的接口,我使用 spring 配置文件来决定使用哪种技术 运行。每项技术都有自己的 Spring java 配置,并用它们所针对的配置文件进行注释。
我 运行 我的黄瓜测试定义了哪个配置文件是活动的,但这迫使我每次想测试不同的配置文件时都手动更改字符串,从而无法 运行 自动化测试所有这些。无论如何,黄瓜中是否有提供一组配置文件,因此每个测试 运行 一次?
谢谢!
你有两种可能
- 标准 - 使用 Cucumber runners 的一些测试 类 运行
- 编写支持多种配置的自定义 Cucumber jUnit 运行程序(或准备一个)。
在第一种情况下,它将如下所示。缺点是您必须为每个运行器定义不同的报告,并且每个配置都有几乎相同的 Cucumber 运行器。
下面是 类 的样子:
CucumberRunner1.java
@RunWith(Cucumber.class)
@CucumberOptions(glue = {"com.abc.def", "com.abc.common"},
features = {"classpath:com/abc/def/",
"classpath:com/abc/common.feature"},
format = {"json:target/cucumber/cucumber-report-1.json"},
tags = {"~@ignore"},
monochrome = true)
public class CucumberRunner1 {
}
StepAndConfig1.java
@ContextConfiguration(locations = {"classpath:/com/abc/def/configuration1.xml"})
public class StepsAndConfig1 {
@Then("^some useful step$")
public void someStep(){
int a = 0;
}
}
CucumberRunner2.java
@RunWith(Cucumber.class)
@CucumberOptions(glue = {"com.abc.ghi", "com.abc.common"},
features = {"classpath:com/abc/ghi/",
"classpath:com/abc/common.feature"},
format = {"json:target/cucumber/cucumber-report-2.json"},
tags = {"~@ignore"},
monochrome = true)
public class CucumberRunner2 {
}
OnlyConfig2.java
@ContextConfiguration(classes = JavaConfig2.class)
public class OnlyConfig2 {
@Before
public void justForCucumberToPickupThisClass(){}
}
第二种方法是使用支持多种配置的自定义 Cucumber runner。你可以自己写,也可以拿现成的,比如我的-CucumberJar.java and root of project cucumber-junit。 在这种情况下,Cucumber runner 将如下所示:
CucumberJarRunner.java
@RunWith(CucumberJar.class)
@CucumberOptions(glue = {"com.abc.common"},
tags = {"~@ignore"},
plugin = {"json:target/cucumber/cucumber-report-common.json"})
@CucumberGroupsOptions({
@CucumberOptions(glue = {"com.abc.def"},
features = {"classpath:com/abc/def/",
"classpath:com/abc/common.feature"}
),
@CucumberOptions(glue = {"com.abc.ghi"},
features = {"classpath:com/abc/ghi/",
"classpath:com/abc/common.feature"}
)
})
public class CucumberJarRunner {
}