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

ちゃんと実装してちゃんとテストしよう / PyCon mini Hiroshima 2019

7pairs
October 12, 2019

ちゃんと実装してちゃんとテストしよう / PyCon mini Hiroshima 2019

7pairs

October 12, 2019
Tweet

More Decks by 7pairs

Other Decks in Technology

Transcript

  1. ͪΌΜͱ࣮૷ͯ͠ 

    ͪΌΜͱςετ͠Α͏
    2019-10-12 (PyCon mini Hiroshima 2019)

    ௕୩৔ ५໵

    View full-size slide

  2. ࣗݾ঺հ
    • ௕୩৔ ५໵ (͸ͤ͹ ͡ΎΜ΍)
    • ֤छSNSͰ͸ 7pairs Ͱ׆ಈத
    • ޷͖ͳେࡕۅӂ͸தଜ߶໵ / ޷͖ͳ෋࢜େֶ͸֎࡚मଡ
    • ͓࢓ࣄͰ͸Android (Java / Kotlin) / iOS (Objective-C / Swift) ͕த৺
    • ͋·ΓҰൠతͱ͸ݴ͑ͳ͍Pythonͷ࢖͍ํΛ໛ࡧ͢Δͷ͕େ޷͖
    • PyCon JPͰAltJS (2018) / Android (2019) ʹ͍ͭͯൃද
    • https://speakerdeck.com/7pairs

    View full-size slide

  3. ޿ౡͰͷొஃ (4೥ͿΓ2ճ໨)
    • ৽ਓڭҭͱPython (2015)
    • https://speakerdeck.com/7pairs/pycon-mini-
    hiroshima-2015
    • ͪΌΜͱ࣮૷ͯͪ͠ΌΜͱςετ͠Α͏ (←New!)
    • https://speakerdeck.com/7pairs/pycon-mini-
    hiroshima-2019

    View full-size slide

  4. ஶॻ
    Chalice, Transcrypt, pytest, SeleneΛར༻ͯ͠

    αʔόʔϨεΞϓϦΛ࣮૷ɾςετ͢ΔຊͰ͢
    AmazonଞͰઈࢍൢചதʂ

    View full-size slide

  5. ͜ͷηογϣϯͰ࿩͢͜ͱ
    • ͪΌΜͱςετ͢Δͱ͸Ͳ͏͍͏͜ͱ͔ʁ
    • ͪΌΜͱςετ͢ΔͨΊʹ 

    Ͳ͏࣮૷͢΂͖͔ʁ

    View full-size slide

  6. ͜ͷηογϣϯͰ࿩͞ͳ͍͜ͱ
    • ݸผͷςετπʔϧͷৄࡉʹ͍ͭͯ
    • ݸผͷCI / CDπʔϧͷৄࡉʹ͍ͭͯ

    View full-size slide

  7. ͜ͷηογϣϯͷΰʔϧ
    • ιϑτ΢ΣΞςετʹ͍ͭͯ 

    ཧղ͍ͯͨͩ͘͠
    • ඼࣭ʹର͢ΔҙࣝΛߴΊ͍ͯͨͩ͘
    • ࠙਌ձͰࢲ͕΅ͬͪʹͳΒͳ͍

    View full-size slide

  8. ձ৔ͷΈͳ͞·ʹ࣭໰Ͱ͢

    View full-size slide

  9. ීஈςετΛ͍ͯ͠ͳ͍ํ

    View full-size slide

  10. ීஈςετΛ͍ͯ͠Δํ

    View full-size slide

  11. ͋ͳͨͷ΍͍ͬͯΔ͜ͱ͸

    ຊ౰ʹςετͳͷͰ͠ΐ͏͔ʁ

    View full-size slide

  12. ςετ (Testing)
    શͯͷϥΠϑαΠΫϧΛ௨࣮ͯ͡ࢪ͢Δ੩తɺಈ
    తͳϓϩηεʹ͓͍ͯɺ੒Ռ෺͕ಛఆͷཁ݅Λຬ
    ଍͢Δ͔Λ൑ఆ͠ɺ໨తʹ߹க͢Δ͜ͱΛ࣮ূ
    ͠ɺܽؕΛݟ͚ͭΔͨΊɺιϑτ΢ΣΞϓϩμΫ
    τ΍ؔ࿈੒Ռ෺ʹର͠ɺܭըɺ४උɺධՁΛ͢Δ
    ͜ͱɻ
    ——ʰISTQBιϑτ΢ΣΞςετඪ४༻ޠू (೔ຊޠ൛)ʱΑΓ

    View full-size slide

  13. ͪΌΜͱ໨త͕Ռ͍ͨͤͯΔ͔ʁ
    • ςετίʔυΛॻ͍͔ͨΒͱ͍ͬͯ 

    ඞͣ͠΋ͦΕ͕ςετʹͳΔΘ͚Ͱ͸ͳ͍
    • ը໘Λૢ࡞ͯ͠ಈ࡞Λ֬ೝ͔ͨ͠Βͱ͍ͬͯ 

    ඞͣ͠΋ͦΕ͕ςετʹͳΔΘ͚Ͱ͸ͳ͍

    View full-size slide

  14. ςετϨϕϧ

    View full-size slide

  15. ςετϨϕϧ (Test level)
    ܥ౷తʹ·ͱΊɺ؅ཧ͍ͯ͘͠ςετͷ׆ಈͷ
    άϧʔϓɻ֤ςετϨϕϧ͸ϓϩδΣΫτͷಛఆ
    ͷ੹຿ͱରԠ෇͚͕Ͱ͖ΔɻςετϨϕϧͷྫ
    ʹ͸ɺίϯϙʔωϯτςετɺ౷߹ςετɺγ
    εςϜςετɺड͚ೖΕςετ͕͋Δɻ
    ——ʰISTQBιϑτ΢ΣΞςετඪ४༻ޠू (೔ຊޠ൛)ʱΑΓ

    View full-size slide

  16. ίϯϙʔωϯτςετ (Component testing)
    • ݸʑͷιϑτ΢ΣΞίϯϙʔωϯτͷςετɻ 

    (ISTQB༻ޠू)
    • Ϣχοτςετ / ୯ମςετͱ 

    ݺ͹ΕΔ͜ͱ͕ଟ͍
    • ίʔυͷ։ൃ୲౰ऀ͕ߦ͏͜ͱ͕Ұൠత

    View full-size slide

  17. ౷߹ςετ (Integration testing)
    • ౷߹ͨ͠ίϯϙʔωϯτ΍γεςϜͷ 

    ΠϯλʔϑΣʔε΍૬ޓ࡞༻ͷܽؕΛ 

    ఠग़͢ΔͨΊͷςετɻ (ISTQB༻ޠू)
    • ݸʑͷίϯϙʔωϯτͷػೳͰ͸ͳ͘ 

    ίϯϙʔωϯτؒͷίϛϡχέʔγϣϯʹ 

    য఺Λ౰ͯΔඞཁ͕͋Δ

    View full-size slide

  18. γεςϜςετ (System testing)
    • ౷߹͞ΕͨγεςϜ͕ɺಛఆͷཁ݅Λ 

    ຬͨ͢͜ͱΛ࣮ূ͢ΔͨΊͷςετͷ 

    ϓϩηεɻ (ISTQB༻ޠू)
    • ΤϯυπʔΤϯυͷશମͷৼΔ෣͍Λ֬ೝ͢Δ
    • ػೳͱඇػೳͷ྆ํΛ֬ೝ͢Δ

    View full-size slide

  19. ड͚ೖΕςετ (Acceptance testing)
    • γεςϜ͕ɺϢʔβͷχʔζɺཁ݅ɺ 

    ϏδωεɾϓϩηεΛຬ଍͢Δ͔ΛνΣοΫ 

    ͢ΔͨΊͷެࣜͳςετɻ (ISTQB༻ޠू)
    • ϢʔβʔʹΑͬͯߦΘΕΔ͜ͱ͕ଟ͍
    • ຊ൪؀ڥ·ͨ͸ͦΕʹ͍ۙ؀ڥͰߦ͏

    View full-size slide

  20. ΞδϟΠϧ։ൃʹ

    ͜ͷϞσϧ͸౰ͯ͸·Βͳ͍ͷͰ͸

    View full-size slide

  21. ΞδϟΠϧͩͬͯมΘΒͳ͍
    • ܭըΑΓมԽ΁ͷదԠʹॏ͖Λஔ͖ 

    υΩϡϝϯτΑΓಈ͘ίʔυΛॏࢹ͢Δ͕ 

    ઃܭΛ͠ͳ͍։ൃख๏Ͱ͸ͳ͍
    • ҰճͷΠςϨʔγϣϯͷதͰ͸ 

    VࣈϞσϧʹ͍ۙܗ͕Ͱ͖͕͋Δ͸ͣ

    View full-size slide

  22. ಈతςετ / ੩తςετ

    View full-size slide

  23. ಈతςετ (Dynamic testing)
    ίϯϙʔωϯτ΍γεςϜͷιϑτ΢ΣΞΛ࣮
    ߦͤͯ֬͞ೝ͢Δςετɻ
    ——ʰISTQBιϑτ΢ΣΞςετඪ४༻ޠू (೔ຊޠ൛)ʱΑΓ

    View full-size slide

  24. ੩తςετ (Static testing)
    ιϑτ΢ΣΞ։ൃͷ੒Ռ෺ (ཁ݅ɺઃܭɺຢ͸ɺ
    ίʔυͳͲ) ͷ࣮ߦΛͤͣʹ࣮ࢪ͢Δ੒Ռ෺ͷς
    ετɻͨͱ͑͹ɺϨϏϡʔɺ੩తղੳͳͲɻ
    ——ʰISTQBιϑτ΢ΣΞςετඪ४༻ޠू (೔ຊޠ൛)ʱΑΓ

    View full-size slide

  25. ੩తςετͷྫ
    • ੩తղੳπʔϧΛར༻ͯ͠ 

    ιʔείʔυΛධՁ͢Δ
    • ઃܭॻΛ࡞੒ऀͱผͷਓ͕ؒϨϏϡʔ͠ 

    ޡΓͷࢦఠɺվળͷఏҊΛߦ͏
    • ιʔείʔυΛ࡞੒ऀͱผͷਓ͕ؒϨϏϡʔ͠ 

    ޡΓͷࢦఠɺվળͷఏҊΛߦ͏

    View full-size slide

  26. ੩తςετͷಛ௃
    • ϓϩάϥϜΛಈ͔͢ඞཁ͕ͳ͍ͨΊ 

    ૣ͍ஈ֊Ͱߦ͏͜ͱ͕Ͱ͖Δ
    • อकੑʹؔ͢Δܽؕ (Մಡੑɺ࠶ར༻ੑ౳) ͷ 

    ݕग़͕Ͱ͖Δ
    • ܽؕʹΑͬͯҾ͖ى͜͞ΕͨނোͰ͸ͳ͘ 

    ܽؕͦͷ΋ͷΛݕग़͢Δ

    View full-size slide

  27. ͱ͜ΖͰ͓ؾ͖ͮʹͳΓ·͔ͨ͠ʁ

    View full-size slide

  28. PyCon miniͳͷʹPython͕
    ग़͖͍ͯͯͳ͍ͷͰ͸

    View full-size slide

  29. ͪΌΜͱ࣮૷͢Δ

    View full-size slide

  30. ςελϒϧͳ࣮૷Λ৺͕͚Δ
    • ςετΛҙ࣮ࣝͯ͠૷͢Δ
    • ଞͷηΦϦʔͱ૬൓͢Δ͜ͱ΋͋Δ
    • ઃܭ͕৑௕ʹͳΔՄೳੑ͕͋Δ
    • PythonicͰͳ͍࣮૷ʹͳΔՄೳੑ͕͋Δ

    View full-size slide

  31. ঢ়گʹΑͬͯมԽ͢Δ஋͸

    ֎෦͔Βड͚औΔ

    View full-size slide

  32. վળલ
    import datetime
    # 結果が実行タイミングに依存する
    def is_saturday():
    today = datetime.date.today()
    return today.weekday() == 5
    if is_saturday():
    print('Hello, PyCon mini Hiroshima!')

    View full-size slide

  33. վળޙ
    import datetime
    # 同じ引数を渡せば同じ結果が返る
    def is_saturday(target):
    return target.weekday() == 5
    if is_saturday(datetime.date.today()):
    print('Hello, PyCon mini Hiroshima!')

    View full-size slide

  34. ґଘੑΛ֎෦͔Β஫ೖ͢Δ

    View full-size slide

  35. վળલ
    class Weather:
    def __init__(self):
    self._api = WeatherApi()
    # テストを実施するたびに
    # APIへのアクセスが発生する
    def get_celsius(self):
    return self._api.get_celsius()

    View full-size slide

  36. վળޙ
    class Weather:
    def __init__(self, api):
    self._api = api
    # テストを実施するたびに
    # 引数で渡されたオブジェクトへの
    # アクセスが発生する
    def get_celsius(self):
    return self._api.get_celsius()

    View full-size slide

  37. ελϒ / ϞοΫ͕ར༻Մೳʹ
    import unittest
    class DummyWeatherApi(WeatherApi):
    def get_celsius(self):
    return 33.4
    class SampleTest(unittest.TestCase):
    def test_sample(self):
    weather = Weather(DummyWeatherApi())
    # get_celsiusに対するテストを実施

    View full-size slide

  38. ͪΌΜͱςετ͢Δ

    View full-size slide

  39. ԿΛ୲อ͍ͨ͠ͷ͔໌֬ʹ͢Δ
    • Must / Want / NeverʹԊͬͨ֬ೝΛ͢Δ
    • MustɿͰ͖ͳ͚Ε͹͍͚ͳ͍͜ͱ
    • WantɿͰ͖͍ͯΔͱخ͍͜͠ͱ
    • Neverɿىͬͯ͜͸͍͚ͳ͍͜ͱ

    View full-size slide

  40. ڥք஋Λҙࣝͯ͠ςετ͢Δ
    • ೖྗ஋ͷڥքͷલޙͷ஋Ͱςετ͢Δ
    • ίϯϙʔωϯτςετͷجຊͱͳΔٕ๏
    • ࣗવͱΧόϨοδ্͕͕Δ͸ͣ

    View full-size slide

  41. ϒϥοΫϘοΫεςετ΋ඞཁ
    • ౷߹ςετҎ߱ͷςετͰ͸ 

    ϒϥοΫϘοΫεςετ͕த৺ͱͳΔ
    • ςετର৅ͷೖྗͱग़ྗʹண໨͠ 

    ಺෦ߏ଄ͷৄࡉ͸ҙࣝ͠ͳ͍

    View full-size slide

  42. ϋοϐʔύεΛҙࣝ͢Δ
    • ϋοϐʔύεͷςετ͚ͩͰ͸ࠔΔ͕ 

    γεςϜͷྲྀΕΛ௥͏ʹ͸༗༻
    • γεςϜςετͷϋοϐʔύε͸ 

    యܕతͳϢʔεέʔεʹ΋ͳΔ

    View full-size slide

  43. ؍఺࿙ΕΛ๷͙ͨΊʹ
    • ͋ͳͨͷίʔυ͕ɺࣗ෼ͷݴ༿Λ࿩͞ͳ͍ਓʹ
    ಡ·Εͳ͍ͱ֬৴͍ͯ͠ͳ͚Ε͹ɺίϝϯτΛ
    ӳޠͰॻ͘Α͏ʹ͓ئ͍͠·͢ɻ (PEP 8ΑΓ)
    • ͋ͳͨͷςετίʔυ͕ɺӳޠͰࢥߟͰ͖Δਓ
    ʹ͔͠ಡ·Εͳ͍ͱ֬৴͍ͯ͠ͳ͚Ε͹ɺςε
    τϝιου໊Λ೔ຊޠͰॻ͘Α͏ʹ͓ئ͍͠·
    ͢ɻ (ॾઆ͋Γ·͢)

    View full-size slide

  44. Python 2͸ؔ਺໊ʹ

    ASCII͔͠࢖͑ͳ͍ͷͰ͸

    View full-size slide

  45. ࣗಈςετ

    View full-size slide

  46. ࣗಈςετʹ͍ͭͯ
    • աڈͷPyCon JPͰ 

    ૉ੖Β͍͠ൃද͕ͨ͘͞Μ͋Γ·͢
    • https://www.youtube.com/user/PyConJP/
    search?query=ςετ

    View full-size slide

  47. ςετϐϥϛου

    View full-size slide

  48. ιϑτ΢ΣΞςετͷ7ݪଇ

    View full-size slide

  49. 1. ςετ͸͕ܽؕ͋Δ͜ͱ͸ࣔͤΔ͕

    ͕ܽؕͳ͍͜ͱ͸ࣔͤͳ͍
    • ιϑτ΢ΣΞʹނো͕ݟ͔ͭͬͨ৔߹
    • ͦͷιϑτ΢ΣΞʹ͸͕ܽؕ͋Δ
    • ιϑτ΢ΣΞʹނো͕ݟ͔ͭΒͳ͔ͬͨ৔߹
    • ͦͷιϑτ΢ΣΞʹ͕ܽؕͳ͍ͱ͸ݶΒͳ͍
    • ࣮ࢪͨ͠ςετͰ͸ͨ·ͨ· 

    ܽؕΛݟ͚ͭΒΕͳ͔͚͔ͬͨͩ΋͠Εͳ͍

    View full-size slide

  50. 2. શ਺ςετ͸ෆՄೳ
    • ͢΂ͯͷೖྗͱ͢΂ͯͷ৚݅ͷ૊Έ߹Θͤ͸ 

    ແ਺ʹଘࡏ͢Δ
    • ༗ݶͷ࣌ؒͰ͢΂ͯͷύλʔϯΛ 

    ςετ͢Δ͜ͱ͸ෆՄೳ
    • ςετൣғΛॏཁͳ৔ॴʹߜΔ
    • ςετʹ༏ઌॱҐΛ͚ͭΔ

    View full-size slide

  51. 3. ૣظςετͰ࣌ؒͱίετΛઅ໿
    • ܽؕΛݟ͚ͭΔͷ͕஗͘ͳΔ΄Ͳ 

    मਖ਼ͷίετ͕େ͖͘ͳΔ
    • ݪҼͷಛఆʹ͕͔͔࣌ؒΔ
    • ผͷػೳ͔Βࢀর͞Ε͍ͯͯमਖ਼ͮ͠Β͍
    • ܽؕΛૣ͘ݟ͚ͭΔͨΊ 

    ։ൃαΠΫϧͷૣ͍࣌ظʹςετΛߦ͏

    View full-size slide

  52. 4. ܽؕͷภࡏ
    • ιϑτ΢ΣΞͷ͢΂ͯͷػೳ͕ 

    ಉ͡Α͏ʹ࡞ΒΕ͍ͯΔΘ͚Ͱ͸ͳ͍
    • ઃܭ΍࣮૷ͷ೉қ౓͕ҧ͏
    • ࿈ܞ͕ඞཁͳ֎෦ͱͷΠϯλʔϑΣΠε͕ҧ͏
    • ܽؕ͸ۉҰʹ෼෍ͤͣʹಛఆͷػೳʹूத͠΍͍͢
    • ύϨʔτͷ๏ଇ

    View full-size slide

  53. 5. ࡴ஬ࡎͷύϥυοΫεʹ͝༻৺
    • ಉ͡ιϑτ΢ΣΞʹରͯ͠ಉ͡ςετΛ 

    ܁Γฦ͍ͯ͠Δͱ͍ͣΕܽؕΛൃݟͰ͖ͳ͘ͳΔ
    • ఆظతʹςετͷมߋ΍௥ՃΛߦ͏
    • ͨͩ͠ϦάϨογϣϯςετͰ͸ 

    ಉ͡ςετΛ܁Γฦ͢͜ͱ͕༗༻

    View full-size slide

  54. 6. ςετ͸ঢ়گ࣍ୈ
    • ιϑτ΢ΣΞͷੑ࣭ʹΑͬͯ 

    ॏ఺తʹςετ͢Δ಺༰͕มΘͬͯ͘Δ
    • μ΢ϯͯ͠͸ࠔΔγεςϜͳͷͰ 

    ෛՙςετΛॏ఺తʹߦ͏
    • ͓ۚΛѻ͏γεςϜͳͷͰ 

    ηΩϡϦςΟपΓΛॏ఺తʹςετ͢Δ
    • ͦͷػೳ͕ଘࡏ͠ͳ͍͜ͱ͕Ϗδωε্ͷϦεΫͳͷͰ 

    ςετͷ࣌ؒΛ୹Ίʹͯ͠ϦϦʔεͯ͠͠·͏

    View full-size slide

  55. 7. ʮόάθϩʯͷམͱ݀͠
    • పఈతʹςετΛ࣮ࢪͯ͠ 

    ͕ܽؕݟ͔ͭΒͳ͘ͳͬͯ΋ 

    ͦΕͰ100఺ͷιϑτ΢ΣΞʹͳΔΘ͚Ͱ͸ͳ͍
    • ࢖͍ʹ͍͘γεςϜ
    • ϢʔβʔͷχʔζΛຬͨ͞ͳ͍γεςϜ

    View full-size slide

  56. ͞Βʹਂֶ͘ͿͨΊʹ

    View full-size slide

  57. ςελʔͪΌΜ
    ৽ਓ͞Μ͔ΒΘ͔Δ 

    ιϑτ΢ΣΞςετղઆϚϯΨ
    ιϑτ΢ΣΞςετͷجૅΛ 

    ອըͰ෼͔Γ΍͘͢ղઆ
    Webອը / ಉਓࢽ / Kindle

    View full-size slide

  58. ॳΊͯͷࣗಈςετ
    ࣗಈςετͷߟ͑ํͱϑϨʔϜϫʔΫΛ 

    1͔Βղઆʂ (ॻ੶ଳΑΓ)
    WebγεςϜͷࣗಈςετΛ୊ࡐʹ 

    ߟ͑ํ΍ϑϨʔϜϫʔΫʹ͍ͭͯղઆ
    Jonathan Rasmusson ஶ / ۄ઒ ߛࢠ ༁ 

    (ΦϥΠϦʔδϟύϯ)

    View full-size slide

  59. ϚΠϯυϚοϓ͔Β࢝ΊΔιϑτ΢ΣΞςετ
    ؍఺Λ૑଄͠ɺ޿͛ɺਂΊΔ (ॻ੶ଳΑΓ)
    ϚΠϯυϚοϓΛ࢖༻ͨ͠ 

    ςετͷܭըɺઃܭɺใࠂͳͲͷख๏Λ 

    ࣮ྫͱͱ΋ʹ෼͔Γ΍͘͢ղઆ
    ஑ా ڿ, ླ໦ ࡾل෉ ஶ (ٕज़ධ࿦ࣾ)

    View full-size slide

  60. ςετۦಈPython
    pytestΛ࢖ͬͯPythonͰ 

    ςετۦಈ։ൃ͢ΔͨΊͷશ஌ࣝ (දࢴΑΓ)
    pytestΛར༻ͨ͠ςετͷॻ͖ํ΍ 

    ςετۦಈ։ൃͷਐΊํΛղઆ
    Brian Okken ஶ / ҆Ҫ ྗ ؂म 

    גࣜձࣾΫΠʔϓ ؂༁ (ᠳӭࣾ)

    View full-size slide

  61. ·ͱΊ
    • ίϯϙʔωϯτςετ͚͕ͩςετͰ͸ͳ͍
    • ඼࣭ͷͨΊʹҙࣝ͢Δͱ͜Ζ͸ͨ͘͞Μ͋Δ
    • ౰ͨΓલͷ͜ͱΛ౰ͨΓલʹ΍Ζ͏
    • ςετ͸࣮૷ͷޙ޻ఔͰ͸ͳ͍

    View full-size slide

  62. ςετʹ͍ͭͯཧղͰ͖·͔ͨ͠ʁ

    View full-size slide

  63. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide