
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またはコメントに
残してお楽しみに !