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
520
品質への取り組み
shomas
2
5.7k
メルカリにおける Software Engineer in Test
shomas
5
8.8k
Other Decks in Technology
See All in Technology
10個のフィルタをAXI4-Streamでつなげてみた
marsee101
0
160
【re:Invent 2024 アプデ】 Prompt Routing の紹介
champ
0
140
宇宙ベンチャーにおける最近の情シス取り組みについて
axelmizu
0
110
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
muziyoshiz
2
2.2k
コンテナセキュリティのためのLandlock入門
nullpo_head
2
320
あの日俺達が夢見たサーバレスアーキテクチャ/the-serverless-architecture-we-dreamed-of
tomoki10
0
430
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
100
[Ruby] Develop a Morse Code Learning Gem & Beep from Strings
oguressive
1
150
AIのコンプラは何故しんどい?
shujisado
1
190
ブラックフライデーで購入したPixel9で、Gemini Nanoを動かしてみた
marchin1989
1
520
LINEスキマニにおけるフロントエンド開発
lycorptech_jp
PRO
0
330
GitHub Copilot のテクニック集/GitHub Copilot Techniques
rayuron
24
11k
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Music & Morning Musume
bryan
46
6.2k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Unsuck your backbone
ammeep
669
57k
Typedesign – Prime Four
hannesfritz
40
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!