gpt4 book ai didi

java - 运行测试时出现 Cucumber 异常

转载 作者:行者123 更新时间:2023-12-02 01:12:29 26 4
gpt4 key购买 nike

我有一个我认为很愚蠢的问题......我无法对 cucumber 进行测试。

返回以下错误:

cucumber.runtime.CucumberException: 

Classes annotated with @RunWith(Cucumber.class) must not define any
Step Definition or Hook methods. Their sole purpose is to serve as
an entry point for JUnit. Step Definitions and Hooks should be defined
in their own classes. This allows them to be reused across features.
Offending class: class Teste.testecucumber

有人可以帮忙吗?

谢谢!!

最佳答案

@Runwith 在 Cucumber 项目的 TestRunner 类中声明。 Cucumber 项目定义了 3 种类型的类:

  1. 步骤定义类
  2. 要素类
  3. 运行类

请查找上述类的以下示例:

<强>1。要素类

测试用例写在这个类中

   Feature: Title of your feature
I want to use this template for my feature file

@tag1
Scenario: Verify login to the system.
Given I navigate to url
And I enter username as "username"
And I enter password as "password"
When I click Login
Then I should be logged into the system

<强>2。步骤定义类

该类中定义了功能步骤

    public class LoginPage(){

@Given("I navigate to the url")
public void navigate() {

/* your code for the above
step comes here*/
}

}

<强>3。运行者类别

运行者类由特征位置和步骤定义组成。它是一个 Junit 类,不能包含 Cucumber 步骤定义注释。 (这就是为什么运行者类不能成为步骤定义类的原因)。但是,您可以在此类中包含 BeforeClass、AfterClass(Junit 注释)

 import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
features = {"classpath:<location of your folder with feature classes / package name>"},
glue = {"<package name>" },
tags = { "<the tags that has to be executed>","can be comma separated multiple tags" }
)



public class testrunner {

//@AfterClass
public static void quitDriver() {
driver.quit();
}
}

希望这对您有帮助!

关于java - 运行测试时出现 Cucumber 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59165748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com