この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
こんにちは。モバイル事業部の平屋です。
本記事では、テストフレームワークQuickを使ったユニットテストで、指定したテストケースだけ一時的に実行する方法を紹介します。
テストの新規実装や修正時に特定のケースを繰り返し実行したい場合などに役に立つかと思います。
検証環境
- macOS Monterey 12.2
- Xcode Version 13.1
- Quick 4.0.0
- Nimble 9.2.1
サンプルコード
今回は以下の例を使って検証してみました。
class DolphinSpec: QuickSpec {
override func spec() {
describe("describe 1") {
context("context 1") {
it("it 1") {
expect(Dolphin().isHappy).to(beTrue())
}
it("it 2") {
expect(Dolphin().isHappy).to(beTrue())
}
}
context("context 2") {
it("it 3") {
expect(Dolphin().isHappy).to(beTrue())
}
it("it 4") {
expect(Dolphin().isHappy).to(beTrue())
}
}
}
describe("describe 2") {
context("context 3") {
it("it 5") {
expect(Dolphin().isHappy).to(beTrue())
}
it("it 6") {
expect(Dolphin().isHappy).to(beTrue())
}
}
context("context 4") {
it("it 7") {
expect(Dolphin().isHappy).to(beTrue())
}
it("it 8") {
expect(Dolphin().isHappy).to(beTrue())
}
}
}
}
}
指定したテストケースだけ一時的に実行する
指定したテストケース(またはテストグループ)だけ一時的に実行するには、以下の代わりに、
it() { ... }
context() { ... }
describe() { ... }
先頭にf
が付いた以下を使います。
fit() { ... }
fcontext() { ... }
fdescribe() { ... }
fit()
// 今回のサンプルコードの場合、"it 1"のテストケースのみ実行される
fit("it 1") {
// ...
}
fcontext()
// 今回のサンプルコードの場合、"it 7"と"it 8"のテストケースのみ実行される
fcontext("context 4") {
// ...
}
fdescribe
// 今回のサンプルコードの場合、"it 5"〜"it 8"のテストケースが実行される
fdescribe("describe 2") {
// ...
}
指定したテストケースを一時的に除外する
指定したテストケース(またはテストグループ)を一時的に除外するには、以下の代わりに、
it() { ... }
context() { ... }
describe() { ... }
先頭にx
が付いた以下を使います。
xit() { ... }
xcontext() { ... }
xdescribe() { ... }
xit()
// 今回のサンプルコードの場合、"it 2"〜"it 8"のテストケースが実行される
xit("it 1") {
// ...
}
xcontext()
// 今回のサンプルコードの場合、"it 3"〜"it 8"のテストケースが実行される
xcontext("context 1") {
// ...
}
xdescribe()
// 今回のサンプルコードの場合、"it 5"〜"it 8"のテストケースが実行される
xdescribe("describe 1") {
// ...
}