0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DBアクセスのテストコードを書いていて、テーブルにレコードを挿入した際にAUTO INCREMENTされるカラム値が合っているか確認するテストコードを書く必要が合って調べたことのメモ書き。

単純に期待値をテストコードにハードコーディングしてしまうと初回のテストはパスできるが、次回以降は失敗してしまう。

MySQLで次に採番されるAUTO_INCREMENT値を取得するSQLをInsertテストの前に実行して、この取得した値を期待値とすることで再現性のあるテストコードを作成できる。

次に採番されるAUTO_INCREMENT値を取得するSQL
SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_schema = DATABASE() 
  AND table_name = 'テーブル名';
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?