问题
I am working on my first feature file/selenium project.
I have created a feature file and runner class.
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
But whenever I try to run the TestRunner class as a JUnit test, I get the error:
Test class not found in selected project.
回答1:
Here is the solution to your Question:
- As per the current documentation of Cucumber you may require to change the keyword
Scenario OutlinetoScenariointest.featurefile. - Though you mentioned about
TestRunnerclass but your code speaks aboutRunnerclass being implemented aspublic class Runner, be sure which class file are you executing asJUnit test. - You may require to change
@CucumberOptionsto@Cucumber.Optionsfor previous versions (recent versions work with@CucumberOptions) - Put the following 2 parts in a single line as
@Cucumber.Options (features="Features") - As you would be mentioning
@Cucumber.Options(features="Features")ensure that your feature file is placed insideFeaturessub-directory within the Project Directory. So you will be having,
test.featurefile withinFeaturessub-directory with the following code: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 pageYour
Runnerclass will look like:import org.junit.runner.RunWith; import cucumber.api.junit.Cucumber; @RunWith(Cucumber.class) @CucumberOptions(features="Features") public class Runner { }Finally when you will execute
Runnerclass as a JUnit test you will see the following message on your console: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(); }These warnings can be taken care easily by implementing the
glueoptions to Cucumber.
Let me know if this Answers your Question.
回答2:
you need to provide full path of the feature file as mentioned below.
@RunWith(Cucumber.class)
@CucumberOptions(
features = {"src/test/resources/com/gaurang/steps/demo.feature",
"src/test/resources/com/gaurang/steps/demo1.feature"
}
)
public class RunAllTest {
}
or if you have too many feature files the best way is to provide tags to feature file and then use those tags to run as mentioned below .
@userRegistrations
Feature: User Registration
RunAllTest.java
@RunWith(Cucumber.class)
@CucumberOptions(tags={"@userRegistrations"})
public class RunAllTest {
}
And you can use multiple tags
来源:https://stackoverflow.com/questions/43966680/cucumber-test-not-running