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.7k
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
490
品質への取り組み
shomas
2
5.6k
メルカリにおける Software Engineer in Test
shomas
5
8.6k
Other Decks in Technology
See All in Technology
生成AIサービスPanorama AIご説明資料
sdt
0
300
Challenges - Open Farming Hackdays 2024
loleg
0
580
これまでのキャリアとこれからMLエンジニアとしてどう動くか
masatakashiwagi
1
790
長文から長文を生成するLLMツールをオープンソースで作ってみた。
tomohisa
2
150
やっていきテスト
k6s4i53rx
0
160
LLMの現在
pfn
PRO
16
6.2k
家族アルバム みてねで直面してきた技術的負債 / MIXI KAG 2024
isaoshimizu
17
7.7k
Terraform v1.7のTest mocking機能の紹介 / Introducing the Test mocking feature of Terraform v1.7
yayoi_dd
1
100
Introducing Pkl
enomotok
0
330
LLMプロダクト事業の立ち上げにおける挑戦
layerx
PRO
7
1.5k
現実世界の事象から学ぶSOLID原則
h0r15h0
25
11k
KubeCon EU 2024 : Knative Maintainers Session
salaboy
0
380
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
6
950
Building an army of robots
kneath
300
41k
Designing Experiences People Love
moore
135
23k
Side Projects
sachag
451
41k
Done Done
chrislema
178
15k
Designing with Data
zakiwarfel
94
4.8k
A Modern Web Designer's Workflow
chriscoyier
689
190k
Pencils Down: Stop Designing & Start Developing
hursman
115
11k
A Tale of Four Properties
chriscoyier
150
22k
Unsuck your backbone
ammeep
661
56k
Designing for Performance
lara
601
67k
Stop Working from a Prison Cell
hatefulcrawdad
265
19k
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!