Using Instrumentation Testing to Validate Layout on Android

Asked

Viewed 29 times

-1

People,

Before asking my question, below is an example of instrumentation code (incomplete purposely) on Android:

import android.content.Intent
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.ActivityScenario.launch
import androidx.test.core.app.ApplicationProvider.getApplicationContext
import androidx.test.ext.junit.runners.AndroidJUnit4
import br.com.topoil.account.LoginActivity
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {

    @Test
    fun loadActivity() {

        val i = Intent(getApplicationContext(), LoginActivity::class.java)

        var scenario = launch<LoginActivity>(i)
        scenario.moveToState(Lifecycle.State.CREATED)
    }
}

Suppose you put a breakpoint right on this line:

scenario.moveToState(Lifecycle.State.CREATED)

The behavior of this test will be as follows: Loginactivity will appear pretty on Android! And that’s exactly what I wanted, validate the layout. For this the test cannot "end" the life cycle, enter @After etc.

It’s clear that it breaks a branch and helps a lot to really see how Activity will be in the initial state (in this example) right after writing the Activity code and testing on a real device.

Since the instrumentation test has a defined life cycle: Before, Test and After, if you obviously don’t put the breakpoint right there and debug, Activity appears and disappears immediately (which is exactly what happens - and you’re right).

So the question is: Is there a way to test the layouts more efficiently in Android Studio and run a "mocked" Activity so that it enters an initial state and does not disappear, without the need to write an instrumentation test?

Personal thank you!

1 answer

1


Matthew speaks, my beauty?

To answer your question, there is yes! And it is more or less what you imagined.
The two best known forms are:

The description of Activityscenariorule apparently fits you best:

Activityscenariorule launches a Given Activity before the test Starts and closes after the test.

The doc is well completed and is easy to implement.

Aah, and you can also use the Barista, which is an espresso wrapper. So you will be able to do various validations with just a few.

I hope I helped ;) Hugs.

At this link you can see the implementation of Activity scenario Rule.

@RunWith(AndroidJUnit4::class)
class MyTestSuite {

        @get:Rule var activityScenarioRule = activityScenarioRule<MyActivity>()
    
        @Test fun testEvent() {
            val scenario = activityScenarioRule.scenario
            onView(withId(R.id.finish_button)).perform(click())
        }
    }
  • Thank you!!! : -D I will look calmly! Anyway, the "noodle" way quoted above helped me for a few times kakakakak. Of course, it’s not ideal, but boy I searched it huh! I’ll take a look at the Barista too! : -D Thanks for the tip my friend! great week for you!

  • Come on, thank you. Have this nice tutorial on the Docs, teaching how to implement: https://developer.android.com/guide/components/activities/testing

Browser other questions tagged

You are not signed in. Login or sign up in order to post.