黄瓜测试没有运行
问题描述:
我正在处理我的第一个功能文件/硒项目。黄瓜测试没有运行
我创建了一个功能文件和跑步者类。
package cucumberpkg2;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions
(features="Features") \t
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测试,我得到的错误:
测试类没有在选定的项目中找到。
答
您需要提供如下所述的功能文件的完整路径。
@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 {
}
你也可以使用多个标签
答
这里是解决你的问题:
- 为每Cucumber当前文档您可能需要更改关键字
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
子目录内。 -
那么你将有内
Features
子目录test.feature
文件用下面的代码: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 { }
-
最后,当你将执行
Runner
类作为JUnit测试,您将在控制台上看到以下消息: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
选项,可以轻松地保护这些警告。
让我知道这个答案是否是您的问题。
您还需要在@CucumberOptions – kushal
@kushal中为特性文件指定glue作为“glue = {”packagename.classname“}”的步骤定义所在的位置。 'glue'选项不是强制性的,用于测试Cucumber项目绑定。但是当你想测试完整的实现时,'glue'是必须的。谢谢 – DebanjanB
@dev,我知道胶水有时候不是强制性的,但我不明白你的声明:/ – kushal