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. UnitTest Anti-Pattern
    2018/01/30 BPStudy#125
    Shoma Suzuki

    View Slide

  2. About Me
    • Software Engineer in Test at Mercari
    • github/twitter -> shoma
    • ։ൃ/ςετ؀ڥΛ੔උͨ͠Γ
    • CI ઃఆͨ͠Γ
    • ςετॻ͍ͨΓ
    • build Λ௚ͨ͠Γ
    • ٕज़తෛ࠴ͱ޲͖߹ͬͨΓ

    View Slide

  3. 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)


    ʹ͍͍ͩͨॻ͍ͯ͋Γ·͢


    View Slide

  4. ͜ΜͳUnitTest ͸ΠϠͩ
    ~ ରࡦΛఴ͑ͯ ~

    View Slide

  5. 1. Assertion ͕ແ͍
    @Test

    public testSomething() {
    arrangeCondition();
    sut.theMethod();

    }

    View Slide

  6. 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

    View Slide

  7. 2. ։ൃऀͷखݩ͚ͩͰಈ͘
    def test_config_exists():
    assert os.path.isfile(‘/Users/shoma/.gitconfig’)

    View Slide

  8. 2. ։ൃऀͷखݩ͚ͩͰಈ͘
    • ιʔείʔυ؅ཧ֎ͷϑΝΠϧ͸஫ҙ
    • /usr/bin ͱ͔΋ո͍͠
    • ؀ڥηοτΞοϓͷ೉қ౓্͕͕Δ
    • 127.0.0.1/localhost ΋஫ҙ͕ඞཁ
    • [ରࡦ] ؀ڥʹԠͯ͡มΘΔ΋ͷ͸؀ڥม਺Λ࢖͏
    • [ରࡦ] ηοτΞοϓ΋ؚΊ࣮ͨߦํ๏ͷ౷ҰԽ
    • [ରࡦ] ૣΊͷ CI
    • [ରࡦ] ։ൃ/ຊ൪Ұக 12factor.net/ja/dev-prod-parity

    View Slide

  9. 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);
    }

    View Slide

  10. 3. ςετ͍ͯ͠Δͷ͸Mockͩ
    • [ݪҼ] Mock Λ࢖͍͗ͯ͢͠·͏
    • [ରࡦ] CodeCoverageϨϙʔτΛΈΑ͏
    • [ରࡦ] Mutation Test (લड़)

    View Slide

  11. 4. ඪ४ػೳͷςετ
    • Bug͕͋ΔͳΒ΍͓͍ͬͯͯ΋Α͍
    • ਖ਼نදݱͩͱ΍Γ͕ͪ
    • ʮTDD ͸։ൃதͷෆ҆Λίϯτʔϧ͢Δํ๏ͩʯ
    • ڍಈͷཧղ͸ਂ·Δ
    • [ݪҼ] ϥΠϒϥϦʹςετ͕ແ͔ͬͨ

    ˠ OSS ߩݙνϟϯε
    • [ରࡦ] ͋ͳͨͷίʔυΛςετ͠Α͏

    View Slide

  12. 5. ಈ͔͢ͱ՝ۚ͞ΕΔ
    • [ݪҼ] Ϋϥ΢υαʔϏεΛ࢖͍ͬͯΔ
    • [ݪҼ] Credential ͷѻ͍͕͋΍͍͠…
    • [ରࡦ] SDK ʹ Stub/Fake/Simulator ͕͋Δ͔
    ֬ೝ͠Α͏
    • [ରࡦ] ͜͜͸ Mock Խ

    View Slide

  13. 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

    View Slide

  14. Happy Unit Testing!

    View Slide