Androidのテストよもやま話

 Androidのテストよもやま話

「京都Devかふぇ#5 〜Android Event 2019 リジェクトコン〜」の発表資料です #KyotoDevCafe

https://kyoto-dev-cafe.connpass.com/event/117287/

7ddcca09c00a2744b983974225447d19?s=128

Sho Ikeda

March 01, 2019
Tweet

Transcript

  1. Androidͷ ςετΑ΋΍·࿩ @ikesyo ژ౎Dev͔;͐#5 ʙAndroid Event 2019 ϦδΣΫτίϯʙ 2019-03-01 Fri

    #KyotoDevCafe
  2. @ikesyo • ͍͚͠ΐʔʗ஑ా ᠳ • ͸ͯͳ@ژ౎ • εϚʔτϑΥϯΞϓϦ։ൃ • iOS

    / Android / React Native • Swiftίϛολʔ • Kontribute΋ͯ͠Έ͍ͨʂ • https://twitter.com/ikesyo • https://github.com/ikesyo
  3. DroidKaigi 2019 • ΫϩεϓϥοτϑΥʔϜ։ൃ3छͷਆث React Native / TypeScript / GraphQL

    • takuji31͞ΜɺRyo Kitamura͞ΜͱҰॹͷνʔϜͰReact NativeΛॻ͍ͯ ͍·ͨ͠ • ωΠςΟϒͱReact NativeͷϋΠϒϦουΞϓϦ • iOSωΠςΟϒ΋AndroidωΠςΟϒ΋྆ํཧղ͍ͯ͠Δਓ͕͍Δͱྑ ͍ • ಛʹXcode΍GradleͳͲϏϧυγεςϜपΓ
  4. Androidͷςετͷ࿩

  5. PEAKSʢϐʔΫεʣʛ Androidςετશॻ

  6. None
  7. 2018೥·ͰͷAndroid ςετ૯·ͱΊ - ࠓ೥ͷ มߋͱདྷ೥ͷରࡦ - Qiita

  8. None
  9. Fin.

  10. ͱ͍͏༁ʹ͸ߦ͔ͳ͍ͷͰଓ͚·͢ • Local Unit Test (JVM) • JUnit 4, Robolectric

    • src/test • ./gradlew testDevelopDebugUnitTest • Instrumented Test (Emulator/Device) • AndroidJUnitRunner • src/androidTest • ./gradlew connectedDevelopDebugAndroidTest
  11. Robolectric 4.0 and androidx.test • Robolectric 4.0 Released! | Robolectric

    • @RunWith(RobolectricTestRunner::class) -> @RunWith(AndroidJUnit4::class) • 'androidx.test.ext:junit:1.1.0' • Contextͷऔಘํ๏ͷڞ௨ԽͳͲ • Local Unit TestͰ΋Instrumented TestͰ΋ಈ͘ϙʔλϒϧͳ ςετ͕ॻ͚ΔΑ͏ʹ
  12. ͦͯ͠Project Nitrogen΁…… The unification of testing apis between simulators and

    instrumentation opens up a lot of exciting possibilities! Project Nitrogen, which we also announced at Google I/O, will allow you to seamlessly move tests between runtime environments. This means that you will be able to take tests written against the new AndroidX Test APIs and run them on a local JVM, real or virtual device, or even a cloud based testing platform such as Firebase Test Lab. — Write Once, Run Everywhere Tests on Android – Android Developers – Medium
  13. Frictionless Android testing: write once, run everywhere (Google I/O '18)ΑΓ

  14. ςετ༻ϢʔςΟϦςΟ • μϛʔσʔλͷੜ੒ • ςετ༻ͷJSONͷϩʔυ • ςετ༻ͷDaggerͷComponent/Module • ...

  15. testͱandroidTestͷ྆ํʹஔ͘ͷʁ • γϯϘϦοΫϦϯΫʁ • ίʔυڞ༗ • Sharing code between unit

    tests and instrumentation tests on Android • src/sharedTest
  16. ςετͰͷίʔυڞ༗ def sharedTestSrcDir = "src/sharedTest/java" def sharedTestResDir = "src/sharedTest/resources" sourceSets

    { test { java.srcDirs += sharedTestSrcDir resources.srcDirs += sharedTestResDir } androidTest { java.srcDirs += sharedTestSrcDir resources.srcDirs += sharedTestResDir } }
  17. ςετͰͷίʔυڞ༗ • ςετίʔυ΋ϙʔλϒϧʹͳͬͨ • ςετίʔυࣗମ΋src/sharedTestʹஔ͚͹JVMɾσόΠ εʢΤϛϡϨʔλʣͷ྆ํͰςετͰ͖Δ • Project NitrogenͷઌऔΓʂ

  18. Spek

  19. Spek • Spek Framework • spekframework/spek: A specification framework for

    Kotlin • BDDελΠϧͷςετϑϨʔϜϫʔΫ • JUnit 5ϕʔε • 1.xͱ2.x
  20. Spek object CalculatorSpec: Spek({ describe("A calculator") { val calculator by

    memoized { Calculator() } describe("addition") { it("returns the sum of its arguments") { assertThat(3, calculator.add(1, 2)) } } } })
  21. Spek 1.x • JUnit 4ͷςετϥϯφʔϕʔεͰಈ͔͢ํ๏΋Ҋ಺͞Ε͍ͯ Δ: https://spekframework.github.io/spek/docs/latest/ #setting-up-legacy • @RunWith(JUnitPlatform::class)

    • @RunWith(AndroidJUnit4::class)ͱผͷςετϥϯφʔʹ ͳΔͷͰɺRobolectricͱҰॹʹ࢖͑ͳ͍
  22. Spek 1.x • https://spekframework.github.io/spek/docs/latest/ #_android Android integration tests is currently

    not supported !
  23. Spek 2.x • AndroidͰͷ࣮ߦ • Android - Spek Framework •

    android-junit5ͱ͍͏ϓϥάΠϯ͕ඞཁ: https://github.com/ mannodermaus/android-junit5 • READMEʹ͸࣮ݧతͳInstrumentation Test Supportͷهड़͕͋Δ: https://github.com/mannodermaus/android- junit5#instrumentation-test-support • Local Unit Test with RobolectricͰ͸ݫͦ͠͏ʁ
  24. Spek with Robolectric • ৘ใ͓଴͓ͪͯ͠Γ·͢ • ֤ॴͷIssue/PR • Integration with

    spek (kotlin test framework) · Issue #2996 · robolectric/robolectric • Robolectric integration · Issue #65 · spekframework/spek • Robolectric extension to support android unit tests by bangarharshit · Pull Request #324 · spekframework/spek
  25. Spek with Robolectric • Robolectric 4.2 • ϓϥάΠϯγεςϜ΁ͷظ଴ Robolectric 4.2

    introduces a new plugin system, replacing the previous ad-hoc customization mechanisms (subclassing RobolectricTestRunner, providing system properties, etc.). The old mechanisms are deprecated and will eventually be removed; let us know if there are missing extension points you need. See org.robolectric.pluginapi for more details [issue #4362].
  26. ! Happy Testing!!

  27. Thank you