QAF 黄瓜 testNG spring 启动
QAF cucumber testNG with spring boot
我有一个使用 spring boot 和 testng 的 cucumber 项目。
这里是校长 classes
@SpringBootTest
public class CucumberTestDefinitions extends FunctionalTesting {
@Given("Something")
public void smthg(){
}
}
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = FunctionalTestingApp.class, initializers =
ConfigFileApplicationContextInitializer.class)
public class FunctionalTesting {
@Autowired
protected FunctionalTestingConfiguration configuration;
}
@EnableConfigurationProperties(value = {FunctionalTestingConfiguration.class})
public class FunctionalTestingApp {
}
@EnableAutoConfiguration
@ConfigurationProperties(prefix = "testing")
@Data
public class FunctionalTestingConfiguration {
// the config from yml file
}
我没有 testng.xml 文件,因为测试是用 spring-boot 启动的。
出于某些原因,我想对测试进行优先排序,我发现我可以使用 QAF (https://qmetry.github.io/qaf/latest/scenario-meta-data.html#pre-defined-meta-data-for-bdd)。
我尝试使用它,但没有用。
这是我所做的:
我添加了对pom.xml的依赖(我用的是cucumber 5)
我将此注释 @QAFTestStepProvider
添加到 CucumberTestDefinitions class
我在 RunnerTest class
中添加了这个插件 "com.qmetry.qaf.automation.cucumber.QAFCucumberPlugin"
错误在这里
java.lang.NoSuchMethodError: 'java.lang.reflect.Method com.qmetry.qaf.automation.step.client.TestNGScenario.getMethod()'
我尝试在配置文件夹中添加一个 testng.xml 文件,但它没有帮助
QAF spring-boot 兼容吗?
非常感谢您的帮助
编辑
pom.xml :
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.qmetry</groupId>
<artifactId>qaf-cucumber</artifactId>
<version>3.0.0</version>
</dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
奔跑者class
features = {"src/test/resources/toto"})
public class RunnerTest extends AbstractTestNGCucumberTests {
@Autowired
private ObjectMapper objectMapper;
@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
@PostConstruct
public void setUp() {
objectMapper.registerModule(new JavaTimeModule());
}
}
错误信息
java.lang.NoSuchMethodError: 'java.lang.reflect.Method com.qmetry.qaf.automation.step.client.TestNGScenario.getMethod()'
at com.qmetry.qaf.automation.step.client.TestNGScenario.init(TestNGScenario.java:92)
at com.qmetry.qaf.automation.step.client.TestNGScenario.<init>(TestNGScenario.java:70)
at com.qmetry.qaf.automation.step.client.TestNGScenario.<init>(TestNGScenario.java:64)
at com.qmetry.qaf.automation.testng.TestRunnerFactory.convert(TestRunnerFactory.java:76)
at com.qmetry.qaf.automation.testng.TestRunnerFactory.init(TestRunnerFactory.java:67)
at com.qmetry.qaf.automation.testng.TestRunnerFactory.newTestRunner(TestRunnerFactory.java:63)
at org.testng.ITestRunnerFactory.newTestRunner(ITestRunnerFactory.java:55)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:676)
at org.testng.SuiteRunner.init(SuiteRunner.java:178)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:112)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1275)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1251)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1100)
at org.testng.TestNG.runSuites(TestNG.java:1039)
at org.testng.TestNG.run(TestNG.java:1007)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)
不幸的是,我无法重现相同的错误并获得有关依赖项的其他错误。但是,要解决您最初的问题“QAF cucumber testNG with spring boot”和“like to priority tests”方面,简短的回答是肯定的。但可能不会同时使用 TESTNG 和 Cucumber 运行ner,特别是优先测试。这是什么意思?
当您使用 Cucumber 建议的 TestNG 方式时,它只会在 TestNG 中创建一个虚拟测试并将场景作为测试数据提供。哪个不能将每个场景都视为独立的 TestNG 测试!所以您将无法从 TestNG 中获得所有好处(例如,优先级)。
但是,QAF+Cucmber+TestNG并用并不代表不能实现或不能使用。当你使用 QAF it has pure TestNG implementation for BDD 时。 QAF 将每个场景视为 TestNG 测试,将带有示例的场景(场景大纲)视为 data-driven 测试。 qaf-cucuber
将允许使用黄瓜步骤,包括您选择的依赖注入,Spring 在您的情况下。您将受益于所有 TestNG 功能,包括并行执行、侦听器和 QAF 的扩展功能,如 meta-data 过滤器、test-data 过滤器、
所以当你想使用 TestNG 时,最好提供 TestNG configuration to run your feature files written in BDD2 或 Gherkin(BDD2 的子集)。您需要做的就是创建以下 XML 文件到 运行 使用 BDD2 或 Gherkin 编写的功能文件。
<suite name="QAF Demo" verbose="0">
<parameter name="step.provider.pkg" value="pkg.from.where.steps.needs.tobe.loaded" />
<parameter name="scenario.file.loc" value="src/test/resources/toto" />
<test name="BDD Test" enabled="true">
<classes>
<class name="com.qmetry.qaf.automation.step.client.text.BDDTestFactory2"></class>
</classes>
</test>
</suite>
注:
将 step.provider.sharedinstance
设置为 true
当使用黄瓜步骤实现时使用步骤中使用的 class 个变量。
在上面的方式中,因为你没有使用黄瓜运行ner,所以任何黄瓜钩子都不会被执行。当您使用 TestNG 时,它具有更好的侦听器支持,您可以利用它,并且 step listener 您可以将其与 QAF 一起使用。因此,如果您实现了任何挂钩,您可以将其移动到适当的 before/after 侦听器方法中并在配置文件中注册侦听器。
编辑:
我尝试了你分享的测试项目,我 运行 也遇到了不同的问题。最后我能够弄明白如下。
POM 更新:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>test</scope>
</dependency>
你有两个选择。使用执行后创建 TestNGScenario for each scenario or the cucumber runner way. In either case it should work. You can add reporting dashboard to view reports 的 BDD2 Factory。
选项-1:纯Testng
创建配置文件 运行 作为纯 Testng 测试使用 BDDTestFactory2
:
<suite name="QAF Demo" verbose="0">
<parameter name="step.provider.pkg" value="my.custom.packagename.testing"/>
<parameter name="scenario.file.loc" value="src/test/resources/my/custom/packagename/testing"/>
<parameter name="step.provider.sharedinstance" value="true" />
<test name="BDD Test" enabled="true">
<classes>
<class name="com.qmetry.qaf.automation.step.client.text.BDDTestFactory2"></class>
</classes>
</test>
</suite>
使用此配置文件代替您的 运行ner class。在这种情况下,您不需要cucumber-testng依赖性。
选项 2:黄瓜 运行测试用
- 您将需要cucumber-testng依赖性
- 要 运行 使用你的黄瓜 运行 对黄瓜和黄瓜测试使用相同的版本。在您的情况下,cucumber-* 版本需要相同
5.6.0
或 5.4.0
- 更新 运行ner class 如下:
@CucumberOptions(plugin = {"com.qmetry.qaf.automation.cucumber.QAFCucumberPlugin", "pretty", "html:target", "timeline:target"},
/*tags = {"@Ignore"},*/
features = {"src/test/resources/my/custom/packagename/testing"})
public class RunnerTest extends AbstractTestNGCucumberTests {
@Autowired
private ObjectMapper objectMapper;
@Test(groups = "cucumber", description = "Runs Cucumber Scenarios", dataProvider = "scenarios")
public void runScenario(PickleWrapper pickleWrapper, FeatureWrapper featureWrapper) throws Throwable {
super.runScenario(pickleWrapper,featureWrapper);
}
@Override
@DataProvider(parallel = true, name = "scenarios")
public Object[][] scenarios() {
return super.scenarios();
}
@PostConstruct
public void setUp() {
objectMapper.registerModule(new JavaTimeModule());
}
}
运行 作为 TestNG。
我有一个使用 spring boot 和 testng 的 cucumber 项目。
这里是校长 classes
@SpringBootTest
public class CucumberTestDefinitions extends FunctionalTesting {
@Given("Something")
public void smthg(){
}
}
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = FunctionalTestingApp.class, initializers =
ConfigFileApplicationContextInitializer.class)
public class FunctionalTesting {
@Autowired
protected FunctionalTestingConfiguration configuration;
}
@EnableConfigurationProperties(value = {FunctionalTestingConfiguration.class})
public class FunctionalTestingApp {
}
@EnableAutoConfiguration
@ConfigurationProperties(prefix = "testing")
@Data
public class FunctionalTestingConfiguration {
// the config from yml file
}
我没有 testng.xml 文件,因为测试是用 spring-boot 启动的。 出于某些原因,我想对测试进行优先排序,我发现我可以使用 QAF (https://qmetry.github.io/qaf/latest/scenario-meta-data.html#pre-defined-meta-data-for-bdd)。 我尝试使用它,但没有用。
这是我所做的:
我添加了对pom.xml的依赖(我用的是cucumber 5)
我将此注释 @QAFTestStepProvider
添加到 CucumberTestDefinitions class
我在 RunnerTest class
中添加了这个插件"com.qmetry.qaf.automation.cucumber.QAFCucumberPlugin"
错误在这里
java.lang.NoSuchMethodError: 'java.lang.reflect.Method com.qmetry.qaf.automation.step.client.TestNGScenario.getMethod()'
我尝试在配置文件夹中添加一个 testng.xml 文件,但它没有帮助
QAF spring-boot 兼容吗?
非常感谢您的帮助
编辑
pom.xml :
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.qmetry</groupId>
<artifactId>qaf-cucumber</artifactId>
<version>3.0.0</version>
</dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
奔跑者class
features = {"src/test/resources/toto"})
public class RunnerTest extends AbstractTestNGCucumberTests {
@Autowired
private ObjectMapper objectMapper;
@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
@PostConstruct
public void setUp() {
objectMapper.registerModule(new JavaTimeModule());
}
}
错误信息
java.lang.NoSuchMethodError: 'java.lang.reflect.Method com.qmetry.qaf.automation.step.client.TestNGScenario.getMethod()'
at com.qmetry.qaf.automation.step.client.TestNGScenario.init(TestNGScenario.java:92)
at com.qmetry.qaf.automation.step.client.TestNGScenario.<init>(TestNGScenario.java:70)
at com.qmetry.qaf.automation.step.client.TestNGScenario.<init>(TestNGScenario.java:64)
at com.qmetry.qaf.automation.testng.TestRunnerFactory.convert(TestRunnerFactory.java:76)
at com.qmetry.qaf.automation.testng.TestRunnerFactory.init(TestRunnerFactory.java:67)
at com.qmetry.qaf.automation.testng.TestRunnerFactory.newTestRunner(TestRunnerFactory.java:63)
at org.testng.ITestRunnerFactory.newTestRunner(ITestRunnerFactory.java:55)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:676)
at org.testng.SuiteRunner.init(SuiteRunner.java:178)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:112)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1275)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1251)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1100)
at org.testng.TestNG.runSuites(TestNG.java:1039)
at org.testng.TestNG.run(TestNG.java:1007)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)
不幸的是,我无法重现相同的错误并获得有关依赖项的其他错误。但是,要解决您最初的问题“QAF cucumber testNG with spring boot”和“like to priority tests”方面,简短的回答是肯定的。但可能不会同时使用 TESTNG 和 Cucumber 运行ner,特别是优先测试。这是什么意思?
当您使用 Cucumber 建议的 TestNG 方式时,它只会在 TestNG 中创建一个虚拟测试并将场景作为测试数据提供。哪个不能将每个场景都视为独立的 TestNG 测试!所以您将无法从 TestNG 中获得所有好处(例如,优先级)。
但是,QAF+Cucmber+TestNG并用并不代表不能实现或不能使用。当你使用 QAF it has pure TestNG implementation for BDD 时。 QAF 将每个场景视为 TestNG 测试,将带有示例的场景(场景大纲)视为 data-driven 测试。 qaf-cucuber
将允许使用黄瓜步骤,包括您选择的依赖注入,Spring 在您的情况下。您将受益于所有 TestNG 功能,包括并行执行、侦听器和 QAF 的扩展功能,如 meta-data 过滤器、test-data 过滤器、
所以当你想使用 TestNG 时,最好提供 TestNG configuration to run your feature files written in BDD2 或 Gherkin(BDD2 的子集)。您需要做的就是创建以下 XML 文件到 运行 使用 BDD2 或 Gherkin 编写的功能文件。
<suite name="QAF Demo" verbose="0">
<parameter name="step.provider.pkg" value="pkg.from.where.steps.needs.tobe.loaded" />
<parameter name="scenario.file.loc" value="src/test/resources/toto" />
<test name="BDD Test" enabled="true">
<classes>
<class name="com.qmetry.qaf.automation.step.client.text.BDDTestFactory2"></class>
</classes>
</test>
</suite>
注:
将
step.provider.sharedinstance
设置为true
当使用黄瓜步骤实现时使用步骤中使用的 class 个变量。在上面的方式中,因为你没有使用黄瓜运行ner,所以任何黄瓜钩子都不会被执行。当您使用 TestNG 时,它具有更好的侦听器支持,您可以利用它,并且 step listener 您可以将其与 QAF 一起使用。因此,如果您实现了任何挂钩,您可以将其移动到适当的 before/after 侦听器方法中并在配置文件中注册侦听器。
编辑: 我尝试了你分享的测试项目,我 运行 也遇到了不同的问题。最后我能够弄明白如下。
POM 更新:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>test</scope>
</dependency>
你有两个选择。使用执行后创建 TestNGScenario for each scenario or the cucumber runner way. In either case it should work. You can add reporting dashboard to view reports 的 BDD2 Factory。
选项-1:纯Testng
创建配置文件 运行 作为纯 Testng 测试使用 BDDTestFactory2
:
<suite name="QAF Demo" verbose="0">
<parameter name="step.provider.pkg" value="my.custom.packagename.testing"/>
<parameter name="scenario.file.loc" value="src/test/resources/my/custom/packagename/testing"/>
<parameter name="step.provider.sharedinstance" value="true" />
<test name="BDD Test" enabled="true">
<classes>
<class name="com.qmetry.qaf.automation.step.client.text.BDDTestFactory2"></class>
</classes>
</test>
</suite>
使用此配置文件代替您的 运行ner class。在这种情况下,您不需要cucumber-testng依赖性。
选项 2:黄瓜 运行测试用
- 您将需要cucumber-testng依赖性
- 要 运行 使用你的黄瓜 运行 对黄瓜和黄瓜测试使用相同的版本。在您的情况下,cucumber-* 版本需要相同
5.6.0
或5.4.0
- 更新 运行ner class 如下:
@CucumberOptions(plugin = {"com.qmetry.qaf.automation.cucumber.QAFCucumberPlugin", "pretty", "html:target", "timeline:target"},
/*tags = {"@Ignore"},*/
features = {"src/test/resources/my/custom/packagename/testing"})
public class RunnerTest extends AbstractTestNGCucumberTests {
@Autowired
private ObjectMapper objectMapper;
@Test(groups = "cucumber", description = "Runs Cucumber Scenarios", dataProvider = "scenarios")
public void runScenario(PickleWrapper pickleWrapper, FeatureWrapper featureWrapper) throws Throwable {
super.runScenario(pickleWrapper,featureWrapper);
}
@Override
@DataProvider(parallel = true, name = "scenarios")
public Object[][] scenarios() {
return super.scenarios();
}
@PostConstruct
public void setUp() {
objectMapper.registerModule(new JavaTimeModule());
}
}
运行 作为 TestNG。