I 've got a Quiz app using Realm db. Every time the user selects an answer she clicks a button and new text for Question appears. Thats it until she reaches the end where I start a new Activity and display a score based on correct answers.
How should I start/test ( with Espresso I guess ) that activity without having to enter manually every time all the answers and click the button after each answer until I reach the last one?
What I need is to pass some mock data to a variable and make an Intent but I dont know how and cant find anything related with this in Espresso
The activities of testing can be divided into the following basic steps: Planning and Control. Analysis and Design. Implementation and Execution.
Starting activities or services. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent.
unit tests can be written under test java package, and instrumental tests can be written under androidTest package. You can then access UI widgets using Espresso ViewMatchers and then you can apply actions on them using ViewActions . Check documentation for further help. Show activity on this post.
You can launch your next activity with a custom intent like this:
@RunWith(AndroidJUnit4.class) public class NextActivityTest { @Rule public ActivityTestRule<NextActivity> activityRule = new ActivityTestRule<>( NextActivity.class, true, // initialTouchMode false); // launchActivity. False to customize the intent @Test public void intent() { Intent intent = new Intent(); intent.putExtra("your_key", "your_value"); activityRule.launchActivity(intent); // Continue with your test } }
Full example: https://github.com/chiuki/android-test-demo
Blog post: http://blog.sqisland.com/2015/04/espresso-21-activitytestrule.html
For Devs using AndroidX for testing, things are a bit changed.
This is an example UI Test case for testing whether my intended activity opens after clicking on the textview.
import androidx.lifecycle.Lifecycle import androidx.test.core.app.ActivityScenario import androidx.test.espresso.Espresso.onView import androidx.test.espresso.action.ViewActions import androidx.test.espresso.intent.Intents import androidx.test.espresso.intent.Intents.intended import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent import androidx.test.espresso.matcher.ViewMatchers.withId import com.softway.dhananjay.tournamentapp.tournament.TournamentActivity import org.junit.Test class MainActivityTest { @Test fun tournament_activity_starts_onClick_of_textView() { Intents.init() val activityScenario: ActivityScenario<MainActivity> = ActivityScenario.launch(MainActivity::class.java) activityScenario.moveToState(Lifecycle.State.RESUMED) onView(withId(R.id.startTextView)).perform(ViewActions.click()) intended(hasComponent(TournamentActivity::class.java.name)) Intents.release() activityScenario.moveToState(Lifecycle.State.DESTROYED) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With