• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

benwilcock/android-alltest-gradle-sample: A template Gradle / Android project wh ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

benwilcock/android-alltest-gradle-sample

开源软件地址(OpenSource Url):

https://github.com/benwilcock/android-alltest-gradle-sample

开源编程语言(OpenSource Language):

Java 100.0%

开源软件介绍(OpenSource Introduction):

#Android All-test Gradle Sample

This project provides a Gradle project template for an Android project which integrates and configures testing using Robolectric, Robotium, Java JUnit Tests and Android Instrumentation tests together in one project.

Background

I develop the TripComputer Android App but I find testing it using the standard Instrumentation framework is really slow because it demands either a connected device or an emulator to be running upon which it runs the tests.

Slow testing cycles are a disincentive to TDD, so most tutorials bestow the virtues of switching to the Robolectric framework when unit testing Android apps because it 'simulates' the android SDK in a standard Java VM. This allows your Android tests to be executed outside of the emulator/device. It also allows the use of JUnit v4 syntax rather than the older JUnit v3 required by the built in Android Instrumentation testing framework.

However, there's a problem - it's tricky to get this stuff to work in Android Studio.

I've been an Android Studio user ever since it first went public nearly 2 years ago. It's an awesome IDE but one consequence of it's use is that it promotes the Gradle build system to be the default choice for Android projects. This is good news for Android developers but unfortunately, getting Android Studio, Gradle, Robolectric and JUnit to all work happily together is a real pain in the rear.

Over the past year or so it's been a slowly improving picture, but now Android Studio has gone to a 1.0 release, I (and many others) have figured the time was right to try and bring these tools together.

The android-alltest-gradle-sample project on GitHub is my attempt to create a template project that can be used as a starting point for anyone who wishes to use these best of breed Android Testing tools together with Gradle and Android Studio in one project.

To use the sample, simply clone the repository (or download a ZIP), import into Android Studio, test it and then start running code. Check out the Acknowledgements section in the readme for further help, tips and advice (including how to execute your Robolectric tests from within Android Studio as well as from the cmdline).

Project Structure

Project is broken down into 3 modules

  1. /core - Vanilla Java library module (with JUnit 4 tests)
  2. /app - Android app and integration test module (Robotium and Instrumentation)
  3. /app-tests - Android unit test module (Robolectric)

Dependencies & Frameworks

This project template integrates and depends on the following popular Java and Android frameworks:

  1. AssertJ for Android. Used to make the testing of android components easier by introducing an android specific DSL. See https://github.com/square/assertj-android

  2. Robolectric. Used for the simulated testing of Android apps (i.e. device API's are simulated, so there is no need for an emulator or physical device). See https://github.com/robolectric/robolectric

  3. JUnit. Used to simplify testing of core Java and simulated Android tests. See http://junit.org/

  4. AppCompat. Popular google support library for backwards compatibility. See https://developer.android.com/tools/support-library/features.html

  5. Robotium. Used to augment normal Instrumentation Tests and provide black box integration testing. https://code.google.com/p/robotium/

Running Tests

Tests can be run using one of the following (or combinations thereof):

./gradlew clean build connectedCheck

Known Issues

Robolectric isn't fully suporting AppCompat v21 yet, and strange random failures can affect the smooth running of the build if you choose to 'clean' it. There is more information on Stackoverflow here. The suggested workaround is to separate the gradle commands as follows...

./gradlew clean
./gradlew assemble
./gradlew test
./gradlew connectedCheck

Cautions

This stuff is brittle! It's a very volatile set of tools we're using.

  1. Android Studio is only just out of beta.
  2. Android build tools and Gradle plugin is only just out of beta.
  3. Android app development borrows the Java language syntax, but not the runtime, so the compile & build tools are complex compared to standard Java. Kind of reminds me of the early days of JEE...
  4. Gradle is still relatively new to me, so probably this project can be much improved.
  5. Testing with standard Android is way harder and clunkier than it should be IMHO.
  6. It's very slow (on my Core 2 duo anyway, your mileage may vary).
  7. I'm not what you'd call a 'natural born tester' (but I do at least try).

I'm not sure anyone other than Google can improve the situation regarding Android testing if I'm brutally honest. It's a bit of a 'dogs dinner'.

On the plus side, I haven't used any additional gradle plugins. I've seen and tried lots but they can go out of date quite quickly.

Acknowledgements

The original inspiration for this template project came from Jason Atwood's blog at Big Nerd Ranch:

http://www.bignerdranch.com/blog/all-in-together-android-studio-gradle-and-robolectric/

This template project is based on the following blog post and code samples (but I further further adapted it soa as to include instrumentation and a java core module)

http://blog.blundell-apps.com/android-gradle-app-with-robolectric-junit-tests/

The tests for Robolectric can also be configured to run from within Android Studio 1.0.x if you follow this post:

http://blog.blundell-apps.com/how-to-run-robolectric-junit-tests-in-android-studio/

I had lots of issues with AppCompat which took a while to fix, but @sjorsgeelen and @loeschg had the right answer here:

robolectric/robolectric#1334

About the Author

Ben Wilcock is the developer of TripComputer , the only distance tracking app for Android with a battery-saving LOW POWER mode. It’s perfect for cyclists, runners, walkers, hand-gliders, pilots and drivers. It’s free! Download it from the Google Play Store now:-

Get Trip Computer on Google Play

You can follow Ben on his Blog, Website, Twitter or LinkedIn




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap