Overview
In this tutorial, we will "create" some UI tests for an Android application using Espresso testing library.
Tests can be written either in Java or Kotlin.
Requirements
- gradle 5.6
- android SDK (e.g. v28)
- JDK
Description
For this tutorial, we'll use a sample Java maven-based project provided by the Gauge team.
The project contains several tests, as part of distinct specifications.
Next, you may see an example of the PlaceOrder.spec
file, which contains two scenarios (i.e. tests): "Buy a book" and "Cart retains items until order is placed".
/* * Copyright 2018, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.testing.espresso.BasicSample import androidx.test.ext.junit.rules.activityScenarioRule import android.app.Activity import androidx.test.core.app.ActivityScenario import androidx.test.core.app.launchActivity import androidx.test.espresso.Espresso.onView import androidx.test.espresso.action.ViewActions import androidx.test.espresso.action.ViewActions.* import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.matcher.ViewMatchers import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.LargeTest import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith /** * The kotlin equivalent to the ChangeTextBehaviorTest, that * showcases simple view matchers and actions like [ViewMatchers.withId], * [ViewActions.click] and [ViewActions.typeText], and ActivityScenarioRule * * * Note that there is no need to tell Espresso that a view is in a different [Activity]. */ @RunWith(AndroidJUnit4::class) @LargeTest class ChangeTextBehaviorKtTest { /** * Use [ActivityScenarioRule] to create and launch the activity under test before each test, * and close it after each test. This is a replacement for * [androidx.test.rule.ActivityTestRule]. */ @get:Rule var activityScenarioRule = activityScenarioRule<MainActivity>() @Test fun changeText_sameActivity() { // Type text and then press the button. onView(withId(R.id.editTextUserInput)) .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()) onView(withId(R.id.changeTextBt)).perform(click()) // Check that the text was changed. onView(withId(R.id.textToBeChanged)).check(matches(withText(STRING_TO_BE_TYPED))) } @Test fun changeText_newActivity() { // Type text and then press the button. onView(withId(R.id.editTextUserInput)).perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()) onView(withId(R.id.activityChangeTextBtn)).perform(click()) // This view is in a different Activity, no need to tell Espresso. onView(withId(R.id.show_text_view)).check(matches(withText(STRING_TO_BE_TYPED))) } companion object { val STRING_TO_BE_TYPED = "Espresso" } }
Tests can be run from within Android Studio; however, if you decide to manually export the results to an XML file, that format is not a JUnit XML compatible one in is thus not supported.
To run the tests, we can simply invoke gradle/gradlew. Test
./gradlew app:connectedCheck # ...or... # gradle clean cleanTest test
After running the tests and generating the JUnit XML report (e.g., result.xml), it can be imported to Xray (either by the REST API or through Import Execution Results action within the Test Execution).
curl -H "Content-Type: multipart/form-data" -u admin:admin -F "file=@./app/build/test-results/testReleaseUnitTest/TEST-com.example.android.testing.espresso.BasicSample.ChangeTextBehaviorLocalTest.xml" http://jiraserver.example.com/rest/raven/1.0/import/execution/junit?projectKey=CALC
A Test Execution will be created containing information about the executed scenarios.
Each scenario is mapped to a Generic Test in Jira, and the Generic Test Definition field contains the name of the specification concatenated with the scenario name.
The Execution Details of the Generic Test contains information about the "Test Suite" (as per JUnit format), which in this case corresponds to the scenario name with a prefix.
Notes
,..
References
- https://developer.android.com/training/testing/espresso
- https://developer.android.com/training/testing
- https://developer.android.com/studio/test
- https://developer.android.com/kotlin