Just downloaded Android Studio which is based off of the Intellij Idea.
How would one create tests?
I notice there is a option for create a Test Module but t
Add below lib inside gradle file
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
Create class HomeActivityTest inside androidTest directory and before running the test add flurry_api_key and sender_id string inside string resource file and change the value for failure and success case.
@RunWith(AndroidJUnit4.class)
public class HomeActivityTest
{
private static final String SENDER_ID = "abc";
private static final String RELEASE_FLURRY_API_KEY = "xyz";
@Test
public void gcmRegistrationId_isCorrect() throws Exception
{
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
Assert.assertEquals(SENDER_ID, appContext.getString(R.string.sender_id));
}
@Test
public void flurryApiKey_isCorrect() throws Exception
{
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
Assert.assertEquals(RELEASE_FLURRY_API_KEY, appContext.getString(R.string.flurry_api_key));
}
}
The easiest way I found is the streamlined in my following blog post:
Good luck making your code more readable, maintainable and well-tested!
As of now (studio 0.61) maintaining proper project structure is enough. No need to create separate test project as in eclipse (see below).
Edit: As of 0.1.8 this is now supported in the IDE. Please follow the instructions in there, instead of using the instructions below.
Following the Android Gradle Plugin User Guide I was able to get tests working on the command line by performing the following steps on a newly created project (I used the default 'com.example.myapplication' package):
This ran my tests and placed the test results in MyApplicationProject/MyApplication/build/reports/instrumentTests/connected. I'm new to testing Android apps, but it seem to work fine.
From within the IDE, it's possible to try and run the same test class. You'll need to
However this fails (the classpath used when running the tests is missing the test output directory). However, I'm not sure that this would work regardless as it's my understanding that an Android specific test runner is required.
Android Studio keeps evolving so the responses above will eventually be no longer applicable. For the current version of Android Studio 1.2.1.1, there's a nice tutorial on testing at:
http://evgenii.com/blog/testing-activity-in-android-studio-tutorial-part-1/
Android Studio v.2.3.3
Highlight the code context you want to test, and use the hotkey: CTRL
+SHIFT
+T
Use the dialog interface to complete your setup.
The testing framework is supposed to mirror your project package layout for best results, but you can manually create custom tests, provided you have the correct directory and build settings.