Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
UnitTest Anti-Pattern
Search
Shoma Suzuki
January 30, 2018
Technology
2
1.8k
UnitTest Anti-Pattern
こんなUnitTest はイヤだ
~ 対策を添えて ~
Shoma Suzuki
January 30, 2018
Tweet
Share
More Decks by Shoma Suzuki
See All by Shoma Suzuki
Feedback Loops in Development
shomas
1
530
品質への取り組み
shomas
2
5.7k
メルカリにおける Software Engineer in Test
shomas
5
8.8k
Other Decks in Technology
See All in Technology
サーバーレスで楽しよう!お気軽に始められる3つのポイント / Have fun with Serverless!
_kensh
2
230
パフォーマンスとコスト改善のために法人データ分析基盤をBigQueryに移行した話
seiya303
1
100
AWSエンジニアに捧ぐLangChainの歩き方
tsukuboshi
0
220
RevOpsへ至る道 データ活用による事業革新への挑戦 / path-to-revops
pei0804
3
800
企業テックブログにおける執筆ネタの考え方・見つけ方・広げ方 / How to Think of, Find, and Expand Writing Topics for Corporate Tech Blogs
honyanya
0
810
SREとしてスタッフエンジニアを目指す / SRE Kaigi 2025
tjun
15
6.5k
Skip Skip Run Run Run ♫
temoki
0
360
オーティファイ会社紹介資料 / Autify Company Deck
autifyhq
10
120k
CNAPPから考えるAWSガバナンスの実践と最適化
nrinetcom
PRO
1
330
Autify Company Deck
autifyhq
2
41k
Creative Pair
kawaguti
PRO
1
130
一人から始めたSREチーム3年の歩み - 求められるスキルの変化とチームのあり方 - / The three-year journey of the SRE team, which started all by myself
vtryo
7
5.7k
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
52k
Producing Creativity
orderedlist
PRO
343
39k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
11
900
How STYLIGHT went responsive
nonsquared
96
5.3k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Mobile First: as difficult as doing things right
swwweet
222
9.2k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
20
2.4k
Transcript
UnitTest Anti-Pattern 2018/01/30 BPStudy#125 Shoma Suzuki
About Me • Software Engineer in Test at Mercari •
github/twitter -> shoma • ։ൃ/ςετڥΛඋͨ͠Γ • CI ઃఆͨ͠Γ • ςετॻ͍ͨΓ • build Λͨ͠Γ • ٕज़తෛ࠴ͱ͖߹ͬͨΓ
For Disclaimer • xUnit Test Patterns xunitpatterns.com • Unit testing
Anti-patterns catalogue • stackoverflow.com/questions/333682/unit-testing-anti- patterns-catalogue • [༁] joker1007.hatenablog.com/entry/20130709/ 1373365053 • TddAntiPatterns www.hyuki.com/yukiwiki/wiki.cgi?TddAntiPatterns (ݪจ 404 NotFound) ʹ͍͍ͩͨॻ͍ͯ͋Γ·͢
͜ΜͳUnitTest ΠϠͩ ~ ରࡦΛఴ͑ͯ ~
1. Assertion ͕ແ͍ @Test public testSomething() { arrangeCondition(); sut.theMethod(); }
1. Assertion ͕ແ͍ • [ݪҼ] ྫ֎͕ग़ͳ͚Ε͍͍/ಈ͔͚ͩ͢Ͱςετͨͭ͠ Γ • [ରࡦ] PMD
JUnitTestsShouldIncludeAssert pmd.sourceforge.net/pmd-4.3.0/rules/ junit.html#JUnitTestsShouldIncludeAssert • [ରࡦ] phpunit —report-useless-tests phpunit.de/manual/current/en/risky-tests.html • [ରࡦ] Test ͷଥੑΛݕূ͢Δ Mutation Test
2. ։ൃऀͷखݩ͚ͩͰಈ͘ def test_config_exists(): assert os.path.isfile(‘/Users/shoma/.gitconfig’)
2. ։ൃऀͷखݩ͚ͩͰಈ͘ • ιʔείʔυཧ֎ͷϑΝΠϧҙ • /usr/bin ͱ͔ո͍͠ • ڥηοτΞοϓͷқ্͕͕Δ •
127.0.0.1/localhost ҙ͕ඞཁ • [ରࡦ] ڥʹԠͯ͡มΘΔͷڥมΛ͏ • [ରࡦ] ηοτΞοϓؚΊ࣮ͨߦํ๏ͷ౷ҰԽ • [ରࡦ] ૣΊͷ CI • [ରࡦ] ։ൃ/ຊ൪Ұக 12factor.net/ja/dev-prod-parity
3. ςετ͍ͯ͠ΔͷMockͩ class TheService { public Long calculate(Long a, Long
b) { return innerService.calculate(a, b); } } @Test void testCalulate() { InnerService service = mock(InnerService.class): when(service.calculate(19L, 21L)).thenReturn(42L); // ུ Long res = theService.calculate(19L, 21L); assertEquals(LongValueOf(42L), res); }
3. ςετ͍ͯ͠ΔͷMockͩ • [ݪҼ] Mock Λ͍͗ͯ͢͠·͏ • [ରࡦ] CodeCoverageϨϙʔτΛΈΑ͏ •
[ରࡦ] Mutation Test (લड़)
4. ඪ४ػೳͷςετ • Bug͕͋ΔͳΒ͓͍ͬͯͯΑ͍ • ਖ਼نදݱͩͱΓ͕ͪ • ʮTDD ։ൃதͷෆ҆Λίϯτʔϧ͢Δํ๏ͩʯ •
ڍಈͷཧղਂ·Δ • [ݪҼ] ϥΠϒϥϦʹςετ͕ແ͔ͬͨ ˠ OSS ߩݙνϟϯε • [ରࡦ] ͋ͳͨͷίʔυΛςετ͠Α͏
5. ಈ͔͢ͱ՝ۚ͞ΕΔ • [ݪҼ] ΫϥυαʔϏεΛ͍ͬͯΔ • [ݪҼ] Credential ͷѻ͍͕͍͋͠… •
[ରࡦ] SDK ʹ Stub/Fake/Simulator ͕͋Δ͔ ֬ೝ͠Α͏ • [ରࡦ] ͜͜ Mock Խ
6. ͱ͖Ͳ͖ࣦഊ͢Δ • ΜͰ·͢ • [ରࡦ] Vefiry-Act-Vefiry • arrange લͷࣄલ͕݅Ήঢ়ଶͰ͋Δ͔֬ೝ͢Δ
testautomationpatterns.wikispaces.com/VERIFY-ACT-VERIFY • [ରࡦ] ෆ҆ఆͳςετΛmark ͓ͯ͘͠ github.com/nodejs/node/wiki/Flaky-tests • [ରࡦ] TestSize Λنఆͯ͠UnitTest ͱͯ͠ΔൣғΛܾΊΔ testing.googleblog.com/2010/12/test-sizes.html
Happy Unit Testing!