Coder Social home page Coder Social logo

oturgjold's Introduction

Modified Deckard (for Gradle)

Deckard is the simplest possible Android project that uses Robolectric for testing and Gradle to build. It has one Activity, a single Robolectric test of that Activity, and an Espresso test of that Activity.

I took Deckard which many people are familiar with and modified it to demonstrate the ideas expressed in https://engineering.aweber.com/avoiding-robolectric-shock-while-testing-android-with-gradle-and-espresso/

With just a bit of manual intervention, this project also imports into Android Studio. See below for instructions.

Setup

Note: These instructions assume you have a Java 1.6 JDK installed.

To start a new Android project:

  1. Install the Android SDK.

  2. Set your ANDROID_HOME environment variable appropriately.

  3. Run the Android SDK GUI and install API 18 and any other APIs you might need. You can start the GUI by invoking android

  4. Download this project from GitHub.

  5. In the project directory you should be able to run the Robolectric tests:

    cd my-new-project
    ./gradlew clean test
  6. You should also be able to run the Espresso tests: ./gradlew clean connectedAndroidTest. Note: Make sure to start an Emulator or connect a device first so the test has something to connect to.

  7. Change the names of things from 'Deckard' to whatever is appropriate for your project. Package name, classes, and the AndroidManifest are good places to start.

  8. Build an app. Win.

IntelliJ / Android Studio Support

Compatibility

Currently this has been tested with Android Studio 0.9.3.

Importing

Import the project into Android Studio by selecting 'Import Project' and selecting the project's build.gradle. When prompted, you can just pick the default gradle wrapper.

Running the Robolectric Test

  1. Go to your Run/Debug configurations screen. Click the 'app' drop down and select Edit Configurations...
  2. Add a new Gradle configuration. Click on the + and select Gradle
  3. Call this configuration 'Build test classes'
  4. In the section titled Gradle project: select the build.gradle in your robolectric-tests subdirectory
  5. Set Tasks: to testClasses
  6. Click 'Apply'
  7. Add a new JUnit configuration
  8. Make the Gradle configuration a dependency of this configuration. Under Before launch: Make click the + sign, 'Run Another Configuration' and choose 'Build test classes'
  9. Name this configuration 'Run Robolectric Tests'
  10. Change Test kind: to 'All in package'
  11. Change Search for tests: to 'In single module'
  12. Change Use classpath of module: to 'robolectric-tests'
  13. Click 'Apply'
  14. Click 'Ok'

You should now be able to DeckardActivityRobolectricTest. Run it as a normal JUnit test - make sure to choose the JUnit test runner and not the Android one.

Running the Espresso Test

To run the Espresso test, you need to set up a Run Configuration. Go to Edit Configurations -> Defaults -> Android Tests and, after choosing the correct module (app), fill in the Specific instrumentation test runner field. The easiest way is to click the ellipsis button on the right and type in GITR. This will find GoogleInstrumentationTestRunner, which is what you want. The fully-qualified class name will appear. Now you can right click on the test method in DeckardEspressoTest and choose the Android test runner.

oturgjold's People

Contributors

avh4 avatar aziemski avatar briangriffey avatar christiankatzmann avatar chuckjhardy avatar coreydowning avatar cyrret avatar eggertj12 avatar erd avatar finishingmove avatar jdonmoyer avatar mag avatar marcors avatar peterzainzinger avatar seadowg avatar skyisle avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.