O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

de

Unit testing and Android Slide 1 Unit testing and Android Slide 2 Unit testing and Android Slide 3 Unit testing and Android Slide 4 Unit testing and Android Slide 5 Unit testing and Android Slide 6 Unit testing and Android Slide 7 Unit testing and Android Slide 8 Unit testing and Android Slide 9 Unit testing and Android Slide 10 Unit testing and Android Slide 11 Unit testing and Android Slide 12 Unit testing and Android Slide 13 Unit testing and Android Slide 14 Unit testing and Android Slide 15 Unit testing and Android Slide 16 Unit testing and Android Slide 17 Unit testing and Android Slide 18 Unit testing and Android Slide 19 Unit testing and Android Slide 20 Unit testing and Android Slide 21 Unit testing and Android Slide 22 Unit testing and Android Slide 23 Unit testing and Android Slide 24 Unit testing and Android Slide 25 Unit testing and Android Slide 26 Unit testing and Android Slide 27 Unit testing and Android Slide 28 Unit testing and Android Slide 29 Unit testing and Android Slide 30 Unit testing and Android Slide 31 Unit testing and Android Slide 32 Unit testing and Android Slide 33 Unit testing and Android Slide 34 Unit testing and Android Slide 35 Unit testing and Android Slide 36 Unit testing and Android Slide 37 Unit testing and Android Slide 38 Unit testing and Android Slide 39 Unit testing and Android Slide 40 Unit testing and Android Slide 41 Unit testing and Android Slide 42 Unit testing and Android Slide 43 Unit testing and Android Slide 44 Unit testing and Android Slide 45 Unit testing and Android Slide 46 Unit testing and Android Slide 47 Unit testing and Android Slide 48 Unit testing and Android Slide 49 Unit testing and Android Slide 50 Unit testing and Android Slide 51 Unit testing and Android Slide 52 Unit testing and Android Slide 53 Unit testing and Android Slide 54 Unit testing and Android Slide 55 Unit testing and Android Slide 56 Unit testing and Android Slide 57 Unit testing and Android Slide 58 Unit testing and Android Slide 59 Unit testing and Android Slide 60 Unit testing and Android Slide 61
Próximos SlideShares
Introduction to android testing
Avançar
Transfira para ler offline e ver em ecrã inteiro.

1 gostou

Compartilhar

Baixar para ler offline

Unit testing and Android

Baixar para ler offline

Lecture on Android unit testing and instrumentation testing, mDevCamp 2015.
Overview of available libraries and frameworks, and their capabilities and drawbacks.

