我正在开发我的第一个功能文件/ selenium项目。
我创建了一个功能文件和跑步者类。
package cucumberpkg2;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions
(features="Features")
public class Runner {
}
Feature: Login screen
Scenario Outline: Successful login
Given User is on Login page
When User enters valid UserName and Password
And Clicks the login button
Then User landed on the home page
但每当我尝试将TestRunner类作为JUnit测试运行时,我都会收到错误:
在所选项目中找不到测试类。
答案 0 :(得分:2)
以下是您的问题的解决方案:
Scenario Outline
文件中将关键字Scenario
更改为test.feature
。TestRunner
类,但您的代码说Runner
类正在实现为public class Runner
,请确保您正在执行哪个类文件为JUnit test
。@CucumberOptions
更改为@Cucumber.Options
(最新版本适用于@CucumberOptions
)@Cucumber.Options
(features="Features")
@Cucumber.Options(features="Features")
确保您的要素文件放在项目目录中的Features
子目录中。因此,您将在test.feature
子目录中拥有Features
文件,其中包含以下代码:
Feature: Login screen
Scenario: Successful login
Given User is on Login page
When User enters valid UserName and Password
And Clicks the login button
Then User landed on the home page
您的Runner
课程将如下所示:
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features="Features")
public class Runner {
}
最后,当您作为JUnit测试执行Runner
类时,您将在控制台上看到以下消息:
You can implement missing steps with the snippets below:
@Given("^User is on Login page$")
public void User_is_on_Login_page() throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
@When("^User enters valid UserName and Password$")
public void User_enters_valid_UserName_and_Password() throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
@When("^Clicks the login button$")
public void Clicks_the_login_button() throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
@Then("^User landed on the home page$")
public void User_landed_on_the_home_page() throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
通过对Cucumber实施glue
选项,可以轻松完成这些警告。
如果这回答你的问题,请告诉我。
答案 1 :(得分:0)
您需要提供功能文件的完整路径,如下所述。
@RunWith(Cucumber.class)
@CucumberOptions(
features = {"src/test/resources/com/gaurang/steps/demo.feature",
"src/test/resources/com/gaurang/steps/demo1.feature"
}
)
public class RunAllTest {
}
或者如果您有太多的功能文件,最好的方法是为功能文件提供标签,然后使用这些标签运行,如下所述。
@userRegistrations
Feature: User Registration
<强> RunAllTest.java 强>
@RunWith(Cucumber.class)
@CucumberOptions(tags={"@userRegistrations"})
public class RunAllTest {
}
您可以使用多个标签