Day45 of #100DaysOfCode
Hii folks 🙌
Today I’m going to write down my first instrumentation tests for the android app.
Unit 2: Layouts
Pathway 2: Get user input in an app — Part 2
Source: https://developer.android.com/courses/android-basics-kotlin/course
First of all, we will start by downloading the ZIP file of the GitHub repo, and then opening that repo folder in our Android Studio Code.
After that, we will create the instrumentation test directory. But before that
What is the difference between Unit and Instrumentation Tests
Often Unit tests are referred to as “local tests” or “local unit tests”. The main reason for this seems to be that we want to be able to run tests without a device or an emulator attached.
Unit tests cannot test the UI for our app without mocking objects such as an Activity.
Instrumentation tests run on a device or an emulator. In the background, our app will be installed and then a testing app will also be installed which will control our app, launch it, and run UI tests as needed.
Instrumentation tests can be used to test none UI logic as well. They are especially useful when we need to test code that has a dependency on a context.
Now, we will move forward to writing down our first instrumentation test.
- We will open the file which we just created. It looks like this
package com.example.tiptimeclass CalculatorTests {
}
- Instrumentation tests require an InstrumentationTestRunner, which allows the test to execute on a device or emulator. There are several other instrumentation runners, but for this test, we’ll use the
AndroidJUnit4
test runner. To specify the test runner, we need to annotate the class with the following:
@RunWith(AndroidJUnit4::class)
class CalculatorTests {
}
- Just in case, these are the imports that we need:
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.runner.RunWith
- The Tip Time app consists of a single activity,
MainActivity
. In order to interact with the activity, our test case must first launch it. Add the following inside theCalculatorTests
class.
@get:Rule()
val activity = ActivityScenarioRule(MainActivity::class.java)
- Next, we need to write the test logic itself. So we create a function and call it
calculate_20_percent_tip()
and annotate it with the@Test
annotation.
@Test
fun calculate_20_percent_tip() {
}
We will use Espresso for the instrumentation tests. It is a library that let us interact with the UI components through our code.
- Now we will move towards the implementation part of the test function.
- So, in the
calculate_default_tip()
function in our test class, we can write the test logic.
package com.example.tiptime
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.hamcrest.Matchers.containsString
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class CalculatorTests {
@get:Rule()
val activity = ActivityScenarioRule(MainActivity::class.java)
@Test
fun calculate_default_tip() {
onView(withId(R.id.cost_of_service_edit_text))
.perform(typeText("50.00"))
onView(withId(R.id.calculate_button)).perform(click())
onView(withId(R.id.tip_result))
.check(matches(withText(containsString("10.00"))))
}
}
- Now we can see that the test executes as if someone was interacting with the app itself!
We got our first instrumentation test working. Yay ✅
Today I Learned:
- Android Studio generates the necessary test classes when the project is created. However, if we encounter a project that doesn’t have them, we can create them manually.
- We should test rules run before every test in a test class.
- Espresso is a fundamental component of instrumentation tests. It enables interaction with UI components using code.
That is all for Day45 ✅
Thanks for reading, See you tomorrow!