AIで単体テストを書く

AIにテストスイートを生成させましょう。カバレッジ数値を上げるだけでなく、実際にバグを捕まえるテストを書くための実用的なワークフロー。

3 steps
codingdeveloper-tools
1

コードを書く前にすべてのテストケースをリストアップする

テストコードを1行も書く前に、AIに関数のすべてのテストケースをリストアップさせます。

Example prompt
List all test cases I should write for this function. Include: happy path scenarios, edge cases (empty input, null, zero, negative numbers, max values), error cases (invalid input, exceptions), and boundary conditions. Do not write test code yet — just list the cases. Function:

[PASTE FUNCTION]
2

ケースリストからテストコードを生成する

ケースリストをAIに渡し、実際のテストコードを書いてもらいます。各テストの正確性を確認してください。

Example prompt
Write [JEST/PYTEST/VITEST/etc.] unit tests for the following function, covering all these test cases: [PASTE CASE LIST FROM PREVIOUS STEP]. Use descriptive test names. Mock external dependencies. Include setup/teardown if needed. Function:

[PASTE FUNCTION]
3

AIに既存テストのギャップを見つけてもらう

既存のテストファイルとソースコードを貼り付けます。AIにどのシナリオがカバーされていないかを尋ねます。

Example prompt
Review my existing tests against the source code and identify: 1) Test cases that are missing, 2) Tests that test the wrong thing or have incorrect assertions, 3) Code paths that are not covered, 4) Scenarios that could break in production but are not tested. Source:

[PASTE SOURCE]

Existing tests:

[PASTE TESTS]
Ready to try AI/ML API?
Follow this playbook with the actual tool
View Tool