LoginSignup
2
2

More than 3 years have passed since last update.

[ Kotlin をやろう! ] Spek2 でユニットテストを書いてみよう

Posted at

概要

今回はユニットテストを書いて、実行して見たいと思います!

手順

1. build.gradle で環境を構築する

すみません、この辺すっ飛ばします

build.gradle
T.B.D

2. test/SampleSpec.kt を用意する

まずは、この基本的な形をしっかりと覚えてよう
この形さえ覚えておけば、テストはいくらでも拡充できる

test/SampleSpec.kt
package test

import io.ktor.server.testing.*
import kotlinx.coroutines.*
import org.junit.*
import org.spekframework.spek2.*
import org.spekframework.spek2.style.specification.*

object SampleSpec: Spek({

    describe("どのクラス、どのオブジェクトをユニットテストしたいかを記述 ") {

        // describe の事前処理 / 事後処理
        beforeGroup { }
        afterGroup { }

        context("ユニットテストの概要を記述") {
            // context の事前処理 / 事後処理
            beforeGroup { }
            afterGroup { }

            // on の事前処理 / 事後処理
            beforeEachTest { }
            afterEachTest { }
            on("ユニットテストの詳細を記述 - 1") {
                it("Assert(検証) 内容を記述") {
                    Assert.assertEquals(1, 1)
                }
                it("Assert(検証) 内容を記述") {
                    Assert.assertFalse(false)
                }
            }
            on("ユニットテストの詳細を記述 - 2") {
                runBlocking {
                    it("Assert(検証) 内容を記述") {
                        Assert.assertTrue(true)
                    }
                }
            }
        }
    }
})

3. 補足説明

import 各種

import io.ktor.server.testing.*
  • on() を使用するため
import kotlinx.coroutines.*
  • runBlocking { } を使用するため
import org.junit.*
  • Assert.xxx() を使用するために
import org.spekframework.spek2.*`
import org.spekframework.spek2.style.specification.*

Spek2 を使用するために

事前処理 / 事後処理 各種

beforeGroup { }
afterGroup { }
beforeGroup { }
afterGroup { }
beforeEachTest { }
afterEachTest { }

動かしてみよう

1. ユニットテスト実行

と、その前に、、
beforeafterがどのような順番で動いているかを確認しておく

コードを下記に書き換えておく

test/SampleSpec.kt
package test

import io.ktor.server.testing.*
import kotlinx.coroutines.*
import org.junit.*
import org.spekframework.spek2.*
import org.spekframework.spek2.style.specification.*

object SampleSpec: Spek({

    describe("どのクラス、どのオブジェクトをユニットテストしたいかを記述 ") {

        // describe の事前処理 / 事後処理
        beforeGroup { }
        afterGroup { }

        context("ユニットテストの概要を記述") {
            // context の事前処理 / 事後処理
            beforeGroup {
                println("context の事前処理")
            }
            afterGroup {
                println("context の事後処理")
            }

            // on の事前処理 / 事後処理
            beforeEachTest {
                println("on の事前処理")
            }
            afterEachTest {
                println("on 事後処理")
            }
            on("ユニットテストの詳細を記述 - 1") {
                println("ユニットテストの詳細を記述 - 1")
                it("Assert(検証) 内容を記述 1 - 1") {
                    println("Assert(検証) 内容を記述 1 - 1")
                    Assert.assertEquals(1, 1)
                }
                it("Assert(検証) 内容を記述 1 - 2") {
                    println("Assert(検証) 内容を記述 1 - 2")
                    Assert.assertFalse(false)
                }
            }
            on("ユニットテストの詳細を記述 2 - 1") {
                println("ユニットテストの詳細を記述 - 2")
                runBlocking {
                    it("Assert(検証) 内容を記述 2 - 1") {
                        println("Assert(検証) 内容を記述 2 - 1")
                        Assert.assertTrue(true)
                    }
                }
            }
        }
    }
})

2. ユニットテスト実行

下記コマンドで実行

$ ./gradlew test

さらに、うまくいけば下記に出力されるはず

$ ./gradlew test

> Task :junitPlatformTest
ユニットテストの詳細を記述 - 1
Assert(検証) 内容を記述 1 - 1
Assert(検証) 内容を記述 1 - 2
ユニットテストの詳細を記述 - 2
Assert(検証) 内容を記述 2 - 1

Test run finished after 4 ms
[         1 containers found      ]
[         0 containers skipped    ]
[         0 containers started    ]
[         0 containers aborted    ]
[         0 containers successful ]
[         0 containers failed     ]
[         0 tests found           ]
[         0 tests skipped         ]
[         0 tests started         ]
[         0 tests aborted         ]
[         0 tests successful      ]
[         0 tests failed          ]


BUILD SUCCESSFUL in 3s

最後に

なし

次回はこれ!

なし

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2