I have written BDD tests for Android with Cucumber and Gherkin but when I run the test Class “CucumberTestRunner”, it shows that tests 0/0 are done, i.e., it doesn’t see the tests I have written.
My codes:
CucumberTestRunner.java
package com.example.bddtestingwithcucumbergherkinespressoselenium.cucumber;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "classpath:features",
glue = "com.example.bddtestingwithcucumbergherkinespressoselenium.cucumber.steps"
)
public class CucumberTestRunner {
}
ToDoListSteps.java:
package com.example.bddtestingwithcucumbergherkinespressoselenium.cucumber.steps;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.*;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import com.example.bddtestingwithcucumbergherkinespressoselenium.MainActivity;
import com.example.bddtestingwithcucumbergherkinespressoselenium.R;
import org.junit.Rule;
public class ToDoListSteps {
@Rule
public ActivityScenarioRule<MainActivity> activityRule = new ActivityScenarioRule<>(MainActivity.class);
@Given("the app is open")
public void theAppIsOpen() {
System.out.println("Test is running");
// Launch the app using Espresso's Intents
//TODO: wrong: it executes the app every time. must only check app being open and if NOT open, open it.
Intent intent = new Intent(InstrumentationRegistry.getInstrumentation().getTargetContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // Ensure a fresh start
InstrumentationRegistry.getInstrumentation().startActivitySync(intent);
}
@When("I enter {string} as a task")
public void iEnterAsATask(String task) {
onView(ViewMatchers.withId(R.id.task_input)).perform(typeText(task));
}
@And("I tap the "Add" button")
public void iTapTheAddButton() {
onView(withId(R.id.add_button)).perform(click());
}
@Then("the task {string} should be displayed in the list")
public void theTaskShouldBeDisplayedInTheList(String task) {
onView(withText(task)).check(matches(isDisplayed()));
}
}
ToDoList.feature:
Feature: To-Do List Functionality
Scenario: Add a new task
Given the app is open
When I enter "Buy groceries" as a task
And I tap the "Add" button
Then the task "Buy groceries" should be displayed in the list
Scenario: Mark a task as complete
Given the app is open
And the task "Buy groceries" is in the list
When I tap the checkbox next to "Buy groceries"
Then the task "Buy groceries" should be marked as complete
Scenario: Delete a task
Given the app is open
And the task "Buy groceries" is in the list
When I long-press the task "Buy groceries"
And I tap the "Delete" button
Then the task "Buy groceries" should be removed from the list
Scenario: View empty list
Given the app is open
And there are no tasks in the list
Then the message "No tasks yet!" should be displayed
Scenario: Clear all completed tasks
Given the app is open
And the tasks "Buy groceries" and "Walk the dog" are in the list
And "Buy groceries" is marked as complete
When I tap the "Clear Completed" button
Then the task "Buy groceries" should be removed from the list
And the task "Walk the dog" should still be in the list
And the message I have when I run the CucumberTestRunner class : 0/0 tests