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

UnitTest Anti-Pattern

Shoma Suzuki
January 30, 2018

UnitTest Anti-Pattern

こんなUnitTest はイヤだ
~ 対策を添えて ~

Shoma Suzuki

January 30, 2018
Tweet

More Decks by Shoma Suzuki

Other Decks in Technology

Transcript

  1. About Me • Software Engineer in Test at Mercari •

    github/twitter -> shoma • ։ൃ/ςετ؀ڥΛ੔උͨ͠Γ • CI ઃఆͨ͠Γ • ςετॻ͍ͨΓ • build Λ௚ͨ͠Γ • ٕज़తෛ࠴ͱ޲͖߹ͬͨΓ
  2. 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)
 
 ʹ͍͍ͩͨॻ͍ͯ͋Γ·͢

  3. 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
  4. 2. ։ൃऀͷखݩ͚ͩͰಈ͘ • ιʔείʔυ؅ཧ֎ͷϑΝΠϧ͸஫ҙ • /usr/bin ͱ͔΋ո͍͠ • ؀ڥηοτΞοϓͷ೉қ౓্͕͕Δ •

    127.0.0.1/localhost ΋஫ҙ͕ඞཁ • [ରࡦ] ؀ڥʹԠͯ͡มΘΔ΋ͷ͸؀ڥม਺Λ࢖͏ • [ରࡦ] ηοτΞοϓ΋ؚΊ࣮ͨߦํ๏ͷ౷ҰԽ • [ରࡦ] ૣΊͷ CI • [ରࡦ] ։ൃ/ຊ൪Ұக 12factor.net/ja/dev-prod-parity
  5. 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); }
  6. 4. ඪ४ػೳͷςετ • Bug͕͋ΔͳΒ΍͓͍ͬͯͯ΋Α͍ • ਖ਼نදݱͩͱ΍Γ͕ͪ • ʮTDD ͸։ൃதͷෆ҆Λίϯτʔϧ͢Δํ๏ͩʯ •

    ڍಈͷཧղ͸ਂ·Δ • [ݪҼ] ϥΠϒϥϦʹςετ͕ແ͔ͬͨ
 ˠ OSS ߩݙνϟϯε • [ରࡦ] ͋ͳͨͷίʔυΛςετ͠Α͏
  7. 5. ಈ͔͢ͱ՝ۚ͞ΕΔ • [ݪҼ] Ϋϥ΢υαʔϏεΛ࢖͍ͬͯΔ • [ݪҼ] Credential ͷѻ͍͕͋΍͍͠… •

    [ରࡦ] SDK ʹ Stub/Fake/Simulator ͕͋Δ͔ ֬ೝ͠Α͏ • [ରࡦ] ͜͜͸ Mock Խ
  8. 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