从主要方法运行黄瓜时,可以了解我的特征

时间:2018-09-24 07:43:02

标签: selenium cucumber cucumber-java cucumber-junit

按照下面的屏幕快照从主类运行黄瓜测试运行器时,只是给我提供了定义步骤的功能框架。而我已经编写了实现这些步骤的代码。

附加屏幕截图以了解我的代码以及项目结构。 Mainjarvish.java是我尝试运行TestRunner.java的主文件。

main method

1 个答案:

答案 0 :(得分:0)

失败,因为您的类test/java/stepDefinitions/Contactssrc/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)