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

Swift で ParameterizedTest をやってみた話/swift-parameterized-test

Swift で ParameterizedTest をやってみた話/swift-parameterized-test

iOS Test Night #10 での発表
https://testnight.connpass.com/event/119381/

Yusuke Hosonuma

April 16, 2019
Tweet

More Decks by Yusuke Hosonuma

Other Decks in Programming

Transcript

  1. Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    4XJGUͰ1BSBNFUFSJ[FE5FTU
    Λ΍ͬͯΈͨ࿩
    1
    iOS Test Night #10
    2019/4/16ʢTueʣ

    ࡉপ ༞հ@DeNA

    View full-size slide

  2. "CPVU
    2
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

    View full-size slide

  3. :VTVLF)PTPOVNB
    • 48&5άϧʔϓ
    ⁃ ςετࣗಈԽɹJ04(P
    • 'BWPSJUF

    • ۙگ
    ⁃ USZ4XJGU50,:0Ͱొஃ
    ⁃ വؗʹཱྀߦʹߦ͖ͬͯͨʂ
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    3
    @tobi462ʢτϏʣ
    SwiftCheckͰ࢝ΊΔ
    Property-based Testing

    View full-size slide

  4. ࠓ೔࿩͢͜ͱ
    4
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

    View full-size slide

  5. 4XJGUͰ
    1BSBNFUFSJ[FEUFTU
    Λ΍ͬͯΈͨ࿩
    5
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

    View full-size slide

  6. 1BSBNFUFSJ[FEUFTUʁ
    6
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

    View full-size slide

  7. 7
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    Parameterized-Test
    • ࣄલʹςετσʔλΛྻڍ͢Δॻ͖ํ
    • ςετσʔλͷ௥Ճɾ࡟আ͕͠΍͍͢ͱ͍͏ར఺
    • +6OJUͰ͸ඪ४Ͱαϙʔτ
    • (PݴޠͰ͸Ұൠతͳॻ͖ํ
    • ςʔϒϧۦಈςετͱݺ͹ΕͨΓ

    View full-size slide

  8. 9$5FTUͰ͸ະαϙʔτ
    8
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ΋ͪΖΜ

    View full-size slide

  9. Ͱ΋ࣗ෼Ͱॻ͚Δ͸ͣʂ
    9
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

    View full-size slide

  10. 'J[[#V[[ͷςετ
    Λॻ͍ͯΈΔ͜ͱʹ͢Δͧʂ
    10
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

    View full-size slide

  11. 11
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    XCTest Ͱ Parameterized Test
    class ParamTests: XCTestCase {
    func testParams() {
    let tests: [(args: Int, expect: String)] = [
    (1, "1"),
    (2, "2"),
    (3, "Fizz"),
    (4, "4"),
    (5, "Buzz"),
    (6, "6"),
    ]
    for (args, expect) in tests {
    let result = fizzBuzz(args)
    XCTAssertEqual(expect, result)
    }
    }
    }

    View full-size slide

  12. 12
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    XCTest Ͱ Parameterized Test
    class ParamTests: XCTestCase {
    func testParams() {
    let tests: [(args: Int, expect: String)] = [
    (1, "1"),
    (2, "2"),
    (3, "Fizz"),
    (4, "4"),
    (5, "Buzz"),
    (6, "6"),
    ]
    for (args, expect) in tests {
    let result = fizzBuzz(args)
    XCTAssertEqual(expect, result)
    }
    }
    }
    ೖྗ஋ͱظ଴஋ͷλϓϧ

    View full-size slide

  13. 13
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    XCTest Ͱ Parameterized Test
    class ParamTests: XCTestCase {
    func testParams() {
    let tests: [(args: Int, expect: String)] = [
    (1, "1"),
    (2, "2"),
    (3, "Fizz"),
    (4, "4"),
    (5, "Buzz"),
    (6, "6"),
    ]
    for (args, expect) in tests {
    let result = fizzBuzz(args)
    XCTAssertEqual(expect, result)
    }
    }
    }
    ഑ྻͰςετσʔλΛ༻ҙ

    View full-size slide

  14. 14
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    XCTest Ͱ Parameterized Test
    class ParamTests: XCTestCase {
    func testParams() {
    let tests: [(args: Int, expect: String)] = [
    (1, "1"),
    (2, "2"),
    (3, "Fizz"),
    (4, "4"),
    (5, "Buzz"),
    (6, "6"),
    ]
    for (args, expect) in tests {
    let result = fizzBuzz(args)
    XCTAssertEqual(expect, result)
    }
    }
    }
    ϧʔϓͰॱ൪ʹݕূ

    View full-size slide

  15. 15
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    XCTest Ͱ Parameterized Test
    class ParamTests: XCTestCase {
    func testParams() {
    let tests: [(args: Int, expect: String)] = [
    (1, "1"),
    (2, "2"),
    (3, "Fizz"),
    (4, "4"),
    (5, "Buzz"),
    (6, "6"),
    ]
    for (args, expect) in tests {
    let result = fizzBuzz(args)
    XCTAssertEqual(expect, result)
    }
    }
    }

    View full-size slide

  16. 16
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    XCTest Ͱ Parameterized Test
    class ParamTests: XCTestCase {
    func testParams() {
    let tests: [(args: Int, expect: String)] = [
    (1, "1"),
    (2, "2"),
    (3, "Fizz"),
    (4, "4"),
    (5, "Buzz"),
    (6, "6"),
    ]
    for (args, expect) in tests {
    let result = fizzBuzz(args)
    XCTAssertEqual(expect, result)
    }
    }
    }
    ςετσʔλ௥Ճɾ࡟আָ͕ͩ͠ɺ
    ݟ௨͠΋Α͍ؾ͕͢Δʂ

    View full-size slide

  17. ςετ͕ࣦഊͨ࣌͠
    17
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ͔͠͠

    View full-size slide

  18. 18
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ςετ͕ࣦഊͨ࣌͠

    View full-size slide

  19. 19
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ςετ͕ࣦഊͨ࣌͠
    Τϥʔ͕ू໿͞Εͯ͠·͏

    View full-size slide

  20. 20
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ςετ͕ࣦഊͨ࣌͠
    ཧ૝Λݴ͑͹ɺ͜ͷߦʹग़ͯཉ͍͠

    View full-size slide

  21. 9$5FTUͰ
    ΤϥʔදࣔͷҐஔ͸ࢦఆͰ͖Δ
    21
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ࣮͸

    View full-size slide

  22. 22
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ಠࣗͷΞαʔγϣϯ
    func assertEmpty(_ s: String,
    file: StaticString = #file,
    line: UInt = #line) {
    XCTAssert(s.isEmpty, file: file, line: line)
    }
    class AssertTests: XCTestCase {
    func testEmpty() {
    assertEmpty("Swift")
    }
    }

    View full-size slide

  23. 23
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ಠࣗͷΞαʔγϣϯ
    func assertEmpty(_ s: String,
    file: StaticString = #file,
    line: UInt = #line) {
    XCTAssert(s.isEmpty, file: file, line: line)
    }
    class AssertTests: XCTestCase {
    func testEmpty() {
    assertEmpty("Swift")
    }
    }
    ۭจࣈྻͰ͋Δ͜ͱΛݕূ

    View full-size slide

  24. 24
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ಠࣗͷΞαʔγϣϯ
    func assertEmpty(_ s: String,
    file: StaticString = #file,
    line: UInt = #line) {
    XCTAssert(s.isEmpty, file: file, line: line)
    }
    class AssertTests: XCTestCase {
    func testEmpty() {
    assertEmpty("Swift")
    }
    }
    ݺͼग़͠ݩͷϑΝΠϧɾߦ਺Λऔಘ

    View full-size slide

  25. 25
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ಠࣗͷΞαʔγϣϯ
    func assertEmpty(_ s: String,
    file: StaticString = #file,
    line: UInt = #line) {
    XCTAssert(s.isEmpty, file: file, line: line)
    }
    class AssertTests: XCTestCase {
    func testEmpty() {
    assertEmpty("Swift")
    }
    }
    Ҿ਺Ͱ౉͢

    View full-size slide

  26. 26
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ಠࣗͷΞαʔγϣϯ
    func assertEmpty(_ s: String,
    file: StaticString = #file,
    line: UInt = #line) {
    XCTAssert(s.isEmpty, file: file, line: line)
    }
    class AssertTests: XCTestCase {
    func testEmpty() {
    assertEmpty("Swift")
    }
    }
    ࣦഊ͢ΔσʔλͰ࣮ߦ͢Δͱɻɻ

    View full-size slide

  27. 27
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ಠࣗͷΞαʔγϣϯ
    ݺͼग़͠ݩʹΤϥʔදࣔ͞Εͨʂ

    View full-size slide

  28. 28
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ಠࣗͷΞαʔγϣϯ
    ݺͼग़͠ݩʹΤϥʔදࣔ͞Εͨʂ
    ͜ΕͰվળͰ͖ͦ͏ͩͧʂ

    View full-size slide

  29. 1BSBNFUFSJ[FE5FTU
    Λվળͯ͠ΈΔ
    29
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

    View full-size slide

  30. 30
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    վળ൛ Parameterized Test
    class ParamTests: XCTestCase {
    func testParams() {
    let tests: [(line: UInt, args: Int, expect: String)] = [
    (#line, 1, "1"),
    (#line, 2, "2"),
    (#line, 3, "Fizz"),
    (#line, 4, "4"),
    (#line, 5, "Buzz"),
    (#line, 6, "6"),
    ]
    for (line, args, expect) in tests {
    let result = fizzBuzz(args)
    XCTAssertEqual(expect, result, line: line)
    }
    }
    }

    View full-size slide

  31. 31
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    վળ൛ Parameterized Test
    class ParamTests: XCTestCase {
    func testParams() {
    let tests: [(line: UInt, args: Int, expect: String)] = [
    (#line, 1, "1"),
    (#line, 2, "2"),
    (#line, 3, "Fizz"),
    (#line, 4, "4"),
    (#line, 5, "Buzz"),
    (#line, 6, "6"),
    ]
    for (line, args, expect) in tests {
    let result = fizzBuzz(args)
    XCTAssertEqual(expect, result, line: line)
    }
    }
    }
    ֤λϓϧʹMJOFΛ௥Ճͯ͠

    View full-size slide

  32. 32
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    վળ൛ Parameterized Test
    class ParamTests: XCTestCase {
    func testParams() {
    let tests: [(line: UInt, args: Int, expect: String)] = [
    (#line, 1, "1"),
    (#line, 2, "2"),
    (#line, 3, "Fizz"),
    (#line, 4, "4"),
    (#line, 5, "Buzz"),
    (#line, 6, "6"),
    ]
    for (line, args, expect) in tests {
    let result = fizzBuzz(args)
    XCTAssertEqual(expect, result, line: line)
    }
    }
    }
    "TTFSUJPOؔ਺ʹ౉͢

    View full-size slide

  33. 33
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    վળ൛ Parameterized Test

    View full-size slide

  34. 34
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    վળ൛ Parameterized Test
    ࣦഊͨ͠Ґஔ͕෼͔Γ΍͍͢ʂ
    ͜Ε͕ސ٬ͷٻΊ͍ͯͨ΋ͷ

    View full-size slide

  35. MJOF͢Βॻ͖ͨ͘ͳ͍
    35
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ސ٬͸

    View full-size slide

  36. MJOF͢Βॻ͖ͨ͘ͳ͍
    36
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ސ٬͸
    OSS Խɺܾఆʂ

    View full-size slide

  37. 37
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ๻ͷߟ͑ͨ࠷ڧͷ Parameterized Test
    class ParamTests: XCTestCase {
    func testFizzBuzz() {
    assert(fizzBuzz).forAll([
    when(1, then: "1"),
    when(2, then: "2"),
    when(3, then: "Fizz"),
    when(4, then: "4"),
    when(5, then: "Buzz"),
    when(6, then: "6"),
    ])
    }
    }

    View full-size slide

  38. 38
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ๻ͷߟ͑ͨ࠷ڧͷ Parameterized Test
    class ParamTests: XCTestCase {
    func testFizzBuzz() {
    assert(fizzBuzz).forAll([
    when(1, then: "1"),
    when(2, then: "2"),
    when(3, then: "Fizz"),
    when(4, then: "4"),
    when(5, then: "Buzz"),
    when(6, then: "6"),
    ])
    }
    }
    ݕূର৅ͷؔ਺Λࢦఆͯ͠

    View full-size slide

  39. 39
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ๻ͷߟ͑ͨ࠷ڧͷ Parameterized Test
    class ParamTests: XCTestCase {
    func testFizzBuzz() {
    assert(fizzBuzz).forAll([
    when(1, then: "1"),
    when(2, then: "2"),
    when(3, then: "Fizz"),
    when(4, then: "4"),
    when(5, then: "Buzz"),
    when(6, then: "6"),
    ])
    }
    }
    ݕূ͢Δೖྗ஋ɾظ଴஋Λྻڍ

    View full-size slide

  40. 40
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ๻ͷߟ͑ͨ࠷ڧͷ Parameterized Test

    View full-size slide

  41. 41
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ๻ͷߟ͑ͨ࠷ڧͷ Parameterized Test
    https://github.com/
    YusukeHosonuma/SwiftParamTest
    ΂ɺผʹελʔ͕ཉ͍͠Θ͚͡Ό
    ͳ͍Μ͔ͩΒͶ///

    View full-size slide

  42. ·ͱΊ
    42
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

    View full-size slide

  43. 43
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.
    ·ͱΊ
    • 1BSBNFUFSJ[FE5FTU͸σʔλΛྻڍ͢ΔΑ͏ͳ
    ςετίʔυͷॻ͖ํ
    • 9$5FTU4XJGUͰ΋ॻ͚Δ
    • 9$5"TTFSUܥ͸ΤϥʔҐஔΛ੍ޚͰ͖Δ
    • Α͔ͬͨΒ044࢖ͬͯΈͯͶʂ
    ΞΠσΟΞͱ͔΋ืूʂɹ5XJUUFS!UPCJ

    View full-size slide

  44. 1BSBNFUFSJ[FEUFTU
    CFHJOOJOHXJUI
    4XJGU1BSBN5FTU
    44
    Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

    View full-size slide