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

壊して育てる、テストの信頼性

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.
Avatar for naohisa naohisa
February 19, 2026

 壊して育てる、テストの信頼性

AIがコードもテストも書く時代、そのテストは本当に信頼できるのか。テストの偽陰性(検知漏れ)問題に着目し、コードを意図的に壊してテストの信頼性を検証するMutation Testingの手法を紹介します。

Avatar for naohisa

naohisa

February 19, 2026
Tweet

More Decks by naohisa

Other Decks in Technology

Transcript

  1. 偽陰性の例 バグがあって実際にレコードが作成されていなくてもテストが通ってしまう。 it "creates a user" do post "/users", params:

    { user: { name: "Alice" } } expect(response).to have_http_status(:created) end こうなっていれば検知できた。 it "creates a user" do expect { post "/users", params: { user: { name: "Alice" } } }.to change(User, :count).by(1) expect(response).to have_http_status(:created) end 9
  2. Mutation Testing コードを意図的に壊して、テストが検知できるか確かめる > を < に変える、true を false に変える、条件分岐を削除するなど

    # 元のコード # Mutation(変異) if user.admin? if !user.admin? grant_access grant_access end end テストが落ちなければ、そのテストは偽陰性を含んでいる可能性が高い。 テストを追加して検出力を上げ、正しく落ちるように改善していく。 →こうして信頼性を育てることができる。 11
  3. mutant gem RubyではMutation Testingを実行できる mutant というgemがある。 bundle exec mutant run

    --use rspec 'User#admin?' 指定したメソッドに対して自動で変異(コードを壊す操作)を生成し、テストで検知 できるか確認する ASTベースで変異を生成している RSpecやMinitestなどと連携して使える GitHub: https://github.com/mbj/mutant gem自体は10年ほど前からあるが、AIの台頭によって再注目されそう…? 12
  4. 実行例: Person#adult? # lib/person.rb # spec/person_spec.rb class Person it 'returns

    true for age 19' do def adult? expect(Person.new(age: 19).adult?).to be(true) @age >= 18 end end it 'returns false for age 17' do end expect(Person.new(age: 17).adult?).to be(false) end mutantを実行すると、17件の変異のうち2件が生き残ってしまった Mutations: 17 Results: 17 Kills: 15 Alive: 2 def adult? - @age >= 18 + @age > 18 end → age == 18 の境界値テストが不足していたことが分かった。 13
  5. @age >= 18 に対して生成される変異(17件) raise | super | (行削除) |

    nil @age | nil >= 18 | @age > 18 | @age == 18 @age.eql?(18)| @age.equal?(18) | 18 | @age >= nil @age >= 0 | @age >= 1 | @age >= 19 | @age >= 17 @age >= 18 (noop) たった1行のコードに対して17件の変異が生成される。 これを手動で確認するのは現実的ではない。 14