From dd9beae7c034df8102664e3b863bc3451c29a89d Mon Sep 17 00:00:00 2001 From: hmalik144 Date: Sat, 25 Feb 2023 15:32:17 +0000 Subject: [PATCH] Android cucumber setup to run a sample feature file --- app/build.gradle | 9 ++++-- .../assets/features/sample.feature | 5 ++++ .../sumtest/ExampleInstrumentedTest.kt | 24 ---------------- .../com/example/sumtest/steps/SampleSteps.kt | 28 +++++++++++++++++++ .../example/sumtest/test/CucumberRunner.kt | 11 ++++++++ build.gradle | 4 +++ 6 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 app/src/androidTest/assets/features/sample.feature delete mode 100644 app/src/androidTest/java/com/example/sumtest/ExampleInstrumentedTest.kt create mode 100644 app/src/androidTest/java/com/example/sumtest/steps/SampleSteps.kt create mode 100644 app/src/androidTest/java/com/example/sumtest/test/CucumberRunner.kt diff --git a/app/build.gradle b/app/build.gradle index 709b046..53fb063 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -14,7 +14,7 @@ android { versionCode 1 versionName "1.0" - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "io.cucumber.android.runner.CucumberAndroidJUnitRunner" } buildTypes { @@ -44,6 +44,9 @@ dependencies { implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1' implementation 'androidx.activity:activity-ktx:1.6.1' testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test.ext:junit:1.1.4' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.5' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + androidTestImplementation 'com.android.support.test:runner:1.0.2' + androidTestImplementation "io.cucumber:cucumber-android:${rootProject.ext.cucumberVersion}" } \ No newline at end of file diff --git a/app/src/androidTest/assets/features/sample.feature b/app/src/androidTest/assets/features/sample.feature new file mode 100644 index 0000000..bc2e3d5 --- /dev/null +++ b/app/src/androidTest/assets/features/sample.feature @@ -0,0 +1,5 @@ +Feature: Sample feature file + + @sample_test + Scenario: Successful login + Given I start the application diff --git a/app/src/androidTest/java/com/example/sumtest/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/example/sumtest/ExampleInstrumentedTest.kt deleted file mode 100644 index b4393ac..0000000 --- a/app/src/androidTest/java/com/example/sumtest/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.example.sumtest - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("com.example.sumtest", appContext.packageName) - } -} \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/sumtest/steps/SampleSteps.kt b/app/src/androidTest/java/com/example/sumtest/steps/SampleSteps.kt new file mode 100644 index 0000000..6b65a58 --- /dev/null +++ b/app/src/androidTest/java/com/example/sumtest/steps/SampleSteps.kt @@ -0,0 +1,28 @@ +package com.example.sumtest.steps + +import androidx.lifecycle.Lifecycle +import androidx.test.core.app.ActivityScenario +import androidx.test.platform.app.InstrumentationRegistry +import com.example.sumtest.SumActivity +import io.cucumber.java.Before +import io.cucumber.java.en.Given +import org.junit.Assert + +class SampleSteps { + + private lateinit var mActivityScenarioRule: ActivityScenario<*> + + @Before + fun setup() { + mActivityScenarioRule = ActivityScenario.launch(SumActivity::class.java) + } + + @Given("I start the application") + fun I_start_the_application() { + mActivityScenarioRule.moveToState(Lifecycle.State.STARTED) + + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + Assert.assertEquals("com.example.sumtest", appContext.packageName) + } + +} \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/sumtest/test/CucumberRunner.kt b/app/src/androidTest/java/com/example/sumtest/test/CucumberRunner.kt new file mode 100644 index 0000000..d0960bc --- /dev/null +++ b/app/src/androidTest/java/com/example/sumtest/test/CucumberRunner.kt @@ -0,0 +1,11 @@ +package com.example.sumtest.test + +import io.cucumber.junit.CucumberOptions + + +@CucumberOptions( + features = ["features"], + tags = ["@sample_test"], + glue = ["com.example.sumtest.steps"] +) +class CucumberRunner \ No newline at end of file diff --git a/build.gradle b/build.gradle index 2536974..bbb6e74 100644 --- a/build.gradle +++ b/build.gradle @@ -3,4 +3,8 @@ plugins { id 'com.android.application' version '7.3.1' apply false id 'com.android.library' version '7.3.1' apply false id 'org.jetbrains.kotlin.android' version '1.7.20' apply false +} + +ext { + cucumberVersion = "4.10.0" } \ No newline at end of file