implementation 'com.android.support:appcompat-v7:27.0.0'
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.core:core-ktx:1.0.0'
testing bacame a core function for app development to build a quality software.
Android test require physical device or emulator which make tests take more time so we divide tests into:
Each test is a function with @Test
annotation
we use Assert class
from org.junit.Assert
as a judge to tests
exmaple
class ExampleUnitTest { @Test fun addition_isCorrect() { assertEquals(4, 2 + 2) } }
There are many assert helper methods such as
equals()
functionAlso we should pay attention to test consistency(tests should not effect other tests , i.e each test run run with same state), this done by:
@Before
method which used to setup environment for each test as it run before every test@After
method which runs after each test used to tearDown resources@Test
.@RunWith
ViewMatcher: provide matcher for android views, will be used to specify what view we need to interact with.
withId
, withText
, isDisplayed
, isChecked
, equalTo
, allOf
, anyOf
)ViewActions: used to perform action of a view
click
, typeText
, replaceText
, closeSoftKeyboard
)NOTE: we will need to run activity by using ActivityTestRule
that run activity before each test
ViewAssertions: used to check specific action/view is done example (matches
which accept one of ViewMatchers )
exmaple
@RunWith(AndroidJUnit4ClassRunner::class) class ReminderViewModelTest { @get:Rule val activityRule = ActivityTestRule(MainActivity::class.java) @Test fun getReminders() { val reminderName = "TestAA" // open add activity onView(withId(R.id.fab)).perform(click()) // type text onView(withId(R.id.edReminder)).perform(typeText(reminderName)) // close kb closeSoftKeyboard() // click add button onView(withId(R.id.btnAdd)).perform(click()) // go back pressBack() // make sure note is added onView(withText(reminderName)).check(ViewAssertions.matches(isDisplayed())) } }
It is special case to test data with views that use Adapter pattern such as (ListView
, Spinner
, etc)
code
// first click so spinner open its dropDown list onView(withId(R.id.spinnerView)).perform(click()) // select item onData(allOf(instanceOf(Reminder::class.java) , equalTo(ReminderItem("message")))).perform(click())
import org.hamcrest.Matchers.*
to able to use (allOf
,instanceOf
,equalTo
)some cases with match,assert:
onView(allOf(withId(R.id.fab) , isEnabled())).perform(click())
onView(allOf(withId(R.id.fab) , isEnabled())).check(ViewAssertions.doesNotExist())