Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Try KotlinTest

Rui Kowase
December 15, 2017

Try KotlinTest

Android Test Nightの資料です。

Android Test Night #2 - connpass
https://testnight.connpass.com/event/69397/

Rui Kowase

December 15, 2017
Tweet

More Decks by Rui Kowase

Other Decks in Technology

Transcript

  1. Assertion Lib (Test Framework) for Kotlin 名前 GitHub Star 簡単な説明

    spek 970 Spec風、Java 8 Only kotlintest 593 Spec風 expekt 96 メソッドチェーン記法、更新頻度低 Assertk 78 AssertJ風 knit 57 DroidKaigi 2017アプリで利用、更新頻度低 (2017/12/15時点)
  2. 基本的な使い方 // test cases in init block class MyTests :

    StringSpec() { init { // tests here } } // test cases in lambda expression class MyTests : StringSpec({ // tests here })
  3. 基本的な使い方 // test cases in init block class MyTests :

    StringSpec() { init { // tests here } } // test cases in lambda expression class MyTests : StringSpec({ // tests here })
  4. 基本的な使い方 // test cases in init block class MyTests :

    StringSpec() { init { // tests here } } // test cases in lambda expression class MyTests : StringSpec({ // tests here })
  5. 基本的な使い方 // test cases in init block class MyTests :

    StringSpec() { init { // tests here } } // test cases in lambda expression class MyTests : StringSpec({ // tests here })
  6. 色々なSpec Spec 簡単な説明 String Spec StringSpec reduces the syntax to

    the absolute minimum. Fun Spec FunSpec allows you to create tests similar to the junit style. Should Spec ShouldSpec is similar to fun spec, but uses the keyword should instead of test. Word Spec WordSpec uses the keyword should and uses that to nest test blocks after a context string. Feature Spec FeatureSpec allows you to use feature and scenario. Behavior Spec BehaviorSpec allows you to use given, when, then. Free Spec FreeSpec allows you to nest arbitary levels of depth using the keyword - (minus).
  7. String Spec class MyTests : StringSpec() { init { "strings.length

    should return size of string" { "hello".length shouldBe 5 } } }
  8. Word Spec class MyTests : WordSpec() { init { "String.length"

    should { "return the length of the string" { "sammy".length shouldBe 5 "".length shouldBe 0 } } } }
  9. Word Spec class MyTests : WordSpec() { init { "String.length"

    should { "return the length of the string" { "sammy".length shouldBe 5 "".length shouldBe 0 } } } }
  10. Feature Spec class MyTests : FeatureSpec() { init { feature("the

    thingy bob") { scenario("should explode when I touch it") { // test here } } } }
  11. Feature Spec class MyTests : FeatureSpec() { init { feature("the

    thingy bob") { scenario("should explode when I touch it") { // test here } } } }
  12. Behavior Spec class MyTests : BehaviorSpec() { init { given("a

    broomstick") { `when`("I sit on it") { then("I should be able to fly") { // test code } } } } }
  13. Behavior Spec class MyTests : BehaviorSpec() { init { given("a

    broomstick") { `when`("I sit on it") { then("I should be able to fly") { // test code } } } } }
  14. Free Spec class MyTests : FreeSpec() { init { "String.length"

    - { "should return the length of the string" { "sammy".length shouldBe 5 "".length shouldBe 0 } } } }
  15. Free Spec class MyTests : FreeSpec() { init { "String.length"

    - { "should return the length of the string" { "sammy".length shouldBe 5 "".length shouldBe 0 } } } }
  16. Intercepting a Test Case override fun interceptTestCase(context: TestCaseContext, test: ()

    -> Unit) { // before test() // don't forget to call test()! // after }
  17. Intercepting a Test Case override fun interceptTestCase(context: TestCaseContext, test: ()

    -> Unit) { // before test() // don't forget to call test()! // after } setUp()
  18. Intercepting a Test Case override fun interceptTestCase(context: TestCaseContext, test: ()

    -> Unit) { // before test() // don't forget to call test()! // after } tearDown()
  19. AndroidのPresenterのテストで使ってみる class StringSpecTest : StringSpec() { @Mock lateinit var mRepository:

    GitHubRepository @Mock lateinit var mView: GitHubContract.View lateinit var mPresenter: GitHubPresenter lateinit var mSchedulerProvider: BaseSchedulerProvider override fun interceptTestCase(context: TestCaseContext, test: () -> Unit) { MockitoAnnotations.initMocks(this) mSchedulerProvider = ImmediateSchedulerProvider() mPresenter = GitHubPresenter(mRepository, mView, mSchedulerProvider) test() // don't forget to call test()! }
  20. AndroidのPresenterのテストで使ってみる class StringSpecTest : StringSpec() { @Mock lateinit var mRepository:

    GitHubRepository @Mock lateinit var mView: GitHubContract.View lateinit var mPresenter: GitHubPresenter lateinit var mSchedulerProvider: BaseSchedulerProvider override fun interceptTestCase(context: TestCaseContext, test: () -> Unit) { MockitoAnnotations.initMocks(this) mSchedulerProvider = ImmediateSchedulerProvider() mPresenter = GitHubPresenter(mRepository, mView, mSchedulerProvider) test() // don't forget to call test()! }
  21. AndroidのPresenterのテストで使ってみる class StringSpecTest : StringSpec() { @Mock lateinit var mRepository:

    GitHubRepository @Mock lateinit var mView: GitHubContract.View lateinit var mPresenter: GitHubPresenter lateinit var mSchedulerProvider: BaseSchedulerProvider override fun interceptTestCase(context: TestCaseContext, test: () -> Unit) { MockitoAnnotations.initMocks(this) mSchedulerProvider = ImmediateSchedulerProvider() mPresenter = GitHubPresenter(mRepository, mView, mSchedulerProvider) test() // don't forget to call test()! }
  22. AndroidのPresenterのテストで使ってみる init { "Should init service when presenter started"{ mPresenter.start()

    verify(mRepository).initService() } "Should show error when request failed"{ Mockito.`when`(mRepository.request("")).thenRetrn(Observable.error(Exception()) mPresenter.request(GitHubPresenterTest.USER) verify(mView).showError() } }
  23. AndroidのPresenterのテストで使ってみる init { "Should init service when presenter started"{ mPresenter.start()

    verify(mRepository).initService() } "Should show error when request failed"{ Mockito.`when`(mRepository.request("")).thenRetrn(Observable.error(Exception()) mPresenter.request(GitHubPresenterTest.USER) verify(mView).showError() } }
  24. AndroidのPresenterのテストで使ってみる init { "Should init service when presenter started"{ mPresenter.start()

    verify(mRepository).initService() } "Should show error when request failed"{ Mockito.`when`(mRepository.request("")).thenRetrn(Observable.error(Exception()) mPresenter.request(GitHubPresenterTest.USER) verify(mView).showError() } }