Testing and continuous integration
導入済み
6:03
ソフトウェアエンジニアリング
Software engineering is how systems outlive their authors. The first three units shape the product: requirements, architecture and interface design. The next…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| test pyramid/test ˈpɪrəmɪd/ | テストピラミッド |
| unit tests/ˈjuːnɪt tests/ | Tests of individual units of behaviour, usually isolated from unrelated parts of the system. |
| integration tests/ˌɪntɪˈɡreɪʃn tests/ | tests that check several parts working together |
| end-to-end tests/end tʊ end tests/ | tests of the whole system from the user's point of view |
| Continuous integration/kənˈtɪnjuːəs ˌɪntɪˈɡreɪʃn/ | building and testing every change automatically as soon as it is pushed |
| coverage/ˈkʌvərɪdʒ/ | the share of the code that the tests run |
| Mutation testing/mjuːˈteɪʃn ˈtestɪŋ/ | making small deliberate bugs to check that the tests catch them |
| stub/stʌb/ | a stand-in that returns fixed answers to the code under test |
| mock/mɒk/ | a stand-in that also checks how the code under test calls it |
| fake/feɪk/ | |
| Equivalence partitioning/ɪˈkwɪvələns pɑːˈtɪʃənɪŋ/ | dividing inputs into groups that should behave the same, and testing one value from each |
| Boundary value analysis/ˈbaʊndəri ˈvæljuː əˈnæləsɪs/ | testing values at and just beyond the edges of each input range |
| flaky tests/ˈfleɪki tests/ | tests that sometimes pass and sometimes fail with no code change |
テストピラミッド
- テストピラミッドでは、単体テストを多く(高速で、他から切り離して実行)、結合テストをそれより少なく(実際のデータベースを使用)、エンドツーエンドテストを少数(アプリ全体を対象に)行う。
- 継続的インテグレーション(CI)はプッシュのたびにこれらを実行し、結果が赤ならマージを阻止する。

The pyramid puts — at the base
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Many fast units, fewer integration, few slow end-to-end.
カバレッジを正しく見る
- 行カバレッジは実行された行の割合を数え、分岐カバレッジは両方の方向に進んだ分岐の割合を数える。このコースのテストスイートは、行カバレッジが92%、分岐カバレッジが81%である。実態をより正直に示すのは分岐カバレッジだ。テストされていない分岐はそれぞれ、テストされていない動作を意味する。
- 行カバレッジが85%に達しないとマージを阻止するゲートがあり、未テストの分岐の一覧が今後取り組むべき項目の一覧になる。アサーションのないテストは、カバレッジを見かけ上良くするだけである。
Branch coverage is the honest number because —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
92% line with 81% branch: the gap is decisions never exercised.
3つのレベルすべてでFR-2をテストする
- 同じルール、FR-2(返却期限 = 貸出日 + 21)を各レベルでテストする。
| レベル | テストで確認すること |
|---|---|
| 単体(ミリ秒) | 3つの日付:通常の日付、うるう年の2月、年末 |
| 結合(秒) | 貸出情報が保存され、正しく読み出せること |
| エンドツーエンド(最も遅い) | スクリプトで操作される会員が本を借り、返却期限を確認できること |
- CIで赤から緑へ:6冊目の貸出について失敗するテストをプッシュすると、CIは赤になる。上限を確認する処理を書くと緑になり、カバレッジが上がってマージの阻止が解除される。内部の詳細ではなく、動作(返却期限)をテストする。
Due = borrow date + 21 is tested at the unit level with —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Pure function: normal, leap February, year boundary.
Put the red-green cycle for the sixth-borrow rule in order.
難易度 3/5 · 200 コイン + 今日初回達成時の小ボーナス
Seeing the test fail first proves it can fail; a test that was never red may be testing nothing.
テストダブルとミューテーションテスト
- スタブは固定データを返す。モックはそれに加えて、どのように呼び出されたかも確認する(「リマインダーがちょうど1回送信された」)。フェイクは、メモリ内のデータベースのように、簡略化されていても実際に動くものである。テストダブルは境界部分で使い、テスト対象のロジックには決して使わない。
- ミューテーションテストはテスト自体を確認する。貸出上限の確認処理で
>=を>に変更し、テストをもう一度実行する。 - それでもすべてのテストに合格するなら、ミュータントは生き残っており、境界テスト(5冊借りている状態で6冊目を借りようとするテスト)が欠けている。ミューテーションスコアは、全ミュータント数に対する検出されたミュータント数の割合である。$50$のうち$45$なら$90\%$である。
A test suite kills 38 of 40 mutants. What is its mutation score, in per cent?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
38 / 40 = 0.95 = 95%.
A test must check that exactly one reminder was sent. Which double fits?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A mock records its calls, so the test can assert there was exactly one.
分割と境界
- 同値分割は、コードが同じように扱うべき入力をクラスに分ける。「貸出は最大5冊」の場合、0〜4冊を借りている状態(貸出可能)と、5冊以上を借りている状態(貸出拒否)に分ける。各クラスにつき1つのテストが最低限必要である。
- 境界値分析では、数が1つずれるバグが起きやすい境界にテストを追加する。4冊を借りている状態(貸出可能)と5冊を借りている状態(貸出拒否)をテストする。これこそが、
>=を>に変更したミュータントを検出するテストである。
For "a member may hold up to 5 loans", which pair of holdings is the boundary test?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
One allowed, one refused, on either side of the edge.
計算例:不安定なテストがもたらす影響
- 1%の確率でランダムに失敗するテストは、問題が小さく見える。そのようなテストが200個あり、それぞれの失敗が独立しているテストスイートでは、すべてが緑になる確率は$0.99^{200} = \mathbf{13.4\%}$である。
- すると、赤になったビルドの大半がノイズになり、人々は赤という結果を信頼しなくなり、本当の失敗を見逃してしまう。不安定なテストは切り離してすぐに修正し、緑になるまでただ再実行することは決してしない。
100 tests, each failing independently at random on 1% of runs: chance the suite is all green (one decimal, %)?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
$0.99^{100} = 0.366$.
Rerunning a flaky test until it passes is an acceptable fix.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
It hides real failures; quarantine and fix the cause.
失いやすい得点
- アサーションのないテスト。 カバレッジは上がるが、何も確認しない。
- 行カバレッジを実態そのものと考えること。 行カバレッジが$92\%$でも、分岐カバレッジはわずか$81\%$という場合がある。分岐カバレッジの数値を確認しよう。
- テストが失敗するところを一度も確認しないこと。 失敗し得ると分かるように、まず失敗するテストをプッシュして赤にしよう。
まとめ
- テストピラミッド:単体テストを多く、結合テストをそれより少なく、エンドツーエンドテストを少数行う。
- 実態をより正直に示すのは分岐カバレッジで、行カバレッジの$92\%$に対して$81\%$である。
- 内部の詳細ではなく動作をテストする。CIの実行結果が赤ならマージは阻止される。
何が問題でしたか?この内容はサイトの運営チームだけが確認します。