Unit testing and Android

  1. 1. Unit Testing & Android Tomáš Kypta
  2. 2. Unit Testing Experience on Android
  3. 3. Android apps are difficult to test
  4. 4. Types of Android tests
  5. 5. Types of Android tests Instrumentation tests Unit tests
  6. 6. Instrumentation tests
  7. 7. Instrumentation tests • running on physical device or emulator • gradle connectedCheck
  8. 8. Intstrumentation tests • Legacy instrumentation tests or • Testing Support Library
  9. 9. Legacy instrumentation tests • JUnit3 • Tests extend from TestCase • AndroidTestCase • ActivityInstrumentationTestCase2 • ServiceTestCase • …
  10. 10. Testing Support Library • AndroidJUnitRunner • JUnit4 compatible android { defaultConfig { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } } androidTestCompile 'com.android.support.test:runner:0.3'
 androidTestCompile 'com.android.support.test:rules:0.3'
  11. 11. Testing Support Library • Test filtering • @RequiresDevice - run on physical device, no emulator • @SdkSupress - don’t run on lower Android API level
  12. 12. Test Sizes • @SmallTest • @MediumTest • @LargeTest
  13. 13. Test Sizes Feature Small Medium Large Network access No localhost only Yes Database No Yes Yes File system access No Yes Yes Use external systems No Discouraged Yes Multiple threads No Yes Yes Sleep statements No Yes Yes System properties No Yes Yes Time limit (seconds) 60 300 900+
  14. 14. Potential problems • Manifest merger • problems when testing libraries • aar dependency using manifest placeholders • e.g. ${applicationId}, ${localApplicationId} android { defaultConfig { manifestPlaceholders = [localApplicationId:”com.example.mylib”] } }
  15. 15. Problems • It affects the device! • Wiping contacts will wipe contacts! • You can’t prepare all test preconditions • e.g. you can’t dynamically change permissions
  16. 16. Problems Test failed to run to completion. Reason: 'Instrumentation run failed due to 'java.lang.IllegalStateException''. Check device logcat for details
  17. 17. • framework for functional UI tests • part of Android Testing Support Library androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
  18. 18. @Test public void sayHello(){ onView(withId(R.id.editText)) .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); onView(withText("Say hello!”)) .perform(click()); String expectedText = "Hello, " + STRING_TO_BE_TYPED + "!"; onView(withId(R.id.textView)) .check(matches(withText(expectedText))); }
  19. 19. Problems • testing on device is not isolated • device state affects the result • e.g. screen on/off might affect test result onView(withId(R.id.my_view)) .check(matches(isDisplayed()));
  20. 20. Instrumentation tests are SLOOOOOW
  21. 21. java.lang.RuntimeException: Stub!
  22. 22. Unit Tests
  23. 23. Unit Tests • run on JVM • mockable android.jar • gradle test
  24. 24. Grade and Android Studio support • natively supported since Gradle plugin 1.1.0 • big problems in AS in previous versions • has issues • switching between unit & instrumentation tests • disabled type is not indexed
  25. 25. • the essential piece • alone can be used only for pure Java • don’t use on Android APIs!
  26. 26. Method ... not mocked. android {
 testOptions {
 unitTests.returnDefaultValues = true
 }
 } • Helps rarely • returns 0, false, null, …
  27. 27. • mocking framework • easy to use • compatible with Android unit testing testCompile ‘org.mockito:mockito-core:1.10.19'
  28. 28. • can be used in instrumentation tests • needs dexmaker androidTestCompile ‘org.mockito:mockito-core:1.10.19' androidTestCompile "com.google.dexmaker:dexmaker:1.2" androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
  29. 29. • @RunWith(MockitoJUnitRunner.class) • @Mock • Mockito.mock() • Mockito.when().thenReturn() • when(obj.getA()).thenReturn(aInstance)
  30. 30. • Mockito.spy() • wrapping a real object • Mockito.verify() • verify that special condition are met • e.g. method called, called twice, …
  31. 31. Limitations • final classes • anonymous classes • primitive types • static methods
  32. 32. • at first, might be difficult to use • the ultimate mock of Android APIs • allows custom shadows • @RunWith(RobolectricTestRunner.class)
  33. 33. • supports Android API level 18 • doesn’t support multiple users on the same machine!!! 2.4
  34. 34. • Robolectric class splits into • Robolectric • RuntimeEnvironment • Shadows • ShadowApplication • ShadowLooper 3.0-rc3
  35. 35. that’s the s**t you need for Android unit testing
  36. 36. Potential problems • difficult to search for solutions • long history of bigger changes • many obsolete posts
  37. 37. Potential problems • difficulties running on command line and in AS • different paths • difficulty working with resources • RobolectricGradleTestRunner • doesn’t work in AS
  38. 38. Code Coverage
  39. 39. Code Coverage • unit tests • JaCoCo • instrumentation tests • EMMA • obsolete • Google is supposedly working on JaCoCo support
  40. 40. JaCoCo
  41. 41. JaCoCo • enabled by default for unit tests • generates binary report in build/jacoco • build/jacoco/testDebugUnitTest.exec • gradle test
  42. 42. Code Coverage • don’t use it • generates coverage-instrumented-classes • for instrumentation tests • coverage for instrumentation tests is not ready buildTypes {
 debug {
 testCoverageEnabled true
 } }
  43. 43. Good tests
  44. 44. Good tests • run in any order • run in isolation • run consistently • run fast • are orthogonal
  45. 45. How to write testable apps?
  46. 46. Rules of thumb • prefer pure Java • abstract away from Android APIs • separate business logic and UI • don’t write business logic into activities and fragments • try avoid static methods • use dependency injection
  47. 47. Questions?
  48. 48. References • Espresso • https://code.google.com/p/android-test-kit/ • https://code.google.com/p/android-test-kit/wiki/ EspressoV2CheatSheet
  49. 49. References • Mockito • http://mockito.org/ • https://github.com/mockito/mockito • Dexmaker • https://github.com/crittercism/dexmaker
  50. 50. References • Robolectric • http://robolectric.org/ • https://github.com/robolectric/robolectric
  51. 51. References • code coverage • JaCoCo • Eclipse Public License v1.0 • http://www.eclemma.org/jacoco/trunk/index.html • https://github.com/jacoco/jacoco • EMMA • Common Public License v1.0 • http://emma.sourceforge.net/
  • siriuseddy

    Sep. 14, 2015

Lecture on Android unit testing and instrumentation testing, mDevCamp 2015. Overview of available libraries and frameworks, and their capabilities and drawbacks.

Vistos

Vistos totais

1.797

No Slideshare

0

De incorporações

0

Número de incorporações

10

Ações

Baixados

32

Compartilhados

0

Comentários

0

Curtir

1

×