Share
Embed
Swift で ParameterizedTest をやってみた話/swift-parameterized-test
iOS Test Night #10 での発表
https://testnight.connpass.com/event/119381/
Yusuke Hosonuma
April 16, 2019
Transcript
44 slides
-
Copyright (C) DeNA Co.,Ltd. All Rights Reserved. 4XJGUͰ1BSBNFUFSJ[FE5FTU ΛͬͯΈͨ 1
iOS Test Night #10 2019/4/16ʢTueʣ ࡉপ ༞հ@DeNA -
:VTVLF)PTPOVNB • 48&5άϧʔϓ ⁃ ςετࣗಈԽɹJ04(P • 'BWPSJUF ⁃ •
ۙگ ⁃ USZ4XJGU50,:0Ͱొஃ ⁃ വؗʹཱྀߦʹߦ͖ͬͯͨʂ Copyright (C) DeNA Co.,Ltd. All Rights Reserved. 3 @tobi462ʢτϏʣ SwiftCheckͰ࢝ΊΔ Property-based Testing -
7 Copyright (C) DeNA Co.,Ltd. All Rights Reserved. Parameterized-Test •
ࣄલʹςετσʔλΛྻڍ͢Δॻ͖ํ • ςετσʔλͷՃɾআ͕͍͢͠ͱ͍͏ར • +6OJUͰඪ४Ͱαϙʔτ • (PݴޠͰҰൠతͳॻ͖ํ • ςʔϒϧۦಈςετͱݺΕͨΓ -
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 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 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 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 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 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) } } } ςετσʔλՃɾআָ͕ͩ͠ɺ ݟ௨͠Α͍ؾ͕͢Δʂ -
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 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 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 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 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") } } ࣦഊ͢ΔσʔλͰ࣮ߦ͢Δͱɻɻ -
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 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 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ؔʹ͢ -
34 Copyright (C) DeNA Co.,Ltd. All Rights Reserved. վળ൛ Parameterized
Test ࣦഊͨ͠Ґஔ͕͔Γ͍͢ʂ ͜Ε͕ސ٬ͷٻΊ͍ͯͨͷ -
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 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 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"), ]) } } ݕূ͢ΔೖྗɾظΛྻڍ -
41 Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ͷߟ͑ͨ࠷ڧͷ Parameterized
Test https://github.com/ YusukeHosonuma/SwiftParamTest ɺผʹελʔ͕ཉ͍͠Θ͚͡Ό ͳ͍Μ͔ͩΒͶ/// -
43 Copyright (C) DeNA Co.,Ltd. All Rights Reserved. ·ͱΊ •
1BSBNFUFSJ[FE5FTUσʔλΛྻڍ͢ΔΑ͏ͳ ςετίʔυͷॻ͖ํ • 9$5FTU4XJGUͰॻ͚Δ • 9$5"TTFSUܥΤϥʔҐஔΛ੍ޚͰ͖Δ • Α͔ͬͨΒ044ͬͯΈͯͶʂ ΞΠσΟΞͱ͔ืूʂɹ5XJUUFS!UPCJ