按照下面的屏幕快照从主类运行黄瓜测试运行器时,只是给我提供了定义步骤的功能框架。而我已经编写了实现这些步骤的代码。
附加屏幕截图以了解我的代码以及项目结构。
Mainjarvish.java
是我尝试运行TestRunner.java
的主文件。
答案 0 :(得分:0)
失败,因为您的类test/java/stepDefinitions/Contacts
与src/main/java/MainJarvish
不在同一类路径范围内。
假设以下结构。
src/main/java/MainJarvish.java
src/test/java/stepDefinitions/Contacts.java
src/test/java/FeatureList/Smoke.feature
MainJarvish.java
import cucumber.api.cli.Main;
public class MainJarvish {
public static void main(String[] args) throws Exception {
String[] options = {"--glue", "stepDefinitions",
"src/test/java/FeatureList/Smoke.feature"};
byte exitcode = Main.run(options, Thread.currentThread().getContextClassLoader());
System.out.println("exitcode = " + exitcode);
}
}
Contacts.java
package stepDefinitions;
import cucumber.api.java.en.Given;
public class Contacts {
@Given("^Submit message for execution$")
public void submitMessageForExecution() throws Throwable {
System.out.println("submitMessageForExecution");
}
}
烟雾功能
Feature: example feature
Scenario: describe the test scenario
Given Submit message for execution
编译类
mvn clean compile test-compile
它生成class
个文件
target/classes/MainJarvish.class
target/test-classes/stepDefinitions/Contacts.class
现在执行ManJarvish
mvn exec:java -Dexec.mainClass=MainJarvish
类Contacts
不在类路径上,因此找不到步骤定义。
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli)
...
Undefined scenarios:
src/test/java/FeatureList/Smoke.feature:3 # describe the test scenario
1 Scenarios (1 undefined)
1 Steps (1 undefined)
由于MainJarvish
在执行时位于src/java
中,因此仅将target/classes
下的项目类添加到类路径中。
如果类Contacts
位于类路径上,例如通过更改类路径范围
mvn exec:java -Dexec.mainClass=MainJarvish -Dexec.classpathScope="test"
现在将target/classes
之下和target/test-classes
之下的类添加到类路径中,并找到步骤定义。
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli)
submitMessageForExecution
...
1 Scenarios (1 passed)
1 Steps (1 passed)
根据您要实现的目标,您可以考虑将MainJarvish
移至test/java
或创建测试运行程序类。
src / test / java / TestRunner.java
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
glue = "stepDefinitions",
features="src/test/java/FeatureList/Smoke.feature"
)
public class TestRunner {
}
然后您可以运行功能测试
mvn clean test
输出
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestRunner
submitMessageForExecution
1 Scenarios (1 passed)
1 Steps (1 passed)