Sleipnir presentation

Download Sleipnir presentation

Post on 19-Aug-2014

51 views

Category:

Engineering

0 download

Embed Size (px)

DESCRIPTION

BDD testing in Swift with Sleipnir

TRANSCRIPT

<ul><li> Sleipnir BDD style testing for Swift </li> <li> Who we are Mobile team at railsware.com Open source fans railsware.github.io/ </li> <li> Outline What is BDD Testing in Swift - XCTest vs BDD style What Sleipnir is How to use it Questions </li> <li> BDD </li> <li> BDD Behavior Driven Development </li> <li> BDD Behavior Driven Development BDD species that tests of any unit of software should be specied in terms of the desired behaviour of the unit. (from Wiki) ! BDD is all about specications </li> <li> BDD describe("Calculator") { ! describe("#add") { ! it("returns the sum of its arguments") { expect(Calculator().add(1, 2)).to(equal(3)) } } } </li> <li> Testing in Swift </li> <li> Testing in Swift - XCTest class BookTest : XCTestCase { func testPersonHasName() { let person = Person(name: "John Doe) ! XCTAssertNotNil(person.name, "name should not be nil") XCTAssertEqual(person.name, "John Doe", "incorrect name") } } </li> <li> Testing in Swift - XCTest class BookTest : XCTestCase { func testPersonHasName() { let person = Person(name: "John Doe) ! XCTAssertNotNil(person.name, "name should not be nil") XCTAssertEqual(person.name, "John Doe", "incorrect name") } } Does not specify behavior of the `Person` class </li> <li> Testing in Swift - BDD class PersonSpec : SleipnirSpec { var personSpec = describe("Person") { let person = Person(name: "John Doe") context("name") { it("should not be nil") { expect(person.name).toNot(beNil()) } it("should be correct") { expect(person.name).to(equal("John Doe")) } } } } </li> <li> Testing in Swift - BDD Test code is a specication of a class: Person name should not be nil Person name should be correct </li> <li> Introducing Sleipnir https://github.com/railsware/Sleipnir </li> <li> What Sleipnir is Mythological steed of Odin, Norse God </li> <li> What Sleipnir is BDD-style framework for Swift </li> <li> What Sleipnir is class HorseSpec : SleipnirSpec { var spec = describe("Horse") { context("usual") { it("is not awesome") { let usualHorse = UsualHorse() expect(usualHorse.legsCount).to(equal(4)) usualHorse.isAwesome.should.beFalse() } } context("Sleipnir") { it("is awesome") { let sleipnirHorse = Sleipnir() expect(sleipnirHorse.legsCount).to(equal(8)) sleipnirHorse.isAwesome.should.beTrue() } } } } </li> <li> Core principles Not using XCTest Pure Swift BDD testing framework Command line output Seeded random tests invocation </li> <li> How to use it </li> <li> Running specs 1. Create a test target in Xcode 2. Invoke Runner.run() method in main.swift 3. Run test target </li> <li> Running specs Running With Random Seed: 7197 ! ...F...... ! ! FAILURE Some spec should pass: /Path/To/Your/Specs/SomeSpec.swift:27 Expected to equal ! ! Finished in 0.0102 seconds ! 10 examples, 1 failures </li> <li> Setup/Teardown blocks beforeEach { } afterEach { } beforeAll { } afterAll { } </li> <li> Setup/Teardown blocks let someSpec = describe("Some spec") { var someArray: [Int]? ! beforeEach { someArray = [1, 2, 3] } afterEach { someArray = nil } it("should pass") { expect(someArray).toNot(beNil()) expect(someArray).to(contain(3)) } } </li> <li> Focused specs Focus means Run only focused stuff Useful in a project with a lot of specs </li> <li> Focused specs describe("Some spec") { fit("focused") { // WILL RUN } it("not focused") { // WILL NOT RUN } } </li> <li> Focused specs fdescribe("focused group") { // ... } ! fcontext("focused group") { // ... } fit("focused example") { // ... } </li> <li> Pending specs Pending means Dont run this stuff Useful to denote an example that does not pass yet </li> <li> Pending specs xdescribe("Pending group") { it("will not run") { expect(false).to(beTrue()) } it("is pending", PENDING) } Running With Random Seed: 2428 ! ...P....... ! PENDING Pending group is pending ! ! Finished in 0.0062 seconds ! 11 examples, 0 failures, 1 pending </li> <li> Shared example groups Useful for extracting common specs Allow to run same specs in different context </li> <li> Shared example groups sharedExamplesFor("some awesome stuff") { (sharedContext : SharedContext) in var stuff: Stuff? beforeEach { stuff = sharedContext()["stuff"] as Stuff } it("should be awesome") { expect(stuff.awesome).to(beTrue()) } } </li> <li> Matchers equal beNil beFalse/beTrue beGreaterThan/beLessThan expect(3).to(equal(3)) expect(3).toNot(beNil()) expect(true).to(beTrue()) expect(3).to(beGreaterThan(1)) </li> <li> Matchers on collections/strings contain beginWith/endWith beEmpty expect([1,2,3]).to(contain(1,2)) expect("foobar").to(beginWith("foo")) expect([1,2,3]).toNot(beEmpty()) </li> <li> Questions? https://github.com/railsware/Sleipnir </li> </ul>