Sleipnirを使用したSwiftでのBDDテスト


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()) } } } } 

スレイプニルの基本原理



また、SwiftでのBDDテスト用のいくつかの代替フレームワーク、たとえばQuickを見つけました。
それらの選択は、開発者の個人的な好みの問題です。

使用例


BookLibrary 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は、いくつかのテスト初期化および非初期化ブロックをサポートしています: beforeAllafterAllbeforeEachおよび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つのタイプのマッチャーのみをサポートしています: equalbeTruebeFalseですが 、新しいものはすぐに追加されます。

今後の計画


これは最初のパブリックリリースであるため、多くの機能はまだ実装されていません。 次のような実装計画が近い将来にあります。

バグレポートとフィードバックをgithubまたはコメントに残してお楽しみに

Source: https://habr.com/ru/post/J228811/


All Articles