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

batsを使おう / Let's use bats

6094c91088af5e8ea5094aa750d13570?s=47 cottondesu
September 16, 2018

batsを使おう / Let's use bats

bashのテストフレームワーク「bats」に
ついての簡単な例付きでご紹介

6094c91088af5e8ea5094aa750d13570?s=128

cottondesu

September 16, 2018
Tweet

Transcript

  1. CBUTΛ࢖͓͏ 2018年09月15日 kanazawa.rb meetup#73

  2. !DPUUPO@EFTV ΏΔͬͱ;ΘͬͱอकΤϯδχΞ ׂ৽ن։ൃ ׂอक ػೳ௥Ճɺػೳमਖ਼ɺόάमਖ਼ FUD

  3. CBTIॻ͍ͯ·͔͢ʁ

  4. CBTIͰ΋ςετ Ͱ͖Δͷ஌ͬͯ·͔ͨ͠ʁ

  5. ࣮͸

  6. CBUTͰग़དྷΔΜͰ͢

  7. CBUTͱ͸

  8. #BTI"VUPNBUFE 5FTUJOH4ZTUFN IUUQTHJUIVCDPNTTUFQIFOTPOCBUT

  9. CBTIͷςετϑϨʔϜϫʔΫ w ରԠ04 "SDI "MQJOF 6CVOUV 'SFF#4%YBOEY  NBD04 8JOEPXT

    CBUT
  10. Πϯετʔϧ͸؆୯

  11. Πϯετʔϧ IPNFCSFXͰΠϯετʔϧ CSFXJOTUBMMCBUTDPSF OQNͰΠϯετʔϧ OQNJOTUBMMHCBUT

  12. ࢖͍ํ

  13. w !UFTUͷ\^ͷϒϩοΫ͕ςετର৅ w CBUTϑΝΠϧʹهࡌ w CBUTίϚϯυͰCBUTϑΝΠϧΛ࣮ߦ ࢖͍ํ ϧʔϧ

  14. w TIFMMͷಡΈࠐΈ͸TFUVQ ʹهࡌ w SVOϑΝϯΫγϣϯ໊Ͱ࣮ߦ w SVOͷ݁Ռ͸#BUTͷ૊ΈࠐΈม਺ʹೖΔ ͨΊɼҎԼΛςετ݁Ռͱͯ͠࢖༻ ࢖͍ํ ϧʔϧ

    w \TUBUVT^εςʔλείʔυ w \PVUQVU^ग़ྗ݁Ռ w \MJOFT^ग़ྗ݁Ռʢ഑ྻʣ
  15. ࡞੒ྫ

  16. VTSCJOFOWCBUT !UFTUBEEJUJPOVTJOHCD\ˡ!UFTUͷ\^ͷϒϩοΫ SFTVMU FDIP cCD  <SFTVMUFR> ^ !UFTUBEEJUJPOVTJOHED\ SFTVMU

    FDIP QcED  <SFTVMUFR> ^ BEEJUJPOCBUT
  17. ࣮ߦྫ

  18. CBUTBEEJUJPOCBUT 㾎BEEJUJPOVTJOHCDˡ!UFTUͷ\^ͷϒϩοΫ໊ 㾎BEEJUJPOVTJOHED UFTUT GBJMVSFT

  19. 失敗例

  20. #!/usr/bin/env bats @test "addition ng" { result="$(echo 2+1 | bc)"

    [ "$result" -eq 4 ] } addition_ng.bats
  21. 実行例

  22. CBUTBEEJUJPO@OHCBUT ✗BEEJUJPOOH  JOUFTUGJMFBEEJUJPO@OHCBUT MJOF  A<SFTVMUFR>GBJMFE UFTU GBJMVSF

  23. ෳ਺ͷCBUTΛ࣮ߦ΋Մೳ

  24. CBUT CBUT 㾎BEEJUJPOVTJOHCD 㾎BEEJUJPOVTJOHED ✗BEEJUJPOOH  JOUFTUGJMFBEEJUJPO@OHCBUT MJOF  A<SFTVMUFR>GBJMFE

    UFTUT GBJMVSF
  25. TIFMMಡΈࠐΈྫ

  26. #!/bin/sh add() { echo $(($1 + $2)) } add.sh

  27. #!/usr/bin/env bats setup() { . add.sh } @test "input add

    eq 3" { run add 1 2 [ "${status}" -eq 0 ] [ "${output}" == "3" ] } @test "input add ng" { run add 1 4 [ "${status}" -eq 0 ] [ "${output}" == "4" ] } add.bats
  28. 実行例

  29. CBUTBEECBUT 㾎JOQVUBEEFR ✗JOQVUBEEOH  JOUFTUGJMFBEECBUT MJOF  A<\PVUQVU^>GBJMFE UFTUT GBJMVSF

  30. %FNP

  31. w Πϯετʔϧ΋࢖͍ํ΋؆୯ w ςετίʔυͷॻ͖ํ΋؆୯ w CBTIʹΑΔςετϥΠϑΛ࢝Ί·͠ΐ͏ ૯ׅ