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/

70d9714ea13fc1133803d61fb16e4160?s=128

Yusuke Hosonuma

April 16, 2019
Tweet

Transcript

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

    iOS Test Night #10 2019/4/16ʢTueʣ 
 ࡉপ ༞հ@DeNA
  2. "CPVU 2 Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

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

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

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

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

  7. 7 Copyright (C) DeNA Co.,Ltd. All Rights Reserved. Parameterized-Test •

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

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

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

  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) } } }
  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) } } } ೖྗ஋ͱظ଴஋ͷλϓϧ
  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) } } } ഑ྻͰςετσʔλΛ༻ҙ
  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) } } } ϧʔϓͰॱ൪ʹݕূ
  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) } } }
  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) } } } ςετσʔλ௥Ճɾ࡟আָ͕ͩ͠ɺ ݟ௨͠΋Α͍ؾ͕͢Δʂ
  17. ςετ͕ࣦഊͨ࣌͠ 17 Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ͔͠͠

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

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

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

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

    ࣮͸
  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") } }
  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") } } ۭจࣈྻͰ͋Δ͜ͱΛݕূ
  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") } } ݺͼग़͠ݩͷϑΝΠϧɾߦ਺Λऔಘ
  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") } } Ҿ਺Ͱ౉͢
  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") } } ࣦഊ͢ΔσʔλͰ࣮ߦ͢Δͱɻɻ
  27. 27 Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ಠࣗͷΞαʔγϣϯ ݺͼग़͠ݩʹΤϥʔදࣔ͞Εͨʂ

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

    ͜ΕͰվળͰ͖ͦ͏ͩͧʂ
  29. 1BSBNFUFSJ[FE5FTU Λվળͯ͠ΈΔ 29 Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

  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) } } }
  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Λ௥Ճͯ͠
  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ؔ਺ʹ౉͢
  33. 33 Copyright (C) DeNA Co.,Ltd. All Rights Reserved. վળ൛ Parameterized

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

    Test ࣦഊͨ͠Ґஔ͕෼͔Γ΍͍͢ʂ ͜Ε͕ސ٬ͷٻΊ͍ͯͨ΋ͷ
  35. MJOF͢Βॻ͖ͨ͘ͳ͍ 35 Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ސ٬͸

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

    OSS Խɺܾఆʂ
  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"), ]) } }
  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"), ]) } } ݕূର৅ͷؔ਺Λࢦఆͯ͠
  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"), ]) } } ݕূ͢Δೖྗ஋ɾظ଴஋Λྻڍ
  40. 40 Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ๻ͷߟ͑ͨ࠷ڧͷ Parameterized

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

    Test https://github.com/ YusukeHosonuma/SwiftParamTest ΂ɺผʹελʔ͕ཉ͍͠Θ͚͡Ό ͳ͍Μ͔ͩΒͶ///
  42. ·ͱΊ 42 Copyright (C) DeNA Co.,Ltd. All Rights Reserved.

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

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

    Reserved.