Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Quickで体験するテストの構造化

laiso
January 19, 2017

 Quickで体験するテストの構造化

iOS Test Night #2
https://testnight.connpass.com/event/45717/

[テストの書き方、Quickの使い方](https://github.com/Quick/Quick/tree/master/Documentation/ja)
[Better Specs \{ rspec guidelines with ruby \}](http://betterspecs.org/jp/)
[私はRSpecでテストをこんな感じで書いてる](http://d.hatena.ne.jp/sinsoku/20120820/1345470914)
[RSpecによるユニットテストの書き方](https://recompile.net/posts/how-to-write-unit-test-with-rspec.html)
[改めて学ぶ RSpec](http://magazine.rubyist.net/?0035-RSpecInPractice)
[RSpec の入門とその一歩先へ](http://d.hatena.ne.jp/t-wada/20100228/p1)
[テストコードの期待値はDRYを捨ててベタ書きする ~テストコードの重要な役割とは?~](http://qiita.com/jnchito/items/eb3cfa9f7db752dcb796)
[DRY原則とテストの可読性](http://r7kamura.hatenablog.com/entry/2012/09/02/185129)

laiso

January 19, 2017
Tweet

More Decks by laiso

Other Decks in Technology

Transcript

  1. QuickͰମݧ͢Δςετͷ
    ߏ଄Խ
    iOS Test Night #2 (01/19/2017)

    View full-size slide

  2. ͲͷςετϑϨʔϜϫʔΫΛ࢖ͬ
    ͍ͯ·͔͢?

    View full-size slide

  3. Quickͱ͸
    • SpecελΠϧͳςετΛ࣮ݱ͢ΔϑϨʔϜϫʔΫ
    • RSpec(Ruby)΍ͦͷ೿ੜπʔϧ͔ΒώϯτΛಘͨ
    • @modocache (Kiwi Contributor)ʹΑΓ։ൃ͞Εͨ

    View full-size slide

  4. ࢲ͕QuickΛ࢖͏ཧ༝
    • ίϛϡχςΟͰ։ൃ͞Ε͍ͯΔ
    • RSpec/Rubyςετπʔϧͷ஌ݟ
    • ςετίʔυͷߏ଄Խ

    View full-size slide

  5. iOSςετπʔϧ͸ίϛϡχςΟ
    ʹΑͬͯݗҾ͞Ε͖ͯͨ
    • OCTestɻGTMɻSenTestingKitͷެࣜԽ
    • GHUnit, Kiwi, Specta
    • KIF, Calabash, Appium

    View full-size slide

  6. class PersonTests: XCTestCase {
    var person: Person!
    override func setUp() {
    person = Person(name: "John", age: 20)
    }
    func testName() {
    XCTAssertEqual(person.name, "John")
    }
    func testAge() {
    XCTAssertEqual(person.age, 20)
    }
    }

    View full-size slide

  7. class PersonSpec: QuickSpec {
    override func spec() {
    var person: Person!
    beforeEach {
    person = Person(name: "John", age: 20)
    }
    it("Name") {
    XCTAssertEqual(person.name, "John")
    }
    it("Age") {
    XCTAssertEqual(person.age, 20)
    }
    }
    }

    View full-size slide

  8. ผͷPersonͷςετΛ૿΍ͨ͠
    ͍

    View full-size slide

  9. class PersonTests: XCTestCase {
    func testJohn() {
    let person = Person(name: "John", age: 28)
    XCTAssertEqual(person.name, "John")
    XCTAssertEqual(person.age, 28)
    }
    func testNancy() {
    let person = Person(name: "Nancy", age: 18)
    XCTAssertEqual(person.name, "Nancy")
    XCTAssertEqual(person.age, 18)
    }
    }

    View full-size slide

  10. ໰୊఺
    • ςετର৅ͷηοτΞοϓ͕ஈʑෳࡶʹ
    • ෳ਺ͷassertΛ1ͭͷςετͰ͕ͪ͠
    • ԿΛͲ͏ςετ͍͔ͨ͠ᐆດʹ……

    View full-size slide

  11. class PersonSpec: QuickSpec {
    override func spec() {
    var person: Person!
    context("John") {
    beforeEach {
    person = Person(name: "John", age: 28)
    }
    it("Name") {
    XCTAssertEqual(person.name, "John")
    }
    it("Age") {
    XCTAssertEqual(person.age, 28)
    }
    }
    context("Nancy") {
    beforeEach {
    person = Person(name: "Nancy", age: 18)
    }
    it("Name") {
    XCTAssertEqual(person.name, "Nancy")
    }
    it("Age") {
    XCTAssertEqual(person.age, 28)
    }
    }
    }
    }

    View full-size slide

  12. Quickجຊ༻ޠ
    • describe͸ʮมΘΒͳ͍ࣄ࣮ʯϝιου໊΍ػ
    ೳɺ؍఺
    • context͸ʮಛఆͷঢ়گʯલఏ৚݅΍ಛఆͷΦϒ
    δΣΫτͷঢ়ଶ
    • ΫϩʔδϟͷείʔϓͰߏ଄Λදݱ͢Δ

    View full-size slide

  13. QuickͰղܾͰ͖ͳ͍໰୊
    • ςετίʔυ͕୹͔͘ͳΔ
    • ςετͣ͠Β͍ίʔυΛͳΜͱ͔͢Δ
    • XCTestͰ͸࣮ݱͰ͖ͳ͍ػೳΛ׆༻͢Δ

    View full-size slide

  14. Qucikςετͷॻ͖ํ͸XCTestͰ
    ΋Ͱ͖Δ
    • ςετ໊ͷن໿Λࣗ෼ͰकΔඞཁ͕͋Δ
    • ৑௕ʹͳΓಡΈͮΒ͘ͳΓ͕ͪ
    • QuickͰXCTestͬΆ͍ϕλͳςετͱ͍͏ͷ΋ॻ
    ͚Δ

    View full-size slide

  15. class PersonAsJohnTests: XCTestCase {
    var john: Person!
    override func setUp() {
    john = Person(name: "John", age: 20)
    }
    //func testName() {
    //func testAge() {
    }
    class PersonAsNancyTests: XCTestCase {
    var nancy: Person!
    override func setUp() {
    nancy = Person(name: "Nancy", age: 18)
    }
    //func testName() {
    //func testAge() {
    }

    View full-size slide

  16. QuickಋೖͷϦεΫ
    • ֶशίετɿผͷࢥ૝Λ࣋ͭϑϨʔϜϫʔΫͷ
    ಋೖ
    • ੑೳίετɿ࢓૊Έ্Φʔόʔϔου͕ੜ͡Δ
    • อकίετɿXcode+SDKͷมߋʹରͯ͠௥ै͢
    Δ

    View full-size slide

  17. ςετΛߏ଄Խ͢Δར఺
    • ςετέʔεΛ૿΍͠΍͍͢
    • ςετ৚݅Λ໢ཏ͠΍͍͢
    • ϝϯςφϯε͠΍͍͢(૿͖͑ͯͨΒtreeΛ෼ׂ)

    View full-size slide

  18. ͲΜͳ࣌ʹςετΛॻ͘΂͖͔?

    View full-size slide

  19. ෆ۩߹ͷमਖ਼ - ϦάϨογϣϯ
    • मਖ਼ͷඞཁ͕ੜ͡Δ=Ϣʔβʔʹͱͬͯʮͨͩ͠
    ͍ಈ࡞ʯͷՁ஋͕͋Δ
    • ࣦഊ͢Δςετέʔε͕༻ҙ͞Ε͍ͯΔ
    • E2EͰςετ͢ΔͷͰઃܭΛେ͖͘มߋͰ͖Δ

    View full-size slide

  20. ৽نϞδϡʔϧͷ௥Ճ
    • ৽ػೳ=Ձ஋͕͋Δ
    • ઃܭͱͯ͠ͷςετυϦϒϯ
    • طଘͷઃܭͷӨڹΛड͚ͳ͍ͷͰ೉қ౓͕௿͍

    View full-size slide

  21. πʔϧΑΓϚΠϯυηοτ
    • ʮ໾ʹཱͭςετΛॻ͘ͷ͸ඇৗʹ؆୯Ͱɺ
    XCTestͰ΋QuickͰ΋େͨ͠ҧ͍͕ͳ͍ʯ
    @modcache
    • http://modocache.io/testing-tutorial-in-
    japanese
    • ඞཁͳͷ͸υΩϡϝϯτ΍஌ݟΛڞ༗͢Δ৔
    • ςετΛॻ͍ͯ։ൃ͕Ճ଎ͨ͠ମݧ

    View full-size slide

  22. ·ͱΊ
    • ςετͷߏ଄ԽʹऔΓ૊ΉͱXCTestͰ͸৑௕ʹ
    ͳΔ
    • Quick͸ςετΛ໊લͱ֊૚ͰදݱͰ͖Δ
    • ߏ଄ԽʹΑͬͯʮ໾ʹཱͭςετΛॻ͘ʯ͜ͱ
    ͕ॏཁ

    View full-size slide