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. :VTVLF)PTPOVNB • 48&5άϧʔϓ ⁃ ςετࣗಈԽɹJ04(P • 'BWPSJUF ⁃  •

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

    ࣄલʹςετσʔλΛྻڍ͢Δॻ͖ํ • ςετσʔλͷ௥Ճɾ࡟আ͕͠΍͍͢ͱ͍͏ར఺ • +6OJUͰ͸ඪ४Ͱαϙʔτ • (PݴޠͰ͸Ұൠతͳॻ͖ํ • ςʔϒϧۦಈςετͱݺ͹ΕͨΓ
  3. 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) } } }
  4. 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) } } } ೖྗ஋ͱظ଴஋ͷλϓϧ
  5. 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) } } } ഑ྻͰςετσʔλΛ༻ҙ
  6. 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) } } } ϧʔϓͰॱ൪ʹݕূ
  7. 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) } } }
  8. 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) } } } ςετσʔλ௥Ճɾ࡟আָ͕ͩ͠ɺ ݟ௨͠΋Α͍ؾ͕͢Δʂ
  9. 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") } }
  10. 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") } } ۭจࣈྻͰ͋Δ͜ͱΛݕূ
  11. 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") } } ݺͼग़͠ݩͷϑΝΠϧɾߦ਺Λऔಘ
  12. 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") } } Ҿ਺Ͱ౉͢
  13. 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") } } ࣦഊ͢ΔσʔλͰ࣮ߦ͢Δͱɻɻ
  14. 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) } } }
  15. 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Λ௥Ճͯ͠
  16. 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ؔ਺ʹ౉͢
  17. 34 Copyright (C) DeNA Co.,Ltd. All Rights Reserved. վળ൛ Parameterized

    Test ࣦഊͨ͠Ґஔ͕෼͔Γ΍͍͢ʂ ͜Ε͕ސ٬ͷٻΊ͍ͯͨ΋ͷ
  18. 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"), ]) } }
  19. 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"), ]) } } ݕূର৅ͷؔ਺Λࢦఆͯ͠
  20. 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"), ]) } } ݕূ͢Δೖྗ஋ɾظ଴஋Λྻڍ
  21. 41 Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ๻ͷߟ͑ͨ࠷ڧͷ Parameterized

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

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