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

go1.22からのテストカバレッジとの付き合い方

 go1.22からのテストカバレッジとの付き合い方

ryoji miyamoto

June 18, 2024
Tweet

More Decks by ryoji miyamoto

Other Decks in Technology

Transcript

  1. Copyright © 2015 every, Inc. All rights reserved. 2 •

    ࣗݾ঺հ • ΤϒϦʔͷ঺հ • go1.22͔ΒͷςετΧόϨοδͷ࢓༷มߋ • ͲͷΑ͏ͳٞ࿦͕͋ͬͨͷ͔ɺมΘͬͨഎܠΛ௥͏ • ࣮৘ʹ߹Θͤͯରॲͯ͠ΈΔ • ·ͱΊ ͓඼ॻ͖
  2. Copyright © 2015 every, Inc. All rights reserved. 3 ࣗݾ঺հ

    {
 "name😀": "Ryoji Miyamoto",
 "team👥": "DEV_TTD_SE",
 "birthday🎂": "1994/08/22", "local🏠": "Ѫඤݝീ഼඿ࢢ",
 "career😎": {
 "2017/04": "ઍגࣜձࣾೖࣾ👻",
 "2020/12": "ΤϒϦʔೖࣾ🙌"
 },
 "like❤": [
 "ήʔϜ/Ξχϝ🎮",
 "όΠΫ/Ωϟϯϓ🛵🏕",
 "೔ຊञ🍶"
 ]
 }
  3. Copyright © 2015 every, Inc. All rights reserved. 5 ΤϒϦʔͷύʔύεɺϛογϣϯ

    ໌Δ͍มԽͷੵΈॏͳΔ฻Β͠Λɺ୭ʹͰ΋ɻ ʮwell-beingʢ΢ΣϧϏʔΠϯάʣʯ ͦͷ࣮ݱͷͨΊʹਓੜʹ͓͍ͯ୭΋͕࠷దͳબ୒ΛͰ͖Δ͔Ͳ͏͔͸ॏཁͰ͢ɻ ਐԽ͠ଓ͚ΔςΫϊϩδʔʹΑͬͯΦϯϥΠϯɾΦϑϥΠϯؔ܎ͳ͘ɺ ͞·͟·ͳ৘ใʹ৮ΕΔ͜ͱ͕Ͱ͖Δ࣌୅ɻ ΤϒϦʔ͸ɺσʔλɾAIΛ༻͍ͯ࠷దͳ৘ใΛͱͲ͚Δ͜ͱͰ ೔ʑͷੜ׆ʹ͓͚Δෆ҆΍೰ΈΛղফ͍ͨ͠ͱߟ͍͑ͯ·͢ɻ ࢲ͕ͨͪ໨ࢦ͢ͷ͸ɺਓ͕ຖ೔ΛาΉɺͦͷಓͷΓͷύʔτφʔͰ͋Δ͜ͱɻ Մೳੑ΍ؾ͖ͮΛ఻͑ɺͱ͖ʹ͸ࢥΘ͵ग़ձ͍Ͱɺؾ࣋ͪΛ๛͔ʹ͍ͯ͘͜͠ͱɻ Θͨͨͪ͠ΤϒϦʔ͸ɺ ͻͱΓͻͱΓͷ೔ৗʹɺલ޲͖ͳ͖͔͚ͬΛͱͲ͚͍͖ͯ·͢ɻ ୭ʹͰ΋ɺ໌Δ͍มԽͷੵΈॏͳΔ฻Β͕͠๚ΕΔΑ͏ʹɻ
  4. Copyright © 2015 every, Inc. All rights reserved. 16 ϦϦʔεϊʔτͰͷ͓࿩ͷཁ໿

    • Go 1.22Ҏ߱ɺςετϑΝΠϧͷͳ͍ύοέʔδͰ΋ΧόϨοδ͕දࣔ͞ΕΔ • ؔ਺͕Χόʔ͞Ε͍ͯͳ͍৔߹͸0.0%ѻ͍ • ࣮ߦՄೳͳίʔυ͕શ͘ͳ͍৔߹͸ɺʮςετϑΝΠϧ͕ͳ͍ʯͱใࠂ͞ΕΔ • ex.) ߏ଄ମ΍ΠϯλʔϑΣʔεͷఆٛͷΈͰؔ਺͕ͳ͍৔߹ͳͲ ~ go1.21 go1.22
  5. Copyright © 2015 every, Inc. All rights reserved. 18 ࠓճΧόϨοδ௿ԼͰݕग़͞Εͨ΋ͷ

    • ࣗಈੜ੒ܥͷ΋ͷ • ΤϯυϙΠϯτੜ੒(OpenAPI, GraphQLͷδΣωϨʔλ) • ORM • ҙਤతʹॻ͍ͯͳ͍࣮૷ • ςετ಺༰͕ςετίʔυ࡞੒࣌ͷ࿑ྗʹݟ߹ͬͯͳ͍΋ͷ • ඞཁͳςετ؀ڥ͕ෳࡶ • νʔϜ΍ϓϩδΣΫτن໛ʹΑͬͯ͸Skipͯ͠Δͱ͜Ζ
  6. Copyright © 2015 every, Inc. All rights reserved. 19 •

    GOEXPERIMENT = nocoverageredesignΛ͚ͭΔ • ͨͩ༧ࠂͳ͘͜ͷࢦఆ͸Ͱ͖ͳ͘ͳΔՄೳੑ͸͋ΔͷͰকདྷੑ͸ո͍͠ • ྲྀੴʹҰ࣌͠ͷ͗ͳͷͰΑΖ͘͠ͳ͍ લͱಉ͡ڍಈʹ͢ΔͳΒ… GOEXPERIMENT=nocoverageredesign go test -cover ./...
  7. Copyright © 2015 every, Inc. All rights reserved. 21 •

    cmd/go: go test -cover & go test -coverprofile should always output a coverage #24570 • ΧόϨοδϨϙʔτͷग़ྗํࣜʹ͍ͭͯվળͷ༨஍͋Γͱ͍͏࿩ • ٞ࿦ࣗମ͸2018/03/18͔Β • ౰࣌͸go1.10࣌୅ • Go 1.10 Release Notes • ςετ݁ՌΛΩϟογϡ͢Δ • Ұ౓ʹෳ਺ύοέʔδͷΧόϨοδ͕औΕΔ ←͜͜ ىථ࣌ͷഎܠ
  8. Copyright © 2015 every, Inc. All rights reserved. 22 •

    Ұ౓ʹෳ਺ύοέʔδͷΧόϨοδ͕औΕΔΑ͏ʹͳͬͨ • ͕ͩςετ͕ͳ͍΋ͷ͕ܭ্͞Ε͍ͯͳ͍ • ٯʹҰ౓Ͱ΋ର৅ͷؔ਺͕ςετ͞Ε͍ͯΕ͹ܭ্͞ΕΔ →ΧόϨοδ͸ܭଌൣғʹରͯ͠ͷશମͷׂ߹Λग़͍ͨ͠ Կ͕ٞ࿦ͷର৅ͱͳͬͨͷ͔
  9. Copyright © 2015 every, Inc. All rights reserved. 23 •

    ςετϑΝΠϧͷͳ͍ύοέʔδʹ͍ͭͯΧόϨοδ(0%)Λग़ྗ͢Δ΂͖͔ • ग़ྗͨ͠ํ͕ະςετͷؔ਺͕͋Δ͜ͱ͕෼͔Γ΍͍͢ • ग़ྗ͠ͳ͍ͱ૯߹ΧόϨοδ཰͕ෆࣗવʹԼ͕Δ • ૯߹ΧόϨοδʹςετϑΝΠϧͷͳ͍ύοέʔδΛؚΊΔ΂͖͔ • ؚΊͨํ͕ະςετͷؔ਺͕໌֬ʹͳΔ ٞ࿦͞Ε͍ͯͨ͜ͱ 1/2
  10. Copyright © 2015 every, Inc. All rights reserved. 24 •

    go test -coverͱgo test -coverprofileͰΧόϨοδग़ྗΛ౷Ұ͢Δ΂͖͔ • ౷Ұ͠ͳ͍ͱϢʔβʔʹࠞཚΛট͘Մೳੑ • ౷Ұ͢Δͱཁ݅ʹΑͬͯ͸ෆࣗવͳಈ࡞ʹͳΔՄೳੑ • ςετϑΝΠϧ͕ͳ͍=ະςετͳͷ͔ɺͦΕͱ΋୯ʹςετ͕ॻ͔Ε͍ͯͳ͍͚ͩͳͷ͔ • ະςετͱݟͳ͢΂͖͔ • ίʔυʹԠͯ͡൑அ͕෼͔ΕΔՄೳੑ • [no test files]ͱ0.0%ͷͲͪΒ͕ϢʔβʔʹͱͬͯΘ͔Γ΍͍͔͢ ٞ࿦͞Ε͍ͯͨ͜ͱ 2/2
  11. Copyright © 2015 every, Inc. All rights reserved. 25 •

    ςετϑΝΠϧͷ༗ແʹؔΘΒͣʹશύοέʔδ͕ΧόϨοδϨϙʔτʹؚ·ΕΔΑ͏ʹ • go1.20Ҏ߱Ͱ͸ -coverpkg ϑϥάΛ͚ͭͨͱ͖ͷΈ • go1.22͔Βࢦఆͳ͠΋ద༻͞ΕΔΑ͏ʹͳͬͨ ࠷ऴతʹ
  12. Copyright © 2015 every, Inc. All rights reserved. 27 •

    ҙਤతʹςετͳ͍΋ͷΛΧόϨοδೖΕͨ͘͸ͳ͍ • ஫ࢹ͍ͨ͠ͷ͸ࣗ෼͕࣮ͨͪ૷͍ͯ͠Δ෦෼ • ϏδωεϩδοΫपΓ • ͦ΋ͦ΋ෆཁͳ΋ͷ·Ͱܭଌର৅ʹ͍ͯ͠Δͷ͕ྑ͘ͳ͍ • ࣗಈੜ੒ܥɺҙਤతʹॻ͍ͯͳ͍ͱ͜Ζ • Ҿ਺Ͱର৅pkg͸ࢦఆͯ͠ɺೖΕͨ͘ͳ͍΋ͷ͸আ֎͓ͯ͘͠ • go test -cover ./… → go test -cover pkg1 pkg2 ͜ͷมߋΛ౿·͑ͯ
  13. Copyright © 2015 every, Inc. All rights reserved. 29 ςετϑΝΠϧ͕͋Δ΋ͷ͚ͩநग़͢Δ(ϗϫΠτϦετܕ)

    • ઐ༻ͷநग़εΫϦϓτ͔ΒߜΓࠐΉ • *_test.go͕͋ΔσΟϨΫτϦ͕ର৅ͱ͢Ε͹ࣗಈ ੜ੒ܥ͸ר͖ࠐ·Εͳ͍ • ϝϦοτ • جຊతʹैདྷ௨ΓͷڍಈʹͰ͖Δ • σϝϦοτ • ίʔυશମͷ࣭ΛଌΔ͏͑Ͱ͸Ӆṭ͞ΕΔ #!/bin/bash # ݱࡏͷσΟϨΫτϦ͔Β͢΂ͯͷGoύοέʔδΛݕࡧ for pkg in $(go list ./...); do # ֤ύοέʔδͷσΟϨΫτϦΛऔಘ pkg_dir=$(go list -f '{{.Dir}}' $pkg) # *_test.goϑΝΠϧ͕ͦͷσΟϨΫτϦʹଘࡏ͢Δ͔֬ೝ if [[ $(ls $pkg_dir/*_test.go 2> /dev/null) ]]; then # ଘࡏ͢Δ৔߹͸ύοέʔδ໊Λදࣔ echo $pkg fi done go test -cover $(./listup_test_pkg.sh)
  14. Copyright © 2015 every, Inc. All rights reserved. 30 •

    আ֎ର৅pkgΛҰݩ؅ཧͯ͠ɺςετ࣌ʹআ֎͢Δ • ؅ཧํ๏͸yaml΍json౳ͷ͓޷͖ͳํͰ • pkg໊͕ύλʔϯԽͯ͠ΔͳΒهࡌ͸΋ͬͱָ • ϝϦοτ • আ֎͍ͨ͠΋ͷ͕໌ࣔత • ΧόϨοδܭଌͷҙຯͰ͸ݱ࣮త • σϝϦοτ • আ֎Օॴͷཏྻ͕໘౗Ͱखؒ আ֎͍ͨ͠pkgΛ໊ࢦ͢͠ΔʢϒϥοΫϦετܕʣ exclude_patterns=$(yq e '.ignore[]' testcnf.yml | sed 's/^/-e /' | tr '\n' ' ') go test -cover $(go list ./... | grep -vE $(echo $exclude_patterns)) ignore: - github.com/rymiyamoto/example-api/internal/app/ignore1 - github.com/rymiyamoto/example-api/internal/dashboard/ignore2 - github.com/rymiyamoto/example-api/internal/external/ignore3
  15. Copyright © 2015 every, Inc. All rights reserved. 31 •

    go1.22͔ΒΦϓγϣϯࢦఆͳ͠Ͱ΋ςετͷ༗ແʹؔΘΒͣΧόϨοδ͕ग़ྗ͞ΕΔ • ΧόϨοδΛͲ͏ѻ͍͔ͬͯ͘ͷҙݟ͕༷ʑͳͷͰɺͲ͏͢Δ͔͸֤ࣗͷߟ͑ํ࣍ୈ • ࣗಈੜ੒෺Λ஄͖͍ͨͳΒɺςετର৅ՕॴͷΈΛࢦఆ͢Δඞཁ͕͋Δ ·ͱΊ
  16. Copyright © 2015 every, Inc. All rights reserved. 32 •

    7/2(Ր)ʹOPTiM͞ΜͱͷڞಉͰgoͷษڧձΛ։࠵͠·͢🎉 • https://optim.connpass.com/event/321331/ • ͜Μͳਓʹ͓͢͢Ί • Golang ʹڵຯ͕͋ΔΤϯδχΞ΍ֶੜͷํ • GolangͰͷ։ൃʹैࣄ͞Ε͍ͯΔํ • OPTiM ΍ every ͕ͲͷΑ͏ʹGolangΛ׆༻ͨ͠։ൃΛ͍ͯ͠Δͷ͔ڵຯ͕͋Δํ ΤϒϦʔ͔Βͷ͓஌Βͤ
  17. Copyright © 2015 every, Inc. All rights reserved. 33 ΤϒϦʔ͔Βͷ͓஌Βͤ

    ҰॹʹαʔϏεΛ࡞Δ஥ؒΛେืूதͰ͢ʂ 🔍 ΤϒϦʔ ࠾༻ https://corp.every.tv/recruits • Tech Blog΋΍ͬͯ·͢ • ։ൃ෦ XΞΧ΢ϯτ • ΤϒϦʔެࣜΦ΢ϯυϝσΟΞʮevery.thingʯ͸ͪ͜Β https://tech.every.tv/ https://everything.every.tv/ https://x.com/every_engineer