cucumber / cucumber-android

Android support for Cucumber-JVM
MIT License
129 stars 62 forks source link

Build

Cucumber-Android

This project implements Android support for Cucumber-JVM. It allows running cucumber tests with Android Test Orchestrator and using sharding.

NOTE: Although minSdkVersion for cucumber-android is 14 it requires Java 8 language features and minimum Android API level 26. This is done purposely to allow using cucumber in apps with lower minSdk (to avoid compile errors) but tests should be run on devices with API >= 26. However with desugaring enabled it may work in some configurations on lower API levels assuming that desugaring covers all the Java 8 api. Not all features from cucumber-jvm are supported in cucumber-android due to differences in Android vs JDK (especially junit and html plugins which requires xml factory classes not available in Android)

Developers

Prerequisites

This is ordinary multimodule Android project

Building

./gradlew assemble

Setting up the dependency

The first step is to include cucumber-android into your project, for example, as a Gradle androidTestImplementation dependency:

androidTestImplementation "io.cucumber:cucumber-android:$cucumberVersion"

Using Cucumber-Android

  1. Create a class in your test package (usually <package name from AndroidManifest>.test and add @CucumberOptions annotation to that class. This class doesn't need to have anything in it, but you can also put some codes in it if you want. The purpose of doing this is to provide cucumber options. A simple example can be found in cukeulator. Or a more complicated example here:

    @CucumberOptions(glue = { "com.mytest.steps" }, tags = "~@wip" , features = { "features" })
    public class MyTests 
    {
    }

    glue is the path to step definitions, format is the path for report outputs, tags is the tags you want cucumber-android to run or not run, features is the path to the feature files.
    You can also use command line to provide these options to cucumber-android. Here is the detailed documentation on how to use command line to provide these options: Command Line Options for Cucumber Android

  2. Write your .feature files under your test project's assets/ folder. If you specify features = "features" like the example above then it's assets/features.

  3. Write your step definitions under the package name specified in glue. For example, if you specified glue = "com.mytest.steps", then create a new package under your src folder named "com.mytest.steps" and put your step definitions under it. Note that all subpackages will also be included, so you can also put in "com.mytest.steps.mycomponent".

  4. Set instrumentation runner to io.cucumber.android.runner.CucumberAndroidJUnitRunner or class that extends it

    android.defaultConfig.testInstrumentationRunner "io.cucumber.android.runner.CucumberAndroidJUnitRunner"

Debugging

Please read the Android documentation on debugging.

Examples

Currently there is one example in subproject cukeulator

To create a virtual device and start an Android emulator:

$ANDROID_HOME/tools/android avd

Junit rules support

Experimental support for Junit rules was added in version 4.9.0. Cucumber works differently than junit - you cannot just add rule to some steps class because during scenario execution many such steps classes can be instantiated. Cucumber has its own Before/After mechanism. If you have just 1 steps class then this could work If you have many steps classes then it is better to separate rule and @Before/@After hooks
from steps classes

To let Cucumber discover that particular class has rules add

@WithJunitRule
class ClassWithRules {
    ...
}

You can specify tag expression like @WithJunitRule("@MyTag") to control for which scenarios this rule should be executed. See compose.feature and ComposeRuleHolder for example

Sharding and running with Android Test Orchestrator

CucumberAndroidJUnitRunner works with Android Test Orchestrator and sharding because it reports tests and classes as feature names and scenario names like My feature#My scenario and is able to parse -e class argument from instrumentation. It also supports multiple names in -e class argument separated by comma. This means that feature and scenario name cannot have comma in it's name because it is reserved for separating multiple names (only if you want to use Orchestrator or in general class argument, for other use cases comma is allowed).

Jetpack Compose rule

@WithJunitRule
class ComposeRuleHolder {

    @get:Rule
    val composeRule = createEmptyComposeRule()
}

then inject this object in steps, e.g. (can be also inject as lateinit var field (depending on injection framework used)

class KotlinSteps(val composeRuleHolder: ComposeRuleHolder, val scenarioHolder: ActivityScenarioHolder):SemanticsNodeInteractionsProvider by composeRuleHolder.composeRule {

    ...

    @Then("^\"([^\"]*)\" text is presented$")
    fun textIsPresented(arg0: String) {
        onNodeWithText(arg0).assertIsDisplayed()
    }
}

Hilt

There are 2 solutions for using Hilt with Cucumber:

1. HiltObjectFactory

Add dependency:

androidTestImplementation "io.cucumber:cucumber-android-hilt:$cucumberVersion"

Don't use any other dependency with ObjectFactory like cucumber-picocontainer

HiltObjectFactory will be automatically used as ObjectFactory.

To inject object managed by Hilt into steps or hook or any other class managed by Cucumber:

@HiltAndroidTest
class KotlinSteps(
    val composeRuleHolder: ComposeRuleHolder,
    val scenarioHolder: ActivityScenarioHolder
):SemanticsNodeInteractionsProvider by composeRuleHolder.composeRule {

    @Inject
    lateinit var greetingService:GreetingService

    @Then("I should see {string} on the display")
    fun I_should_see_s_on_the_display(s: String?) {
       Espresso.onView(withId(R.id.txt_calc_display)).check(ViewAssertions.matches(ViewMatchers.withText(s)))
    }

}

Such class:

Also: after each scenario Hilt will clear all objects and create new ones (even these marked as @Singleton) (like it does for each test class in Junit)

2. @WithJunitRule

Hilt requires to have rule in actual test class (which for cucumber is impossible because there is no such class). To workaround that:

See https://developer.android.com/training/dependency-injection/hilt-testing#multiple-testrules how to use hilt with other rules (like compose rule)

@WithJunitRule(useAsTestClassInDescription = true)
@HiltAndroidTest
class HiltRuleHolder {

    @Rule(order = 0) 
    @JvmField
    val hiltRule = HiltAndroidRule(this)

   //if you need it to be injected   
    @Inject
    lateinit var greetingService: GreetingService

    @Before
    fun init() {
        //if you have anything to inject here and/or used elsewhere in tests    
        hiltRule.inject()
    }

}

then you can inject such class to steps class using Cucumber dependency injector (like picocontainer)

Running scenarios from IDE

There is third-party plugin (not related with Cucumber organisation and this repository) which allows running scenarios directly from Android Studio or Intellij

Cucumber for Kotlin and Android