I have some very granular test methods and it would be a total waste of time to start the Activity under test each time. For this it would be very handy to just start it once. Unfortunately Espresso/JUnit finishes all Activities after each test method. Is there a simple way around this?
There is no way to do that. Espresso test cases start activity for test. Espresso is designed like that.
If you want to test specific scenarios you can have separate methods which you can used it in different tests.
For more info :
Why does Espresso leave the app after the test has finished? How to stop it from doing that
I use Espresso to test a lot of activities and transitions between them. I want to write different test for those activities and link the Activity Intents to the corresponding test cases.
1 . Activity A (testActivityA runs)
2. ActivityA launches ActivityB (testActivity B) should be called
is it possible to do this with espresso or any other test framework?
I have troubles with my Robolectric unit test.
I can assert without problem that a click started a new activity when the listener uses the method startActivity( Intent )
But it seems Robolectric has trouble when a new activity is started with the method startActivityForResult(Intent, int) : putting some breaks in the code made me figure out that the activity wasn't started ( and just changing for method startActivity( Intent ) made the assert pass).
Is that normal ? It's a pity since the first activity of my app uses startActivityForResult(Intent, int).
Did someone manage to make tests with this way of launching activities ?
Thanks for your help ..
The short Answer to your question is that, due to the way Robolectric converts Android classes to code that executes in the JVM, a lot of their functionality doesn't behave as you'd expect. Many system callbacks won't execute, and you'll have to rely on what Robolectric provides in their Shadow implementation of classes. (See the link provided by #Steven_BDawg).
The long answer: It may be possible to implement this whole flow in one big test, but it's not what the framework is designed for.
Robolectric and Unit Testing in general aren't meant to be used in the way that you describe. The Unit Testing page on wikipedia states that one can view a unit as the smallest testable part of an application. A unit testing suite should contain many lightweight tests, where each test isolates a bit of functionality in your app and ensure it's working properly.
Consider a basic Application that contains two Activities, A and B. Activity A displays some information about a topic, and Activity B allows the user to select which topic to show in A. When the user moves from Activity A to Activity B, B gets called with startActivityForResult() and should return to A with the selected topic.
Now say we want to Unit Test this flow of A getting the result from B and displaying the data. We can break this up into two tests:
Activity Under Test - Activity A. In our test, we'll create a new instance of Activity A. In the Robolectric Test, we create the Intent that we expect B to return to A, and call the shadow method receiveResult() for A, filling out the arguments with a result code of OK and this Intent. After receiveResult(), run your assertions. You now know that Activity A handles the result properly!
Activity Under Test - Activity B. In our test, we'll create a new instance of Activity B, setting it up as if it were started for result from Activity A. In the Robolectric Test, we'll perform all actions needed to select the data, create the intent we'll send back, then run assertions on the intent to ensure it was created correctly.
This is a very simple example. These two steps could probably be broken out into many more tests, as, again, each unit test should only be testing the smallest unit of functionality that your app can be broken into. The example is mainly to help you start thinking in a unit testing kind of way. I've found that as my understand of unit testing deepens, the way I write code has changed. I try to avoid writing methods and classes in such a way that they do too much work and cannot be properly unit tested. As a rule of thumb, code that's easy to unit test performs very specific operations which are readily apparent when reading the code for the first time.
Finally, if you want to take this a step further, mocking frameworks can greatly aid your ability to Unit Test. Mockito is a mocking framework I've had success with in the past. The purpose of a mocking framework is to create stub Objects whose behavior you tightly control. Mockito (or any other Mocking Framework), will allow you to define an object that extends from any type you need and only implement the methods you need. You'll be able to directly control the response to any of these method invocations. This aids Unit Testing because the only real object that you'll need is the Object Under Test; by Mocking all other objects, you'll have a better sense of whether or not the Object Under Test is behaving properly because all other behavior is explicitly defined by you, the tester. (And yes, this does lead to lots of extra code, but such is the life of a good unit tester. However, as previously stated, as you get more comfortable with unit testing, you may find yourself writing methods that require less mocking and are more conducive to writing tests. Some coders will even write their unit tests BEFORE they code, in order to keep their code tight and focused on a single purpose)
Hope this helps!
I did some Google'ing for you. I don't know if this will definitely help you or not, but I think it is a good start!
Roboelectric: Testing startActivityForResult() and onActivityResult()
I am new for Junit testing framework. Is it possible to test a flow of activities with assertions to check if each is launched using only exisiting classes provided in android API.
It seems we can only test at activity level. But can we automate a flow : like spawning front and back between activities ?
Yes, through the use of Android instrumentation, which provides you with methods that put you into the drivers seat when interacting with activities as the system would through it's instrumentation framework.
Basically, your able to trigger and manage every aspect of an activities life cycle, including introspecting results from activities calling each other. So if you've worked with the ActivityTestCase that's suitable when testing a single activity, you can have a look at the InstrumentationTestCase that provides you with a low-level approach to work with multiple activities at once.
i have a main activity and when tap on the button in the main activity it moves to the another activity than runs on another process .. is there a chance to track/get that activity using robotium /by means of other codes to integrate with robotium
Unfortunately no. Robotium builds upon Android's instrumentation objects which are limited to work only within the process of the instrumentation for the given target Activity (as you define a target process in the test-manifest file).
I don't know how it behaves in cases where the target process has a defined shared user id, though...