android-studio-testing-project-template


Source link: https://github.com/emmano/android-studio-testing-project-template

android-studio-testing-project-template

The goal of this project is to speed up the setup of Robolectric, Robotuim, Double Espresso, Roboguice, and Mockito (along with some other nice libraries) in Android Studio. Since there is not built in support for Robolectric (just yet, hopefully) there is a lot of manual set up that has to be done. This project uses jeske717's Robolectric-gralde plugin as well as his InjectedTestRunner (more about it later) found on Java Center.

How is this different than the Deckard-Gradle template provided by Robolectric?

Deckard requires you to download their template and then customize things like the name of your project, module name, etc. This template is configured with the information provided when you go through the project creation flow.

Tested on:

  • Mac OSX (Mavericks)
  • Android Studio 0.8.1, 0.8.5 and 1.0

Set Up

  1. Clone the repo
  2. Go to your Android Studio installation fodler, and then to plugins/android/lib/templates/gradle-projects
  3. Create a backup of the NewAndroidProject and NewProjectTemplate folders (just in case you want to revert the changes)
  4. Copy the two folders you just cloned into the plugins/android/lib/templates/gradle-projects
  5. Open Android Studio and create a new project
  6. After build.gradle gets executed by Android Studio, you would get a dialog saying that the *.iml file was updated and the project needs to be reloaded. Click on OK. (if the dialog doesn't show automatically you should manually run the addRobolectricTestSourcesToIml Gradle task (see below on how to run Gradle tasks)).
  7. Upon restart the robolectricTest/java directory should be green. Now you need to run the configureJUnitDefaultToUseRobolectricClasspath Gradle task. To do so, go to View-> Tool Windows->Gradle. Expand the menu that has the name of your app, and double click on the configureJUnitDefaultToUseRobolectricClasspath task. Android studio will prompt the same message as before (requesting to reload the project). Click OK.
  8. After Android Studio reloads, you should syncronize your project. File->Syncrhonize.
  9. Run the MyActivityRobolectricTest.java inside the robolectricTest module. Right click on the method, Run>testMethodName() (the second option on the dropdown; the JUnit one).
  10. Go to Android Studio > Preferences > Compiler > uncheck "Use in-process build"
  11. As part of the Espresso configuration a custom Run Configuration is needed. They show how to do it on their docs here (look at the Android Studio picture). Make sure you check the "Show chooser dialog". It defaults to Emulator.
  12. If everything is set up correclty, the test should run and pass.
  13. Apparently Android Studio 1.0 defaults to ActionBarActivity regardless of your minSdkVersion selection. Make sure your MainActivity extends Activity or that you fix the Instrumentation tests to run on older versions of Android.

InjectedTestRunner

InjectedTestRunner is the "glue" between Mockito and Roboguice. It basically looks for all @Mock on the test and automagically(thanks to jeske717) binds the mock instance to the correct @Inject on the implementation class. For more information go here

Known Limitations (We will try to fix)

  1. You might encounter some problems when trying to add a new package under robolectricTest/java/your.app.package
  2. After you create a new test on a class that already contains tests, sometimes you will need to run the tests twice if you are running all the tests on the class. Synchronizing the project also works. It seems that gralde doesn't pick up the new added test on the first run. It works on the second run. (Step 10 above should fix this)
  3. Make sure you create the project setting KitKat as minSdk to begin with. Tried using a different minSdk during the project creation flow and it broke the template. You can change your minSdk after you follow the steps above.
  4. Specifying to use the GoogleInstrumentationTestRunner in build.gradle, as required by Espresso, makes the whole test suite run. Removing testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" from build.gradle, allows to run Robotium tests independently, but Espresso tests will not run. I will try to find a way to separate the runs.
  5. In order to see the robolectricTest folder, you need to switch Android Studio from the Android View to the Project View

TODO

  1. Be able to run Robotium and Espresso tests independently

Resources

� A polished and flexible ProgressView, fully customizable with animations.
A ContactPicker library for Android, written in pure Kotlin.
Small Android library to show PDF files
A library to dynamically format your EditTexts to take currency inputs
Lightweight Android library for a new way to create toasts in Android, similar to Bootstrap alerts.
for check required text in EditText

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes