SlideShare a Scribd company logo
1 of 47
UnitTestのための
  クラス設計
    t_ishida
自己紹介


名前:石田 武士(@t_ishida)

所属:アライドアーキテクツ システム部
注意
日頃UnitTestしている人には当たり前の話しか
しません

PHPUnitそのもの使い方は説明しません

このセッションは過去UnitTestにトライしてよ
く分からなかった、もしくは挫折した人に最
適化されています
では、早速
テストしてますか?
テストしてますよ
何度もブラウザから叩いて、こっちの分岐
入った「っぽい」し

何度もブラウザから叩いて、期待通りの回数
ループしてる「っぽい」し

何度もブラウザから叩いて、期待通りのメ
ソッド呼び出した「っぽい」し
テストしてますよ
何度もブラウザから叩いて、こっちの分岐
入った「っぽい」し
    死亡フラグ
何度もブラウザから叩いて、期待通りの回数
ループしてる「っぽい」し

何度もブラウザから叩いて、期待通りのメ
ソッド呼び出した「っぽい」し
本当にテストしてますか?
リロードデバッグですか?

実行タイミングが同じだというだけで、関連の
無いコードを同じ場所に追記しまくってません
か?

それをリロードでデバッグして「テストをしな
いで」リリースしていませんか?
本当にテストしてますか?

リロードデバッグですか?

実行タイミングが同じだというだけで、関連の無い
      死亡フラグ
コードを同じ場所に追記しまくってませんか?

それをリロードでデバッグして「テストをしないで」
リリースしていませんか?

結合テストだけしてテストした気になっていませんか?
にんげんさんてすとしないです?
だって・・・


DBが絡むから値固定できないし

ファイルに吐きだすから値検証できないし

ブラウザから入力しないとダメだし
だって・・・


DBが絡むから値固定できないし
   死亡フラグ
ファイルに吐きだすから値検証できないし

ブラウザから入力しないとダメだし
結合テストをして意味があるの
は単体テストをやり尽くしたあ
    とからです
UnitTestってなに?

身も蓋もない言い方をすれば単体テスト

ただし、本セッションで言う単体テストは
xUnit系テスト、主にPHPUnitのテスト

メソッドに色んなパターンの引数を渡して戻
り値が合ってるか比べるだけです
つまり、こんなコードを
こうテスト
UnitTestの考え方

“単体”での確認が出来れば良い

故に呼んでるメソッドの先の正しさは気にし
なくて良い(その先の正しさは、それ単体で確
認するから)

メソッド単体での正しさを徹底的に確認する
UnitTestのメリット

環境から切り離してロジック単体をテストで
きる

プログラムとして保存するから何度でも実行
出来る。そして速い。(回帰テスト自動化)

電算機のテストは正確 ("hoge" != 'hoge ')
UnitTestのデメリット

テストを作るのは時間がかかる

場合によってはテストのためにクラス設計を
歪める必要がある

必ずしも、それで全てのバグを検出すること
が出来ないので過信は禁物
だからと言って、単体
テストしない理由には
  なりません
ここから本題
UnitTestのためのクラス設計


 UnitTestを意識してクラス設計する必要がある

 故に既存のコードをテストに入れるのは大変

 どれだけ大変かはレガシーコード改善ガイド
 を読むと良いです
UnitTestのための
  クラス設計の指針

強い“依存”を徹底的に排除する

 クラス間の依存

 外部システムへの依存

複雑度を下げる
モック


そのクラスのように振る舞うダミーのこと

ダミーで入力を検証する

ダミーで出力を固定する
こういうこと
モック


モックを使うということはオブジェクトを差し替える
ということ
モックを使うための注意


  つまり・・・
モックを使うための注意

メソッド内で new してたら差し替えられない

staticメソッド呼び出しは差し替えられない

組み込み関数は差し替えられない

final な クラスも差し替えられない(モックは
継承して作る)
依存の排除(クラス間の依存)


 コンストラクタやメソッドの引数でオブジェ
 クトを渡す(ファクトリを渡すのも可)

 セッタで上書き可能なメンバ変数にする
オブジェクト渡し
オブジェクト渡し
セッタで上書き
セッタで上書き
おまけ(依存なし)
依存の排除(外部システム)


 プロキシパターン
プロキシパターン




DBなんか文字列(SQL文)渡したら配列を返すク
         ラスです
プロキシパターン




ファイルの読み込みなんか文字列(path)を渡し
  たら文字列を返すただのメソッドです
複雑度を下げる

同じタイミングで実行されるからと言って関
連の無いコードを同じ場所に書かない

ifの入れ子になったらメソッドを分割する

ループの中は別メソッドにする(走査と操作は
分ける)
ざっくばらんなまとめ

そのクラスに関連するオブジェクトはメンバ
変数にする

メンバ変数はセッターで上書き可能にする

メソッドは小まめに分ける

外部システムとの絡みはラッパーを作る
テストしない理由?

テスト書いている時間がないから

上司がテストに対する理解がなくて時間とら
せてくれないから

あとで書くから

複雑過ぎてテストが書けないから
テストしない理由?

テスト書いている時間がないから


   死亡フラグ
上司がテストに対する理解がなくて時間とら
せてくれないから

あとで書くから

複雑過ぎてテストが書けないから
時間無くてテスト書けない


それは手動でテストしている時間も無いはずな
のでリリースしてはならないコードです
上司の理解を得られない

理解を得る必要はありません

勝手に書いてください

自分の時間と健康と心の平穏を守るためにや
るのです
複雑過ぎてテスト書けない



それは、そもそも手動でもテストできません

メソッドを分割して複雑度を下げてください
あとで書く


絶対にあとで書きません
書いておけば妖精さんがテ
  ストしてくれます
ご清聴ありがとうございました
    http://tech.aainc.co.jp

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

UnitTestのためのクラス設計

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. ユニットテストの説明に前に本講義で多用されるオブジェクト指向というものについて説明しておこうと思います。\nオブジェクト指向とは何か?  というとよくある説明として「モノを中心にしたプログラミングのことだよ。 $apple->color = 'red'; みたいなね」 のようなものを見かけます。\n実際にそれは正しし、分かっている人には分かり易いのですが、分からない人にとってはただでさえ分からないところを一層煙に巻かれる状態になり易いので、別のアプローチから簡単に説明します。\nデータ構造とアルゴリズムをワンセットにした単位で扱うプログラミング手法のことです。構造体に変数と関数ポインタをセットにしたものだと思えば良いです。\nその関数ポインタで呼び出した内部で、その構造体の変数を"$this"として直接アクセスできるだけですね。PHPで言う場合、構造体ってハッシュのことだと思えば良いです。\n以下のコードは配列とクロージャを使ってイメージを表現したコードです。\n\n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. 人間さんはテストしないですか?\n
  14. \n
  15. \n
  16. 結合テストで確認できるのはインターフェースが\n一致しているということだけです。\n\n
  17. ユニットテストっていうのは単体テストのことです。ただし、ここではユニットテストと言う場合、オブジェクト指向プログミングにおけるxUnitを用いたクラス単位のユニットテストのことを指します。JavaならばJUnit、.NETならば NUnit、PHPならばPHPUnitを使います。\n\n
  18. \n
  19. \n
  20. \n
  21. プログラムとして保存する「テスト仕様書」であり「エビデンス」であり\n「プログラムの使い方を説明するドキュメント」であると考えれば良いです。\n他に、「無駄に複雑なコードが書けなくなる」というメリットも有ります。\n最低限「UnitTestが書けるレベルのコード」ということが保証されます。\n
  22. 3つ目は気になるところだと思うのですが、\nクラス境界、システム境界のインターフェースに齟齬が\nあるという種類のバグは検出出来ません\n
  23. \n
  24. \n
  25. こんなコマンド打つと実行できるよ\n
  26. \n
  27. \n
  28. Unitテストは単体テストなので "クラス単体" をテストします。\n依存を排除したクラス設計をしなければなりません。\n\n\n
  29. 依存を排除する理由はつまるところ、これです。\n
  30. モックを作るコードを説明する。\n・メソッドの入力の検証\n・出力の固定\nについて説明する\n\n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. DBWrapperをモックにして、コンストラクタの引数で渡しています。\n\nモックにして期待通りのSQL文が渡されることの確認と\nDBが返した値をそのまま返却することを確認しています。\n図中では畳んでますが、getTestCDで配列を定義しています。\n\n
  39. \n
  40. FileManagerをモックにしてセッタで設定しています。\nまた、自身のfindに対する依存があるため、\n自身をモック化してfindが返す値を固定しています。\n\n
  41. \n
  42. エスケープのテスト\n\n何にも依存しないユーティリティのようなものなので、ままテストできます。\n
  43. ラッパーのことです\n
  44. \n
  45. \n
  46. \n
  47. 引数で渡すにせよ関連するクラスはメンバ変数にしてしまうのが、\nUnitTest的には便利だし、オブジェクト指向の設計的にも理にかなってることが多いはず。\n\n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. 人間さんはテストしないですか?\n
  55. \n