Objective-C開発者は、コードのBDDテストにさまざまなフレームワークを使用できます。
それらのいくつか:
Swiftプログラミング言語の出現により、Objective-Cに縛られることなく、純粋なSwiftにBDDスタイルのテストフレームワークを実装することにしました。
数週間の実装後、 Sleipnirフレームワークの最初のパブリックバージョンをリリースしました。
SleipnirはCedarフレームワークに触発され、このスタイルでBDDテストを作成できます。
class SampleSpec : SleipnirSpec { var spec : () = describe("Horse") { context("usual") { it("is not awesome") { let usualHorse = UsualHorse() expect(usualHorse.legsCount).to(equal(4)) expect(usualHorse.isAwesome()).to(beFalse()) } } context("Sleipnir") { it("is awesome") { let sleipnirHorse = Sleipnir() expect(sleipnirHorse.legsCount).to(equal(8)) expect(sleipnirHorse.isAwesome()).to(beTrue()) } } } }
スレイプニルの基本原理
- Sleipnirは
NSObject
に依存せず、純粋なSwift上のBDDフレームワークです - Sleipnirは
XCTest
使用しません - Sleipnirはテスト結果をコマンドラインに便利な方法で表示し、結果の出力を拡張または補足することができます
- ランダム化されたテストの実行、フォーカスされた/除外されたテストグループなどの他の機能
また、SwiftでのBDDテスト用のいくつかの代替フレームワーク、たとえばQuickを見つけました。
それらの選択は、開発者の個人的な好みの問題です。
使用例
Book
と
Library
2つのクラスを定義し、それらのテストを作成します。
Book
クラスには、本の著者とタイトルに関する情報が含まれています。
class Book { var title: String var author: String init(title: String, author: String) { self.title = title self.author = author } }
Library
クラスは、書籍の単純なコレクションです。
class Library { var books: Book[] init() { self.books = Book[]() } func addBook(book: Book) { books.append(book) } func removeLastBook() { books.removeLast() } func clear() { books.removeAll() } func size() -> Int { return books.count } func hasBooks() -> Bool { return size() > 0 } func filterBy(#author: String) -> Book[] { return books.filter { $0.author == author } } func filterBy(#title: String) -> Book[] { return books.filter { !$0.title.rangeOfString(title).isEmpty } } }
最初に、
Book
クラスの初期化の正確さをテストします。
class LibrarySpec : SleipnirSpec { var book : () = context("Book") { var swiftBook: Book? beforeAll { swiftBook = Book(title: "Introduction to Swift", author: "Apple Inc.") } it("has title") { expect(swiftBook!.title).to(equal("Introduction to Swift")) } it("has author") { expect(swiftBook!.author).to(equal("Apple Inc.")) } } }
SleipnirSpec
クラスを継承する
LibrarySpec
クラスを作成しました。 メイン
context
が含まれ、
Book
クラスの作成されたオブジェクトのプロパティをチェックする2つの
exampla
を定義します。
Book
クラスオブジェクトは
beforeAll{ }
ブロックで作成されます。
Sleipnirは、いくつかのテスト初期化および非初期化ブロックをサポートしています: beforeAll 、 afterAll 、 beforeEachおよびafterEach 。
テストのすべての上位レベルの
exampl
( 記述またはコンテキスト )を呼び出した結果は、正しいインスタンスの変数に割り当てられる必要があります。
var book : () = context("Book") { }
次に、
Library
クラスの動作をテストします。
class LibrarySpec : SleipnirSpec { ... var library : () = context("Library") { var swiftLibrary: Library? beforeAll { swiftLibrary = Library() } afterAll { swiftLibrary = nil } describe("empty") { it("has no books") { expect(swiftLibrary!.hasBooks()).to(beFalse()) } } describe("with books") { beforeEach { swiftLibrary!.addBook(Book(title: "Introduction to Swift", author: "Apple Inc.")) swiftLibrary!.addBook(Book(title: "Using Swift with Cocoa", author: "Apple Inc.")) swiftLibrary!.addBook(Book(title: "Swift tutorials", author: "John Doe")) swiftLibrary!.addBook(Book(title: "Programming iOS with Swift", author: "Vladimir Swiftin")) } afterEach { swiftLibrary!.clear() } it("is not empty") { expect(swiftLibrary!.hasBooks()).to(beTrue()) } it("has correct number of books") { expect(swiftLibrary!.size()).to(equal(4)) swiftLibrary!.removeLastBook() expect(swiftLibrary!.size()).to(equal(3)) } describe("filters books") { it("by author") { expect(swiftLibrary!.filterBy(author: "Apple Inc.").count).to(equal(2)) } it("by title") { expect(swiftLibrary!.filterBy(title: "tutorials").count).to(equal(1)) } } } } }
これらのテストを実行すると、コマンドラインに次の情報が表示されます。
Running With Random Seed: 657464010 ....... Finished in 0.0091 seconds 7 examples, 0 failures
テストが失敗した場合、ファイルと行番号を含む詳細なエラー情報が表示されます。
Running With Random Seed: 2027508247 ..F.... FAILURE Library with books has correct number of books: /Users/atermenji/Coding/objc/Sleipnir/Sample/LibrarySpec.swift:64 Expected 3 to equal [2] Finished in 0.0043 seconds 7 examples, 1 failures
単純な期待値とマッチャーを使用して、
Library
クラスの動作をテストしました。
Sleipnirは現在3つのタイプのマッチャーのみをサポートしています: equal 、 beTrue 、 beFalseですが 、新しいものはすぐに追加されます。
今後の計画
これは最初のパブリックリリースであるため、多くの機能はまだ実装されていません。 次のような実装計画が近い将来にあります。
- フレームワーク配布メカニズム
- 保留中の例をサポート
- フォーカス可能/除外テストグループの実装
- Xcodeテンプレート
- 共有サンプルのサポート
- 構文サポートは (
some_value should equal(some_another_value)
) - Wikiドキュメント
- SleipnirでSleipniraをテストする
- 以下を含む追加のマッチャー:
- ベニル
- beGreaterThan 、 beLessThan 、 beInRangeOf
- 非同期マッチャー( will 、 willNot 、 after )
- コレクションおよび文字列のマッチャー( contains 、 haveCount 、 beginWith 、 endWithなど)
バグレポートとフィードバックをgithubまたはコメントに残してお楽しみに !